Help using Base SAS procedures

Setting variable indicator based on matching character in another variable

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

Setting variable indicator based on matching character in another variable

I have a variable which is collection of strings.

I want to created another variable say x based on some condition like "if characters ('ABC,abc,aBc,)' found in variable y then set x="ABC_found" else "ABC_not found"


Accepted Solutions
Solution
‎05-19-2015 10:48 AM
Super User
Super User
Posts: 7,971

Re: Setting variable indicator based on matching character in another variable

Posted in reply to munitech4u

You haven't stated a question?  Perhaps you want something like:

     x=findw(upcase(YOUR_VARIABLE),"ABC");

X will then contain the first starting postion of ABC in the text.

View solution in original post


All Replies
Solution
‎05-19-2015 10:48 AM
Super User
Super User
Posts: 7,971

Re: Setting variable indicator based on matching character in another variable

Posted in reply to munitech4u

You haven't stated a question?  Perhaps you want something like:

     x=findw(upcase(YOUR_VARIABLE),"ABC");

X will then contain the first starting postion of ABC in the text.

Regular Contributor
Posts: 190

Re: Setting variable indicator based on matching character in another variable

What if i wanted to check, like if 2 chars match ("ABC" or "CDE")

Super User
Super User
Posts: 7,971

Re: Setting variable indicator based on matching character in another variable

Posted in reply to munitech4u

Well, that's a very different requirement than your previous request.  There is the soundex function.  Maybe able to do it with reg expressions also.  The way I would do it though is to first decide on how many need to match.  Then treat the string as an array and loop over it (I assume 2 chars need to match):

data want;

     set have;

     result=0;

     do i=1 to length(strip(the_string));

          if substr(the_string,i,1) in ("C","D","E") then result=result+1;

     end;

run;

Then if result>=2 you know that at least 2 characters match irrespective of position.  

Regular Contributor
Posts: 190

Re: Setting variable indicator based on matching character in another variable

I cannot use substr function, as the character can occur at any position.

Super User
Super User
Posts: 7,971

Re: Setting variable indicator based on matching character in another variable

Posted in reply to munitech4u

That is what the loop is for. 

Iteration 1 = Is character at position 1 in the list of required characters, if so add 1 to total

Iteration 2 = is character at position 2 in the list of required characters, if so add 1 to total

Continue until end of string.

If total >= 2 - i.e if more than two required characters are found in the string any position, then there is a match.

Regular Contributor
Posts: 190

Re: Setting variable indicator based on matching character in another variable

For some reason, result is always 0 coming for me. Is there anything we need to add for repeating over the number of rows?

Super User
Super User
Posts: 7,971

Re: Setting variable indicator based on matching character in another variable

Posted in reply to munitech4u

Start a new topic.  Post some test data, and what you want the output to look like.

Regular Contributor
Posts: 190

Re: Setting variable indicator based on matching character in another variable

Nevermind here is what worked for me: I used the FIND with if condition and set the new variable to 1 if its found and repeated same in else for next keyword and so on.

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 278 views
  • 0 likes
  • 2 in conversation