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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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