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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 4 replies
  • 805 views
  • 3 likes
  • 4 in conversation