Search code examples
sassas-macro

SAS CALL SYMPUT not working inside a macro (and before it did)


I need to store in a macro variable the number of rows in a data set. So I used this:

%macro get_table_size(inset,macvar);
 data _null_;
  set &inset NOBS=size;
  call symput("&macvar",size);
 stop;
 run;
%mend;
%get_table_size(LOANTAPE.INSTRUMENT_VU21,NUM_REG_INS);
%put &NUM_REG_INS;

Before my computer crashed (having to force it to reboot with SAS opened), this worked (I swear xd). But now, the macro NUM_REG_INS is not created. The log says: Apparent symbolic reference MACVAR not resolved.

So I checked the code as a data step and not a macro, like this:

data _null_;
  set LOANTAPE.INSTRUMENT_VU21 NOBS=size;
  call symput("macvar",size);
 stop;
run;
 %put &macvar

And it works. So the problem is when using this code inside a macro. Does anybody knows what could be happening here and how to fix it? And, just for the sake of curiosity, why was it working before?

Thank you, really!!


Solution

  • Variable scope. Look into using CALL SYMPUTX() instead of CALL SYMPUT(). It likely worked before because you either created the macro variable globally while testing and in this case you haven't. Macro variables do not exist outside the macro unless you create them as a global macro variable.

    call symputx("&macvar", size, 'g');
    

    See the documentation here