DATA Step, Macro, Functions and more

Macro set with CALL SYMPUTX not resolving in same step

Reply
New User
Posts: 1

Macro set with CALL SYMPUTX not resolving in same step

[ Edited ]

Capture.PNG

 

Got this error from this Macro:

 

data _null_;

call symputx('L12MODATE',intnx('month',today(),-12,'E'));

call symputx('L1MODATE',intnx('month',today(),-1,'E'));

call symput('testADJL12M',put(&L12MODATE,Monyy7.));

run;

 

 

 

 

Respected Advisor
Posts: 2,841

Re: Macro set with CALL SYMPUTX not resolving in same step

[ Edited ]

It won't resolve in the same data step. Put it in a new data step. (Or just use the formula for &L12MODATE instead of &L12MODATE) (Or, make L12MODATE a data step variable and then you can use it wherever you want in the data step)

--
Paige Miller
Super User
Posts: 9,931

Re: Macro set with CALL SYMPUTX not resolving in same step

Just to clarify further:

&L12MODATE is resolved while the code is being fetched for compilation. call symput() executes after compilation, when the compiled data step is run.

If you want to keep values across dataset observations (and therefore data step iterations), you can use the symget() function to retrieve macro variables set with call symput() in the same data step. But before doing this, look if other methods (retained variables, hash objects, temporary arrays or the lag() function) might not do the same, and avoid the macro detour.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,441

Re: Macro set with CALL SYMPUTX not resolving in same step

[ Edited ]

Or just do it using code:

data _null_;
  call symputx('L12MODATE',intnx('month',today(),-12,'E'));
  call symputx('L1MODATE',intnx('month',today(),-1,'E'));
  call symput('testADJL12M',put(intnx('month',today(),-12,'E'),Monyy7.));
run;

Or, to add, you could just use the base SAS code you give above in your code rather than use macro variables at all.  I.e. as a programmer I know what:

intnx('month',today(),-12,'E')

means, however (and for the sake of a few typing characters):

&L12MODATE.

Is totally meaningless to me, and means I then have to spend further time decoding what is happening - this is what obfuscation does. 

Respected Advisor
Posts: 3,847

Re: Macro set with CALL SYMPUTX not resolving in same step

You need SYMGET(N).

 

27         data _null_;
28            call symputx('L12MODATE',intnx('month',today(),-12,'E'));
29            call symputx('L1MODATE',intnx('month',today(),-1,'E'));
30            call symputx('testADJL12M',put(symgetn('L12MODATE'),Monyy7.));
31            run;

    

32         %put &=L12modate &=testADJL12M;
L12MODATE=20788 TESTADJL12M=NOV2016
Respected Advisor
Posts: 2,841

Re: Macro set with CALL SYMPUTX not resolving in same step

Posted in reply to data_null__

data_null__ wrote:

You need SYMGET(N).

 

27         data _null_;
28            call symputx('L12MODATE',intnx('month',today(),-12,'E'));
29            call symputx('L1MODATE',intnx('month',today(),-1,'E'));
30            call symputx('testADJL12M',put(symgetn('L12MODATE'),Monyy7.));
31            run;

    

32         %put &=L12modate &=testADJL12M;
L12MODATE=20788 TESTADJL12M=NOV2016

I'm sorry, all this symget or symgetn is completely unnecessary and overcomplicates things. 

 

data _null_;
    l12modate=intnx('month',today(),-12,'E');
    call symputx('L12MODATE',l12modate);
    call symputx('L1MODATE',intnx('month',today(),-1,'E'));
    call symputx('testADJL12M',put(l12modate,Monyy7.));
run;

 

 

--
Paige Miller
Ask a Question
Discussion stats
  • 5 replies
  • 155 views
  • 6 likes
  • 5 in conversation