BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Agent1592
Pyrite | Level 9

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
2 REPLIES 2
learsaas
Quartz | Level 8
&var._lag&j = lag&j(&var);
Agent1592
Pyrite | Level 9

Thank you!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1257 views
  • 1 like
  • 2 in conversation