Dear SAS Community:
I already have a sample code for creating lagged variables. However I would like to change the naming of the variables with underscore. Currently the variables are named as lag_&var.&j. I would like to change it to &var_lag&j. So for example if the variable name is age, I would like to have the lagged variable to age_lag1. I tried to change the code but I have problems with the underscores. Thanks much!
%macro lag(vars, lags);
%let m = %sysfunc(countw(&vars));
%do i=1 %to &m;
%let var = %scan(&vars,&i);
%do j=1 %to &lags;
%do;
lag_&var.&j = lag&j(&var);
%end;
%end;
%end;
%mend lag;
data test;
set sashelp.class;
%lag(age height name sex, 4); /* variables to create lag_xxx to var xxx */
run;
&var._lag&j = lag&j(&var);
&var._lag&j = lag&j(&var);
Thank you!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.