Hi, I am trying to add the values of mnth in the order of code starting from 1 to 5, the 2 to 6, 3 to 7 etc. Any help in doing this?
data score;
input code mnth yr;
datalines;
1 1.1 25
2 1.2 26
3 1.3 25
4 2.1 24
5 3.2 26
6 3.4 67
7 5.6 56
8 6 34
9 7 45
9 7 45
;
run;
First, we need to make sure we only have one observation per code:
proc summary data=score;
by code;
var mth;
output out=comp (drop=_type_ _freq_) sum()=;
run;
Then, we create the rolling window with a temporary array, and start outputting once it is filled:
data want;
set comp;
array vals {5} _temporary_;
vals{mod(_n_,5)+1} = mnth;
if _n_ ge 5;
mnth = sum (of vals{*});
keep code mnth;
run;
What shoild the output look like in terms of variables and observations?
Should it start the first time the rolling window is filled with 5 values, or should the months before that appear with the sum of 1,2,3,4 values?
First, we need to make sure we only have one observation per code:
proc summary data=score;
by code;
var mth;
output out=comp (drop=_type_ _freq_) sum()=;
run;
Then, we create the rolling window with a temporary array, and start outputting once it is filled:
data want;
set comp;
array vals {5} _temporary_;
vals{mod(_n_,5)+1} = mnth;
if _n_ ge 5;
mnth = sum (of vals{*});
keep code mnth;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.