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

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"

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

munitech4u
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

munitech4u
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

munitech4u
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

munitech4u
Quartz | Level 8

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1232 views
  • 0 likes
  • 2 in conversation