Programming the statistical procedures from SAS

Do Loop

Reply
Super Contributor
Posts: 1,040

Do Loop

Hi Team,
Data related questions:
In the data below what is the variable with the name last_cd4?
why is cd4date variable appearing twice in the data?
why is the cd4 data to the bottom right side having negative values

Question?
For instance, id 20, I want to calculate a difference between cd4=332 (first date) and cd4=71 (20Nov2007) in which this date is the nearest one year after the first test.

Also, id 40, a difference between cd4=20 (30Jan2003) and cd4=239 (19Jun2003) that the date is nearest one year after the first test.

Code related Questions:
why is the do loop placed prior to the SET statement??
in the delay variable we are doing the interval check.
we are checking the days....the difference between first date and CD4 date...
why are we substracting the equation with 365
could you explain the code from there????

Thanks.


Obs id CD4COUNT CD4DATE cd4   cd4date  last_cd4 cd4date 12month     cd4
1 20     332   29MAY2006 332  29MAY2006      6 03NOV2009 29MAY2007    326
2 20     267   12JUN2006 332  29MAY2006      6 03NOV2009 29MAY2007    326
3 20     207   05DEC2006 332  29MAY2006      6 03NOV2009 29MAY2007    326
4 20      71   20NOV2007 332  29MAY2006      6 03NOV2009 29MAY2007    326
5 20      15   24JUN2008 332  29MAY2006      6 03NOV2009 29MAY2007    326
6 20       8   07AUG2008 332  29MAY2006      6 03NOV2009 29MAY2007    326
7 20       3   02JUN2009 332  29MAY2006      6 03NOV2009 29MAY2007    326
8 20       6   03NOV2009 332  29MAY2006      6 03NOV2009 29MAY2007    326
9 40      20   30JAN2003 20  30JAN2003     326 14DEC2010 30JAN2004   -306
10 40      10   13MAR2003 20  30JAN2003     326 14DEC2010 30JAN2004   -306
11 40     300   08MAY2003 20  30JAN2003     326 14DEC2010 30JAN2004   -306
12 40     239  19JUN2003  20  30JAN2003     326 14DEC2010 30JAN2004   -306
run;


proc sort data=have; by id CD4DATE; run;

data want;
do until(last.id);
     set have; by id;
     if first.id then do;
          firstDate = CD4DATE;
          firstCD4 = CD4COUNT;
          end;
     else do;
          delay = abs(intck("DAY", firstDate, CD4DATE) - 365);
          minDelay = min(delay, minDelay);
          if minDelay = delay then do;
               yearCD4 = CD4COUNT;
               lastDate = CD4DATE;
               end;
          end;
     end;
format firstDate lastDate date9.;
keep id firstDate firstCD4 yearCD4 lastDate;
run;

proc print; run;

Ask a Question
Discussion stats
  • 0 replies
  • 104 views
  • 0 likes
  • 1 in conversation