sasmacrosproc-sql

Using and Creating SAS Macro in the same step


The macro below (the into :&count is causing issues). Although it doesn't show an error, later when I call salescount, it's clear the macro was not assigned properly "Apparent symbolic reference salescount was not resolved." Is there a way to fix the macro so I don't have to go back writing out the full proc sql entry each time I need it?

%macro counts (variable1, count, dataname);

proc sql;
select count(distinct &variable1) into :&count trimmed
from &dataname;
quit;
%mend counts;

%counts(userid, salescount, customers)


Solution

  • When a macro variable does not already exist, creating a macro variable inside of a macro will create a local macro variable. You need to declare it as global first to use it outside of the macro:

    %macro counts (variable1, count, dataname);
        %global &count;
    
        proc sql;
            select count(distinct &variable1) into :&count trimmed
            from &dataname;
        quit;
    %mend counts;