BookmarkSubscribeRSS Feed
TalonHill
Calcite | Level 5

Hello!  I have a question which I'm sure is very easy, but I'm stumped.

How do I generate lags of multiple variables at once?  I have 105 variables -- each variable I need to lag once and twice.

I've created an array, and tried a number of DO statements, but nothing seems to work.  Anyone have any ideas?

Thanks!

3 REPLIES 3
AncaTilea
Pyrite | Level 9

Something like this could work. But you have to be careful with you first.keyword to reset all the lag values to . so it doesn't carry over the value from prior record...

but this piece of code should get you started:

proc sql;

    select cat("prev_",name,"= lag(",name,");") into: my_list separated by " "

    from dictionary.columns

    where libname = "SASHELP" & memname = "CLASS" ;

quit;

data temp;

    set sashelp.class;

    &my_list.;

run;

All you need to change is the name of your library (WORK?) and the name of the dataset. Make sure you use CAPS.


Good luck!

Anca

ballardw
Super User

array vars <your list of vars here>;

array L1vars <your list of vars prefixed with L1 here>;

array L2vars <your list of vars prefixed with L2 here>;

Do i = 1 to dim(vars);

     L1Vars = lag(vars);

     L2Vars = lag2(vars);

end;

drop i;

should work unless you make the do loop conditional.

The LAG2 variables will be blank for the first two records and the Lag variables blank on the first record.

Ksharp
Super User

Tow optional ways :

set x point=_n_-1

And

HashTable

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 2780 views
  • 0 likes
  • 4 in conversation