Version 9.4
The SAS source code contains several equations and each is numbered. The macro CountPrimaryResults counts the number of observations that are Primary. It has parameters of inputdataset, outputdataset, and equation_number.
%macro CountPrimaryResults(inputdataset=,outputdataset=,equation_number=);
proc sql;
create table work.&outputdataset as
select Constituent_Name, count(Constituent_Name) as PrimaryResultsCount
from work.&inputdataset
where Result_Type in ('PRIMARY_RESULT')
group by Constituent_Name;
quit;
data work.&outputdataset;
set work.&outputdataset end=eof;
retain flag 0;
if PrimaryResultsCount le 5 then do;
Warning=Constituent_Name;
flag=1;
end;
if eof and flag=1 then call symputx('check','failed');
else call symputx('check','passed');
run;
%put ✓
%mend CountPrimaryResults;
I would like to create a global macro within CountPrimaryResults that has "passed" or "failed" with the equation number as a part of the macro variable created within symputx.
For equation 7, the call to the macro was:
%CountPrimaryResults(inputdataset=feedunitdose,outputdataset=countfeedunitdose,equation_number=7);
I want a global macro check7 with the result of "passed" or "failed".
This is what I tried:
%macro CountPrimaryResults(inputdataset=,outputdataset=,equation_number=);
proc sql;
create table work.&outputdataset as
select Constituent_Name, count(Constituent_Name) as PrimaryResultsCount
from work.&inputdataset
where Result_Type in ('PRIMARY_RESULT')
group by Constituent_Name;
quit;
data work.&outputdataset;
set work.&outputdataset end=eof;
retain flag 0;
if PrimaryResultsCount le 5 then do;
Warning=Constituent_Name;
flag=1;
end;
if eof and flag=1 then call symputx('chk&equation_number','failed');
else call symputx('chk&equation_number','passed');
run;
%put &chk&equation_number;
%mend CountPrimaryResults;
The warning message reads:
WARNING: Apparent symbolic reference CHK not resolved.
&chk7
but reference to %put &chk7 also doesn't work (echos &chk7) within or outside the macro.
Thank you,
Jane
For the indirect reference, use a double ampersand:
%put "&&chk&equation_number";
Macro triggers are not resolved inside single quotes.
%macro CountPrimaryResults(inputdataset=,outputdataset=,equation_number=);
proc sql;
create table work.&outputdataset as
select Constituent_Name, count(Constituent_Name) as PrimaryResultsCount
from work.&inputdataset
where Result_Type in ('PRIMARY_RESULT')
group by Constituent_Name;
quit;
data work.&outputdataset;
set work.&outputdataset end=eof;
retain flag 0;
if PrimaryResultsCount le 5 then do;
Warning=Constituent_Name;
flag=1;
end;
if eof and flag=1 then call symputx("chk&equation_number","failed");
else call symputx("chk&equation_number","passed");
run;
%put "&chk&equation_number";
%mend CountPrimaryResults;
Call...
%CountPrimaryResults(inputdataset=feedunitdose,outputdataset=countfeedunitdose,equation_number=7);
leads to WARNING message:
WARNING: Apparent symbolic reference CHK not resolved.
"passed"
Any way to get rid of the warning?
Thanks,
Jane
Instead of &chk&equation_number try
&&chk&equation_number
In your %put statement.
If you want quotes in the value of the macro variable use the QUOTE function in the call symputx
%macro dummy(eqno=); data _null_; call symputx("chk&eqno.",quote("failed")); run; %put &&chk&eqno.; %mend; %dummy(eqno=5)
For the indirect reference, use a double ampersand:
%put "&&chk&equation_number";
Thank you!
Who would of thought it was so simple? I guess the double ampersands is to force the reading of the first macro variable as separate from the second macro variable name -
Cheers!
Jane
The double ampersand forces two passes; in the first pass, the double ampersand is resolved to a single one, and macro variables within the string are resolved; in the second pass, the whole construct is resolved. This method can be expanded by using even more consecutive ampersands.
@Kurt_Bremser wrote:
The double ampersand forces two passes; in the first pass, the double ampersand is resolved to a single one, and macro variables within the string are resolved; in the second pass, the whole construct is resolved. This method can be expanded by using even more consecutive ampersands.
And ever more obscure code.
Yes I've done that. And now look for alternate approaches as if I don't look at the code for awhile I don't remember why &&&&This&&var&mess was a good idea.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.