Hi I have
p_taxd_no as
Taxd_001
Taxd_002
Taxd_003
Taxd_011
Taxd_024
Taxd_012
%macro Test1(p_taxd_no);
options mprint mlogic symbolgen;
%if %sysfunc(qsubstr(&p_taxd_no,7,2)) = 01 or 02 or 03 %then %do;
%let ins = CHASE;
%end;
%put &ins;
%if %sysfunc(qsubstr(&p_taxd_no,7,2)) = 11 or 12 or 24 %then %do;
%let ins = DISCOVER;
%end;
%put &ins;
%mend Test1;
%test1(Taxd_002);
But here both conditions are becoming true . So can u please let me know how to resolve so that only INS = CHASE
Thanks
%if %sysfunc(qsubstr(&p_taxd_no,7,2)) = 01 or 02 or 03 %then %do;
%let ins = CHASE;
%end;
%put &ins;
%else %do;
%let ins = DISCOVER;
%end;
%put &ins;
instead of using if then twice use else for second condition.
Overcomplicating the code makes it harder for you to see the real problem: 02 is a separate condition (which is always true), 03 is another separate condition (which is also always true). Both of those are unrelated to the comparison to 01. Since you turned on MLOGIC, you should see messages about that in the log.
A suggested approach:
%let ins = "%substr(&p_taxd_no, 7, 2)";
%if &ins="01" or &ins="02" or &ins="03" then %let ins=CHASE;
%else if &ins="11" or &ins="12" or &ins="24" then %let ins=DISCOVER;
%put &ins;
You'll need to use the IN operator, and in this case you'll also need the MINOPERATOR option to make it work with macro variables. In your current code, SAS is effectively testing 02 = 02 or that 12=12. Since these are always true, that's why both conditions are becoming true.
Try:
%macro Test1(p_taxd_no);
options mprint mlogic symbolgen minoperator;
%if %sysfunc(qsubstr(&p_taxd_no,7,2)) in ( 01 02 03) %then %do;
%let ins = CHASE;
%end;
%put &ins;
%if %sysfunc(qsubstr(&p_taxd_no,7,2)) in ( 11 12 24) %then %do;
%let ins = DISCOVER;
%end;
%put &ins;
%mend Test1;
%test1(Taxd_002);
This condition:
%if %sysfunc(qsubstr(&p_taxd_no,7,2)) = 01 or 02 or 03 %then %do;
is semantically and syntactically equivalent to this condition:
%if (%sysfunc(qsubstr(&p_taxd_no,7,2)) = 01) or (02) or (03) %then %do;
A numeric, non-zero/non-missing value is always considered as a boolean true.
What you want is
%if
%sysfunc(qsubstr(&p_taxd_no,7,2)) = 01 or
%sysfunc(qsubstr(&p_taxd_no,7,2)) = 02 or
%sysfunc(qsubstr(&p_taxd_no,7,2)) = 03
%then %do;
Alternatively, activate the minoperator system option and use IN as @bstarr suggested.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.