BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
arodriguez
Lapis Lazuli | Level 10

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!

1 ACCEPTED SOLUTION

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

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

arodriguez
Lapis Lazuli | Level 10

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

Thanks a lot.

ballardw
Super User

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.

data_null__
Jade | Level 19

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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