I need to call two macro variables which are created within another macro. However, I got the following warning message saying Apparent symbolic reference not resovled.
What I am trying to do is to create a macro variable for the total count of the item and put the number of count in the log.
%MACRO C(VAR,COUNT_VAR,FILTER);
PROC SQL ;
SELECT COUNT(&VAR) INTO: &COUNT_VAR
FROM table2
WHERE &FILTER;
QUIT;
%MEND;
%C(Unsuccessful_item, COUNT_U,Unsuccessful_item EQ "Yes");
%C(Late_item,COUNT_L,Late_item EQ "Yes");
%PUT &COUNT_U &COUNT_L;
WARNING: Apparent symbolic reference COUNT_U not resolved.
WARNING: Apparent symbolic reference COUNT_L not resolved.
If I do this in two separate steps, I am able to run through. However, in real work situation, I need to do this in macro, can anyone advice. Thanks.
PROC SQL ;
SELECT COUNT(Unsuccessful_item) INTO: COUNT_U
FROM table2
WHERE Unsuccessful_item EQ "Yes";
QUIT;
PROC SQL ;
SELECT COUNT(Late_item) INTO: COUNT_L
FROM table2
WHERE Late_item EQ "Yes";
QUIT;
%PUT &COUNT_U &COUNT_L;
38 %PUT &COUNT_U &COUNT_L;
SYMBOLGEN: Macro variable COUNT_U resolves to 13
SYMBOLGEN: Macro variable COUNT_L resolves to 25
13 25
%MACRO C(VAR,COUNT_VAR,FILTER);
PROC SQL ;
SELECT COUNT(&VAR) INTO: &COUNT_VAR
FROM table2
WHERE &FILTER;
QUIT;
%MEND;
%C(Unsuccessful_item, COUNT_U,Unsuccessful_item EQ "Yes");
%C(Late_item,COUNT_L,Late_item EQ "Yes");
%PUT &COUNT_U &COUNT_L;
WARNING: Apparent symbolic reference COUNT_U not resolved.
WARNING: Apparent symbolic reference COUNT_L not resolved.
Macro scoping. Since &COUNT_U is created inside the macro %C, it has value inside the macro, but does not exist outside %C, and so your %PUT is referring to a macro variable that does not exist. You can either make &COUNT_U and &COUNT_L global using the %global command, or use the %PUT inside the macro.
Example:
%MACRO C(VAR,COUNT_VAR,FILTER);
%global &count_var;
/* The rest of your macro goes here */
%mend;
Without details of how you expect to use the variables there aren't a lot of options.
The basic approach if you need to use a macro variable created out of the current scope is often to make the macro variable a global value. That would be done with the %global statement :
%global sometext;
would create a macro variable sometext that is available everywhere in the current session.
It will be up to you to make sure that you don't reuse macro variable names.
This is not a trivial subject. You will want to look up scope of macro variables in documentation.
%MACRO C(VAR,COUNT_VAR,FILTER);
PROC SQL ;
SELECT COUNT(&VAR) INTO: &COUNT_VAR
FROM table2
WHERE &FILTER;
QUIT;
%MEND;
%C(Unsuccessful_item, COUNT_U,Unsuccessful_item EQ "Yes");
%C(Late_item,COUNT_L,Late_item EQ "Yes");
%PUT &COUNT_U &COUNT_L;
WARNING: Apparent symbolic reference COUNT_U not resolved.
WARNING: Apparent symbolic reference COUNT_L not resolved.
Macro scoping. Since &COUNT_U is created inside the macro %C, it has value inside the macro, but does not exist outside %C, and so your %PUT is referring to a macro variable that does not exist. You can either make &COUNT_U and &COUNT_L global using the %global command, or use the %PUT inside the macro.
Example:
%MACRO C(VAR,COUNT_VAR,FILTER);
%global &count_var;
/* The rest of your macro goes here */
%mend;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.