BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ram_SAS
Obsidian | Level 7

Hi,

I have a long macro variable (check) and need to scan  the presence of a word  using a macro and in a data step ? How can I do that?

 

%let check=This is Year 2017, Month 12, Day 12;

data a;
var= "Month";
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

A slight change accomplishes this:

 

if findw("&string", trim(var), ',') > 0 then flag=1;

 

This change tells the function to use commas as the only delimiters.  Note that this fails if the original macro variable contains blanks after the commas.

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Can you please elaborate the task that you want to accomplish?

Astounding
PROC Star

The safest tool is probably the FINDW function:

 

%let check=This is Year 2017, Month 12, Day 12;

data test;

var="Month";

if findw("&check", var) > 0 then flag=1;

else flag=0;

run;

 

You might want to consider whether upper vs. lower case matters:

 

if findw(upcase("&check"), upcase(var)) > 0 then flag=1;

 

 

 

Ram_SAS
Obsidian | Level 7

Thanks for your reply.  I should have posted bit more clearly. How do we flag here? Need to get the third one flagged.


%let string=MONTH 1-ABC MONTH 0,MONTH 1-ABC MONTH 2,MONTH 1-ABC MONTH 3,MONTH 1-ABC MONTH 3,MONTH 5-ABC MONTH 6;
%put &=string;

 

data test;
length var $100;
var="MONTH"; output;
var="MONTH 1-ABC MONTH 1"; output;
var="MONTH 1-ABC MONTH 2"; output;
var="MONTH 1-ABC MONTH 8"; output;
run;

data test;
set test;
if findw("&string", var) > 0 then flag=1;
else flag=0;

run;

 

Astounding
PROC Star

A slight change accomplishes this:

 

if findw("&string", trim(var), ',') > 0 then flag=1;

 

This change tells the function to use commas as the only delimiters.  Note that this fails if the original macro variable contains blanks after the commas.

Ram_SAS
Obsidian | Level 7

Excellent, Thanks.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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