Advertisement
Guest User

Untitled

a guest
Aug 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 0.84 KB | None | 0 0
  1. /*create simulated dataset with 3 patients, A, B, and C*/
  2. data ds1;
  3.      do patient='A','B','C';
  4.           do month=1 to 7;
  5.           num=int(ranuni(0)\*10);
  6.           output;
  7.      end;
  8. end;
  9. run;
  10.  
  11. /*sort dataset by level of summarization*/
  12. proc sort;
  13.   by patient, month;
  14. run;
  15.  
  16. /*set the lookback period or number of months to lag by*/
  17. %let n = 4;
  18.  
  19. /*compute statistic of interest*/
  20. data ds2;
  21. set ds1;
  22.   by patient;
  23. retain num_sum 0;
  24. if first.patient then do;
  25.      count=0;num_sum=0;
  26. end;
  27. count+1;
  28. last&n=lag&n(num);
  29. if count gt &n then num_sum=sum(num_sum,num,-last&n);
  30.      else num_sum=sum(num_sum,num);
  31. /*here we compute a moving average, but this can be any statistic*/
  32. if count ge &n then mov_aver=num_sum/&n;
  33.      else mov_aver=.;
  34. run;
  35.  
  36. /*print results*/title 'Moving average within BY-Group';
  37. proc print;
  38. run;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement