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"
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.
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.
What if i wanted to check, like if 2 chars match ("ABC" or "CDE")
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.
I cannot use substr function, as the character can occur at any position.
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.
For some reason, result is always 0 coming for me. Is there anything we need to add for repeating over the number of rows?
Start a new topic. Post some test data, and what you want the output to look like.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.