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

Thanks!

Ksharp
Super User

Here is the IML code . I believe it is going to be faster than John King(data _null_) 's code .

 

 

data have;
infile cards expandtabs truncover;
input X1	X2	X3	X4	X5;
cards;
1	11	21	31	41
2	12	22	32	42
3	13	23	33	43
4	14	24	34	44
5	15	25	35	45
6	16	26	36	46
7	17	27	37	47
8	18	28	38	48
9	19	29	39	49
;
run;

%let lag=5;
proc iml;
use have;
read all var _num_ into x[c=vnames]; 
close;  
varname=j(ncol(x),&lag,blankstr(nleng(vnames)+8));
do i=1 to &lag;
 varname[,i]=t(cats(vnames,'_',i));
end; 
varname=vnames||rowvec(varname); 

lag=j(nrow(x),&lag#ncol(x),.); 
idx=do(1,&lag#ncol(x),&lag); 
l=1:&lag;
do i=1 to ncol(idx);
 lag[,idx[i]:idx[i]+&lag-1]=lag(x[,i],l);
end;
want=x||lag;  
create want from want[c=varname];
append from want;
close;
quit;
Ksharp
Super User

Here is the new Data Step code , also faster than John King(data _null_)'s code and no need to consider about if the variable like var1-var5 . It is applied to any variable name .

 

 

data have;
infile cards expandtabs truncover;
input X1	X2	X3	X4	X5;
cards;
1	11	21	31	41
2	12	22	32	42
3	13	23	33	43
4	14	24	34	44
5	15	25	35	45
6	16	26	36	46
7	17	27	37	47
8	18	28	38	48
9	19	29	39	49
;
run;

%let lag=5;

data x;
 do i=1 to &lag;
  output;
 end;
run;
proc transpose data=have(obs=0) out=temp;
var _all_;
run;
proc sql;
  select cats(_name_,'_',i,'=lag',i,'(',_name_,')') into : lag separated by ';'
   from temp,x ;
quit;
data want;
 set have;
 &lag ;
run;

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
  • 17 replies
  • 1998 views
  • 1 like
  • 7 in conversation