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 "®ion_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 = "®ion_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: "®ion_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;
Try
options minoperator;
%macro merge_RF(region_data)/mindelimiter=',';
%if ®ion_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 = "®ion_data"));
by date;
if a;
run;
%end;
%mend merge_RF;
%merge_RF(region_input);
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 ®ion_data=Brazil or ®ion_data=HK or /*you type the rest*/ ;
Try
options minoperator;
%macro merge_RF(region_data)/mindelimiter=',';
%if ®ion_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 = "®ion_data"));
by date;
if a;
run;
%end;
%mend merge_RF;
%merge_RF(region_input);
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(®ion_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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.