Hi everyone,
I'm trying to do a program and I get stocked when I need to search if a variable is one of the list of other variable.
I want to do something like this
Data example;
list="1,4,5";
target="3";
if target in list then put "WORKS";
run;
but it give me an error, anyone know an alternative way to do it?
Thanks in advance!
Well, first way which came to mind:
if index(list,target)>0 then "Found";
index gives you the first position of the target, or 0 if not found:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212242.htm
There is also find(), findw() etc.
Well, first way which came to mind:
if index(list,target)>0 then "Found";
index gives you the first position of the target, or 0 if not found:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212242.htm
There is also find(), findw() etc.
The only Function that come to my mind in this moment was the IN but both are right.
Thanks a lot.
The SAS IN operator currently looks in a list of specific values.
You can use:
if Find(list, target)>0 then put "WORKS";
there are modifiers to ignore case and trailing blanks which may be needed when your target is shorter than its declared length.
Also FINDC if only ever searching for single character.
The INDEX function could also do similar but is always case sensitive.
Not a LIST an ARRAY.
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 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.