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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 852 views
  • 1 like
  • 2 in conversation