DATA Step, Macro, Functions and more

Substr of a macro

Reply
Frequent Contributor
Posts: 125

Substr of a macro

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

PROC Star
Posts: 326

Re: Substr of a macro

[ Edited ]
Posted in reply to chennupriya
%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.

Super User
Posts: 5,516

Re: Substr of a macro

Posted in reply to chennupriya

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;

Occasional Contributor
Posts: 9

Re: Substr of a macro

Posted in reply to chennupriya

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);
Super User
Posts: 7,854

Re: Substr of a macro

Posted in reply to chennupriya

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 128 views
  • 0 likes
  • 5 in conversation