Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*create simulated dataset with 3 patients, A, B, and C*/
- data ds1;
- do patient='A','B','C';
- do month=1 to 7;
- num=int(ranuni(0)\*10);
- output;
- end;
- end;
- run;
- /*sort dataset by level of summarization*/
- proc sort;
- by patient, month;
- run;
- /*set the lookback period or number of months to lag by*/
- %let n = 4;
- /*compute statistic of interest*/
- data ds2;
- set ds1;by patient;
- retain num_sum 0;
- if first.patient then do;
- count=0;num_sum=0;
- end;count+1;
- last&n=lag&n(num);
- if count gt &n then num_sum=sum(num_sum,num,-last&n);
- else num_sum=sum(num_sum,num);
- /*here we compute a moving average, but this can be any statistic*/
- if count ge &n then mov_aver=num_sum/&n;
- else mov_aver=.;
- run;
- /*print results*/title 'Moving average within BY-Group';
- proc print;
- run;
-
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement