LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (January 2010, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 28 Jan 2010 21:59:08 -0600
Reply-To:     Yu Zhang <zhangyu05@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Yu Zhang <zhangyu05@GMAIL.COM>
Subject:      Re: Summing number of days based on a YES/NO var - not getting it
              right
Comments: To: Sadia Ali <alisadia786@gmail.com>
In-Reply-To:  <527cda961001281344m7984b065l45b37ae679f44a0c@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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 > > > > > >


Back to: Top of message | Previous page | Main SAS-L page