BookmarkSubscribeRSS Feed
rj
Obsidian | Level 7 rj
Obsidian | Level 7

Hi All, 

 

I need to create 4 lag terms each for 15 different variables, is there a way to create macro for 4 lag terms for each of my variable in SAS UE.

 

Usually, I use Var A1=lag1(VarA);

for creating lag but what if we have more than 20 variables and need to create more than 4 lag terms?

 

Thanks in advance for any suggestions/advise/cool insights!

 

RJ

 

 

 

 

10 REPLIES 10
ballardw
Super User

Since your macro would have to create 20*4 (per the example) NEW variables how are you going to use them? How will you know what the names are for other uses? You may need to provide a bit more detail about what the overall goal and further processes for this to get something workable.

Reeza
Super User

I'd use proc expand with a macro loop.

 

But I'd also suggest looking at PROC TIMESERIES to see if it's helpful in this situation.

rj
Obsidian | Level 7 rj
Obsidian | Level 7
I will look up Proc timeseries and proc expand.Thanks!
Reeza
Super User

@rj I modified the macro above to work for multiple lags & multiple variables.

Shmuel
Garnet | Level 18

I don't understand what do you mean by "4 lag terms".

If you need create lag to many variables you can use next macro, as in this demo:

%macro lag(vars);
  %let m = %sysfunc(countw(&vars)); 
  %do i=1 %to &m;
     %let var = %scan(&vars,&i);
     %do; lag_&var = lag(&var); %end;
  %end;
%mend lag;

data test;
 set sashelp.class;
     %lag(age height name sex);  /* variables to create lag_xxx to var xxx */
run;
Reeza
Super User

And add another loop/parameter to do more lags. 

Mostly stolen from @Shmuel

 

%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;
rj
Obsidian | Level 7 rj
Obsidian | Level 7
VarA1=lag1(var A);
VarA2=lag2(var A);
VarA3=lag3(var A);
VarA4=lag4(var A);
Astounding
PROC Star

You can't get around needing all these statements.  But what you can do is have macro language generate them for you.  For example:

 

%macro all_lags (varlist=);

   %local i j nextvar;

   %do i=1 %to %sysfunc(countw(&varlist));

      %let nextvar = %scan(&varlist, &i);

      %do j=1 %to 4;

         &nextvar&j = lag&j(&nextvar);

      %end;

   %end;

%mend all_lags;

 

Then within the DATA step that creates all 80 additional variables:

 

%all_lags (varlist=VarA VarB VarC ....... VarT)

ballardw
Super User

Note that all of these approaches may have issues with 1) long variable names: adding 4 or more characters to create the new variables means that an existing variable with more than 28 characters could create an invalid variable name and  2) could result in collision of names, using one that already exists in the data set.

Ksharp
Super User
It is a lot easy for IML code.


proc iml;
use sashelp.class;
read all var{age};
close;
lag=lag(age,1:4);
print lag;
quit;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 10 replies
  • 3534 views
  • 3 likes
  • 6 in conversation