Wioleta Zgliczyńska
Wioleta Zgliczyńska

Reputation: 57

Dynamic n in function LAG<n> (variable) SAS

do you know how to use n in function LAGn(variable) that refer to another macro variable in the program-> max in my case?

data example1;                                                 
input value;                                                  
datalines;                                                    
1.0                                                          
3.0                                                            
1.0                                                          
1.0                                                          
4.0                                                           
1.0                                                          
1.0                                                          
2.0                                                          
4.0                                                            
2.0                                                          
;       
proc means data=example1 max;
output out=example11 max=max;
run; 
data example1;  
%let n = max;  
lagval=lag&n.(value);                                            
run;                                                          

proc print data=example1;                                                   
run;

Thank you in advance! Wiola

Upvotes: 2

Views: 433

Answers (2)

Tom
Tom

Reputation: 51601

It is easy to use a macro variable to generate the N part of LAGn() function call.

%let n=4 ;
data want;
  set have ;
  newvar = lag&n(oldvar);
run;

Remember that macro code is evaluated by the macro pre-processor and then the generated code is executed by SAS. So placing %LET statements in the middle of a data step is just going to confuse the human programmer.

Upvotes: 0

user667489
user667489

Reputation: 9569

Is this what you're trying to do?

data example1;                                                 
input value;                                                  
datalines;                                                    
1.0                                                          
3.0                                                            
1.0                                                          
1.0                                                          
4.0                                                           
1.0                                                          
1.0                                                          
2.0                                                          
4.0                                                            
2.0                                                          
;       

proc sql;
  select max(value) format = 1. into :n
  from example1;
quit;

data example1;
  set example1;
  lagval=lag&n(value);                                            
run;     

The format = 1. bit makes sure that the macro variable generated by proc sql doesn't contain any leading or trailing spaces that would mess up the subsequent data step code.

Upvotes: 2

Related Questions