First I provide code where the 6 month is hard-coded, and then I modify the code to allow for 10 month or 12 month or whatever.
/* UNTESTED CODE */
data want;
set have;
array v var1-var276;
array pctchg pctchg1-pctchg276;
do i=1 to dim(v);
pctchg(i)=100*(v(i)-lag5(v(i)))/lag5(v(i)); /* 6 months ago is found via lag5 */
end;
run;
Example code where you can change the lag
/* UNTESTED CODE */
%let lag_desired=10;
%let lag_to_use=%eval(&lag_desired-1);
data want;
set have;
array v var1-var276;
array pctchg pctchg1-pctchg276;
do i=1 to dim(v);
pctchg(i)=100*(v(i)-lag&lag_to_use(v(i)))/lag&lag_to_use(v(i));
end;
run;
Its not exactly clear to me how your data is arranged. Is it something like this
var1 var2 data for q12010 data for q12010 data for q22010 data for q22010 data for q32010 data for q32010 ... ...
If yes, then we can help you create a program. If no, then we need to know the arrangement of the data before we can be of any help, in this case SHOW US a portion of the data (we don't need all 300 variables, but 2 or 3 would be great).
Thanks, now please walk us through one example of performing the calculation of "lag of 6 Quater percentage change", using the above data that you provided.
First I provide code where the 6 month is hard-coded, and then I modify the code to allow for 10 month or 12 month or whatever.
/* UNTESTED CODE */
data want;
set have;
array v var1-var276;
array pctchg pctchg1-pctchg276;
do i=1 to dim(v);
pctchg(i)=100*(v(i)-lag5(v(i)))/lag5(v(i)); /* 6 months ago is found via lag5 */
end;
run;
Example code where you can change the lag
/* UNTESTED CODE */
%let lag_desired=10;
%let lag_to_use=%eval(&lag_desired-1);
data want;
set have;
array v var1-var276;
array pctchg pctchg1-pctchg276;
do i=1 to dim(v);
pctchg(i)=100*(v(i)-lag&lag_to_use(v(i)))/lag&lag_to_use(v(i));
end;
run;
If I am understanding you properly
data want;
set have;
array v var1-var276;
array pctchg pctchg1-pctchg276;
array lagpctchg lagpctchg1-lagpctchg276;
do i=1 to dim(v);
pctchg(i)=100*(v(i)-lag5(v(i)))/lag5(v(i)); /* 6 months ago is found via lag5 */
lagpctchg(i)=lag(pctchg(i));
end;
drop i;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.