DATA Step, Macro, Functions and more

check if the contents of a variable is in another variable

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

check if the contents of a variable is in another variable

Hi,

I have a sas dataset with 2 variables. I would like to accomplish below. If the value of VAR2 is part of the string in VAR1 FIND needs to be 1, if not 0. How can I do this ? I've been trying out FINDC, CAT, INDEXC but none of these work.

 

Many thanks !

 

 

VAR1VAR2FIND
\TEST1\IPN R1\2012\2013\CLEARPATH-COMMANDS.TXTIPN R1\2012\DOCUMENT0
\TEST2\IPN R1\2009\DOCUMENTEN\TRIGGERS DATA_ACCES.TXTIPN R1\2009\DOCUMENT1
\TEST3\IPN R1\2006\DOCUMENTATION\VIEW SCH_RGL_GPH.SQLIPN R1\2006\DOCUMENT1

Accepted Solutions
Solution
‎02-04-2018 06:54 AM
Super User
Posts: 8,220

Re: check if the contents of a variable is in another variable

Posted in reply to Billybob73

One slight correction to @JohnHoughton's suggestion. Since you want 0s and 1s, use:

find=find(var1,var2,'t') gt 0;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Contributor
Posts: 62

Re: check if the contents of a variable is in another variable

Posted in reply to Billybob73

Hi
use find (string , subtring)

 

If the substring has trailing blanks (which is most likely) then there are two options


1. Use the t modifier i.e find(string,substring,'t');

2. Use trimn on the substring ie find(string,trimn(substring));

(Use trimn rather than trim in case the substring variable is missing, because trim would return a blank)

Solution
‎02-04-2018 06:54 AM
Super User
Posts: 8,220

Re: check if the contents of a variable is in another variable

Posted in reply to Billybob73

One slight correction to @JohnHoughton's suggestion. Since you want 0s and 1s, use:

find=find(var1,var2,'t') gt 0;

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 55

Re: check if the contents of a variable is in another variable

John and Art,

Thank you very much ! The final solution is :

 

find=find(%str(mapbestandsnaam),%str(trimn(stringyeardoc)),'t') gt 0;

 

I'm surprised this function won't try to resolve & s which are in the paths that I scan. Why is that ?

As you can see I'm using %str.

There can also be quotes and percentage signs in the paths so I would need to use %nrbquote also I guess.

 

Rgds

B

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 201 views
  • 0 likes
  • 3 in conversation