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

I am trying to use a SAS macro to do a conditional procedure. I set a region name, and if it's in a subset then I want it to do something differently. The code is:

%macro merge_RF(region_data);		
	%if "&region_data" in ('Brazil', 'HK', 'Malaysia', 'Singapore', 'Taiwan', 'Thailand') %then %do;		* for regions with no better data, use MPT risk free index;
		proc sort data = ts_fund2; by Risk_Free_Rate_ID date; run;
		data ts_fund3;
			merge ts_fund2 (in = a) use.idx5;
			by Risk_Free_Rate_ID date;
			if a;
		run;
	%end;	
	%else %do;
		proc sort data = ts_fund2; by date; run;
		data ts_fund3;
			merge ts_fund2 (in = a) use.OECD_rf2 (where = (region = "&region_data"));			
			by date;
			if a;
		run;
	%end;
%mend merge_RF;

%merge_RF(region_input);

However, when I run it, the error message I got is

 

ERROR: Required operator not found in expression: "&region_data" in ('Brazil', 'HK', 'Malaysia',
'Singapore', 'Taiwan', 'Thailand')
ERROR: The macro MERGE_RF will stop executing.

 

What's wrong in my code?
 

Edit:

I forgot to mention this: the input region (region_input) is a macro variable defined earlier, such as

%let region = Japan;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Try

 

options minoperator;
%macro merge_RF(region_data)/mindelimiter=',';	
	%if &region_data in (Brazil, HK, Malaysia, Singapore, Taiwan, Thailand) %then %do;		* for regions with no better data, use MPT risk free index;
		proc sort data = ts_fund2; by Risk_Free_Rate_ID date; run;
		data ts_fund3;
			merge ts_fund2 (in = a) use.idx5;
			by Risk_Free_Rate_ID date;
			if a;
		run;
	%end;	
	%else %do;
		proc sort data = ts_fund2; by date; run;
		data ts_fund3;
			merge ts_fund2 (in = a) use.OECD_rf2 (where = (region = "&region_data"));			
			by date;
			if a;
		run;
	%end;
%mend merge_RF;

%merge_RF(region_input);

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You are mixing and matching macro commands with data step commands. You can't do that. When you put variables inside quotes, that works in a data step but not with macro variables. 

 

Try this

 

%if &region_data=Brazil or &region_data=HK or /*you type the rest*/ ;

 

--
Paige Miller
novinosrin
Tourmaline | Level 20

Try

 

options minoperator;
%macro merge_RF(region_data)/mindelimiter=',';	
	%if &region_data in (Brazil, HK, Malaysia, Singapore, Taiwan, Thailand) %then %do;		* for regions with no better data, use MPT risk free index;
		proc sort data = ts_fund2; by Risk_Free_Rate_ID date; run;
		data ts_fund3;
			merge ts_fund2 (in = a) use.idx5;
			by Risk_Free_Rate_ID date;
			if a;
		run;
	%end;	
	%else %do;
		proc sort data = ts_fund2; by date; run;
		data ts_fund3;
			merge ts_fund2 (in = a) use.OECD_rf2 (where = (region = "&region_data"));			
			by date;
			if a;
		run;
	%end;
%mend merge_RF;

%merge_RF(region_input);
Tom
Super User Tom
Super User

The keyword IN is not normally an operator in macro code. You need to set options to enable macro language to treat IN as having a special meaning. If your macro requires that then set the options on the macro definition and it will override any system options that the calling user might have set.

Also note that quotes are just part of the string to the macro processor. So "Brazil" will never equal 'Brazil'.

 

%macro merge_RF(region_data) / minoperator mindelimiter='|';
%local byvars;

%let byvars=date;
%if %upcase(&region_data) in %upcase(Brazil|HK|Malaysia|Singapore|Taiwan|Thailand) %then 
  %let byvars= Risk_Free_Rate_ID &byvars
;

proc sort data = ts_fund2;
  by &byvars;
run;
data ts_fund3;
  merge ts_fund2 (in = a) use.idx5;
  by &byvars;
  if a;
run;

%mend merge_RF;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 6629 views
  • 1 like
  • 4 in conversation