Advertisement
Guest User

Untitled

a guest
Aug 25th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 0.78 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. /*sort dataset by level of summarization*/
  11. proc sort;
  12. by patient, month;
  13. run;
  14. /*set the lookback period or number of months to lag by*/
  15. %let n = 4;
  16. /*compute statistic of interest*/
  17. data ds2;
  18. set ds1;by patient;
  19. retain num_sum 0;
  20. if first.patient then do;
  21. count=0;num_sum=0;
  22. end;count+1;
  23. last&n=lag&n(num);
  24. if count gt &n then num_sum=sum(num_sum,num,-last&n);
  25. else num_sum=sum(num_sum,num);
  26. /*here we compute a moving average, but this can be any statistic*/
  27. if count ge &n then mov_aver=num_sum/&n;
  28. else mov_aver=.;
  29. run;
  30. /*print results*/title 'Moving average within BY-Group';
  31. proc print;
  32. run;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement