BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jawhitmire
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION
7 REPLIES 7
jawhitmire
Quartz | Level 8
%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

ballardw
Super User

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)
jawhitmire
Quartz | Level 8

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

Kurt_Bremser
Super User

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.

ballardw
Super User

@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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 1219 views
  • 4 likes
  • 3 in conversation