[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Re: Summing number of days based on a YES/NO var - not getting it
From:       Sadia Ali <alisadia786 () GMAIL ! COM>
Date:       2010-01-29 20:02:30
Message-ID: 527cda961001291202u77096433p4e5600da1774e216 () mail ! gmail ! com
[Download RAW message or body]

Thanks Daniel & Yu Zhang for your ideas.
Both your codes worked.

-AS
2010/1/29 Daniel Fernández <fdezdan@gmail.com>

> ok,
> what I understand clear is that you want to count from flg=1 ocurrence
> to the first flg=0
> occurrence by pt.
>
> I hope this code give you a better approach to the solution:
>
> * we sign begin and end dates for each Pt;
> data need;
> set test;
> by  pt flg  notsorted;
> dateSAS=input(dtc,date9.);
> if (first.flg and flg=1) then begin=datesas;
> if (first.flg and flg=0) then end=datesas;
> if first.pt then end=.;
> if (last.pt and flg=1) then end=datesas;
> run;
>
> * we calculate the interval of days between them or until last date
> possible;
> data need;
> set need;
> by pt flg begin notsorted;
> retain x;
> if begin ne . then x=begin;
> if first.pt then end=.;
> else delay=intck('day',x,end);
> drop x;
> run;
>
> Daniel Fernandez.
> Barcelona.
>
>
>
>
>
>
>
> 2010/1/29 Yu Zhang <zhangyu05@gmail.com>:
>  > Not sure if it will help.
> > data test;
> > input pt $2. dtc date9. flag ;
> > cards;
> > X113SEP20061
> > X113DEC20060
> > X121MAR20071
> > X106JUN20070
> > X119SEP20071
> > X119DEC20071
> > X111MAR20081
> > X119JUN20081
> > X117SEP20081
> > X111DEC20080
> > X119MAR20090
> > X103JUN20090
> > X211DEC20060
> > X202APR20071
> > X229JUN20071
> > X224SEP20071
> > X204JAN20081
> > X210APR20080
> > X230JUN20081
> > X229SEP20081
> > X205JAN20091
> > X206APR20091
> > X309MAR20070
> > X326JUN20070
> > X301OCT20071
> > ;
> > RUN;
> >
> >
> > data want;
> > do until(last.pt);
> > set test;
> > by pt;
> > diff=ifn(first.pt,0,dif(dtc));
> > if flag=0 then flagzero+1;
> > if flagzero<2 then sumday+diff;
> > else do; flagzero=.;sumday=.;end;
> > output;
> > end;
> > run;
> >
> > 2010/1/28 Sadia Ali <alisadia786@gmail.com>
> >
> >> Hi Daniel,
> >>
> >> Thanks for looking into this. Your code has given me a direction -
> Thanks
> >> for that!
> >>
> >> I tried your code. It gives me the number of days between from flag=1
> just
> >> above the flag=0 to the flag=0.
> >> for ex if the data is:
> >> pt dt                  flg
> >> x1 11DEC2006   0
> >> x1 02APR2007   1
> >> x1 29JUN2007    1
> >> x1 24SEP2007   1
> >> x1 04JAN2008    1
> >> x1 10APR2008   0
> >> your code gives me:
> >>  pt dt                  flg  day               delay
> >> x1 11DEC2006   0     .
> >> x1 02APR2007   1     .
> >> x1 29JUN2007    1     .
> >> x1 24SEP2007   1     .
> >> x1 04JAN2008    1    .
> >> x1 10APR2008   0    04JAN2008      97
> >>
> >> what I want is the number of day from *first falg=1 to second flag = 0*.
> >> If I add to your code flg=0 and also add code to sum up the delay values
> I
> >> almost get what I want. Besides that I need to add code to instruct SAS
> to
> >> keep adding through the first zero and stop adding at the second zero:
> >>
> >> data need;
> >> do until (flg=0);
> >> set have;
> >> by pt dateSAS;
> >> if first.pt or *flg=0* then day=.;
> >> delay=intck('day',day,datesas);
> >> output;
> >> day=datesas;
> >> end;
> >> run;
> >>
> >>  Now I get:
> >>
> >>  pt dt                  flg  day               delay
> >> x1 11DEC2006   0     .
> >> x1 02APR2007   1     .
> >> x1 29JUN2007    1   02APR2007      88
> >> x1 24SEP2007   1   29JUN2007       87
> >> x1 04JAN2008    1   24SEP2007      102
> >> x1 10APR2008   0    .                      .
> >>
> >> Thanks!!
> >> AS
> >>
> >>
> >> 2010/1/28 Daniel Fernández <fdezdan@gmail.com>
> >>
> >> > hi Ali Sadia,
> >> >
> >> > I am not really sure what you want.
> >> >
> >> > My code counts the numbers of days from a flag=1 to the first flag=0
> >> > it searches.
> >> > I hope it can help you.
> >> >
> >> > data have;
> >> > set test;
> >> > dateSAS=input(dtc,date9.);
> >> > run;
> >> >
> >> > proc sort data=have; by pt dateSAS; run;
> >> >
> >> > data need;
> >> > do until (flg=0);
> >> > set have;
> >> > by pt dateSAS;
> >> > if first.pt or flg=1 then day=.;
> >> > delay=intck('day',day,datesas);
> >> > output;
> >> > day=datesas;
> >> > end;
> >> > run;
> >> >
> >> > Daniel Fernandez.
> >> > Barcelona.
> >> >
> >> >
> >> >
> >> > 2010/1/28 Ali Sadia <alisadia786@gmail.com>:
> >> >  > Hi,
> >> > >
> >> > > I have been working on deriving a variable with the below
> >> specification.
> >> > > This variable is only a component for deriving
> >> > > another larger variable. I have been working on this for a while
> with
> >> no
> >> > > outcome, so would appreciate help from you all:
> >> > >
> >> > > The data has several subjects with a date variable and a flag
> variable.
> >> > > What is required to be done is that the data should be sorted by pt
> and
> >> > dt.
> >> > > The
> >> > > sum of number of days from the first date to the last date for each
> pt
> >> > > should be calculated
> >> > > depending on the value of the flag variable, such that:
> >> > > The summing is done if flag = 1 till number of days = 365, if flag =
> 0
> >> is
> >> > > encountered before the
> >> > > sum number of days=365 the summing can go on, but if a flag=0 is
> again
> >> > > encountered
> >> > > then the summing should be abandoned and the code should look for
> the
> >> > next
> >> > > flag = 1 for the
> >> > > same pt so as to count till sum =365. This check for sum of 365 can
> be
> >> > done
> >> > > for all records that exist for that pt.
> >> > > If the sum doesnot equal to 365 this way then the counting
> >> > > should be started from the last date onwards to see if there is a
> total
> >> > of
> >> > > 60 days counting backwards
> >> > > for flag = 1. The same exception rule again applies that flag = 0
> can
> >> be
> >> > > encountered only once even
> >> > > when counting backwards. The backward summing can be checked only
> for
> >> the
> >> > > last 60 days, cannot be itirated for all the dates for that pt.
> >> > >
> >> > > My efforts: did lag of the dt variable, then calculated the diff  as
> >> > dt-lag
> >> > > (dt) and then summed up the diff variable to get the sum till it
> >> reached
> >> > > 365, but was not able to restrict the counting based on flag
> >> > >
> >> > > Can someone please help??
> >> > > sample data:
> >> > >
> >> > > data test;
> >> > > input pt $2. dtc $9. flg ;
> >> > > cards;
> >> > > X113SEP20061
> >> > > X113DEC20060
> >> > > X121MAR20071
> >> > > X106JUN20070
> >> > > X119SEP20071
> >> > > X119DEC20071
> >> > > X111MAR20081
> >> > > X119JUN20081
> >> > > X117SEP20081
> >> > > X111DEC20080
> >> > > X119MAR20090
> >> > > X103JUN20090
> >> > > X211DEC20060
> >> > > X202APR20071
> >> > > X229JUN20071
> >> > > X224SEP20071
> >> > > X204JAN20081
> >> > > X210APR20080
> >> > > X230JUN20081
> >> > > X229SEP20081
> >> > > X205JAN20091
> >> > > X206APR20091
> >> > > X309MAR20070
> >> > > X326JUN20070
> >> > > X301OCT20071
> >> > > ;
> >> > > RUN;
> >> > >
> >> > > Thanks in Advance -
> >> > > AS
> >> > >
> >> >
> >>
> >
>
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic