1
votes

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!!

2

2 Answers

1
votes

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

1
votes

If you want to access the macro variable outside of the macro then make sure it is not defined as only local to the macro. You could use the third parameter of call symputX to force the update of a global macro variable. But then you could have trouble trying to call this macro to update a macro variable that is local to the calling macro.

Just add a line to the macro to force the macro variable to be global when it does not exist. You should also use the newer CALL SYMPUTX() function, unless you really wanted those leading spaces in the value of macro variable that the automatic conversion of SIZE to a character string would have generated by using the older CALL SYMPUT() function.

Also move the CALL SYMPUTX() to before the SET statement so it runs even if the dataset has zero observations.

%macro get_table_size(inset,macvar);
%if not %symexist(&macvar) %then %global &macvar;
data _null_;
  call symputx("&macvar",size);
  set &inset NOBS=size;
  stop;
run;
%mend;