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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 17 replies
  • 4490 views
  • 1 like
  • 7 in conversation