BookmarkSubscribeRSS Feed
chennupriya
Quartz | Level 8

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

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12
%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.

Astounding
PROC Star

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;

bstarr
Quartz | Level 8

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);
Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 4 replies
  • 808 views
  • 0 likes
  • 5 in conversation