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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.