DATA Step, Macro, Functions and more

IN with a variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 144
Accepted Solution

IN with a variable

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!


Accepted Solutions
Solution
‎03-17-2015 11:29 AM
Super User
Super User
Posts: 7,401

Re: IN with a variable

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.

View solution in original post


All Replies
Solution
‎03-17-2015 11:29 AM
Super User
Super User
Posts: 7,401

Re: IN with a variable

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.

Frequent Contributor
Posts: 144

Re: IN with a variable

The only Function that come to my mind in this moment was the IN but both are right.

Thanks a lot.

Super User
Posts: 10,500

Re: IN with a variable

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.

Respected Advisor
Posts: 3,777

Re: IN with a variable

Not a LIST an ARRAY.

Data _null_;
  
array list{3} (1,4,5);
   do target=3,4;
     
if target in list then put "NOTE: WORKS " target=;
      end;
  
run;

NOTE:
WORKS target=4
☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 193 views
  • 3 likes
  • 4 in conversation