Help using'Contains' in SAS Maacro

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

Help using'Contains' in SAS Maacro

 

 

I wanted to compare if Y is available in X and if available then write output as TRUE. The below is a skeleton code for sample purpose. Can someone help. Thanks.

 

%let X = "Tier_Factor Account_Credit Advance_Quoting_Days BOLA_Pct_CovA" ;
%let Y = "Tier_Factor" ;

/**Since Y is a sub-set of X the expection is that the below code should give an output as TRUE **/

%macro test ;
data _null_ ;
%if &x contains &y %then %put 'TRUE'
%else %put 'FALSE' ;
%end ;
run ;
%mend ;

%test ;


Accepted Solutions
Solution
‎12-09-2016 04:35 PM
Super User
Posts: 10,466

Re: Help using'Contains' in SAS Maacro

Perhaps you are looking at something such as the following code:

Note the removal of quotes. Most of the time quotes as part of the value cause problems. Also with the macro %put you do not need quotes unless you want to see them.

%let X = Tier_Factor Account_Credit Advance_Quoting_Days BOLA_Pct_CovA ;
%let Y = Tier_Factor ;

/**Since Y is a sub-set of X the expection is that the below code should give an output as TRUE **/

%macro test ;

%if %sysfunc(index(&x,&y)) > 0 %then %put TRUE
%else %put FALSE ;
%mend ;
options mprint symbolgen;
%test ;

View solution in original post


All Replies
SAS Super FREQ
Posts: 8,740

Re: Help using'Contains' in SAS Maacro

[ Edited ]

Hi:
All issues of Macro coding aside, one of your issues is that CONTAINS is only a WHERE statement operator, as shown here http://www2.sas.com/proceedings/sugi31/238-31.pdf on page 6/7. Using CONTAINS with an IF statement will generate an error message.

Also, your %IF is a macro statement and you have it inside a DATA _NULL_ step. I would expect a regular IF inside a SAS program, like this:

putlog_true.png

But since I don't understand the full scope of your macro program -- if the only purpose is write TRUE or FALSE to the log, then you do not need to use the Macro %IF at all.

cynthia

Super User
Posts: 17,750

Re: Help using'Contains' in SAS Maacro

1. Don't use macro logic inside a data _null_ step

2. Contains doesn't work in a data step, except in a WHERE statement. Use FIND or INDEX instead.

 

%let X = "Tier_Factor Account_Credit Advance_Quoting_Days BOLA_Pct_CovA" ;
%let Y = "Tier_Factor" ;
/**Since Y is a sub-set of X the expection is that the below code should give an output as TRUE **/
%macro test ;
data _null_ ;
if find(&x, &y) > 0 then put 'True'; 
else put 'FALSE' ;
run ;
%mend ;
%test ;
Solution
‎12-09-2016 04:35 PM
Super User
Posts: 10,466

Re: Help using'Contains' in SAS Maacro

Perhaps you are looking at something such as the following code:

Note the removal of quotes. Most of the time quotes as part of the value cause problems. Also with the macro %put you do not need quotes unless you want to see them.

%let X = Tier_Factor Account_Credit Advance_Quoting_Days BOLA_Pct_CovA ;
%let Y = Tier_Factor ;

/**Since Y is a sub-set of X the expection is that the below code should give an output as TRUE **/

%macro test ;

%if %sysfunc(index(&x,&y)) > 0 %then %put TRUE
%else %put FALSE ;
%mend ;
options mprint symbolgen;
%test ;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 183 views
  • 4 likes
  • 4 in conversation