BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Angel_Saenz
Quartz | Level 8

How can I evaluate a variable to create another if the if function does not allow the like operator?

 

For example is easy if var is numeric, like:

IF VAR1 = 0 THEN VAR2 = 'A';

 

But if I need to evaluate something inside string of characters how can it be?

some like:

IF VAR1 LIKE "%A%" THEN VAR2 = "CONTAINS LETTER A"

 

What operator can I replace to LIKE?

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Either the INDEX or FINDC functions.

 

Art, CEO, AnalystFinder.com

 

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

Either the INDEX or FINDC functions.

 

Art, CEO, AnalystFinder.com

 

Angel_Saenz
Quartz | Level 8

thank you Art, and do you know how can I do it like a evaluate a list?

 

I know this does not work but is only an example:

IF VAR1 IN ('%A%','%B%','%C%') THEN VAR2  = 'CONTAINS LETTERS A B or C'

 

If I use this works:

IF INDEX(VAR1,'A') THEN VAR2 = 'CONTAINS LETTERS A B or C'

IF INDEX(VAR1,'B') THEN VAR2 = 'CONTAINS LETTERS A B or C'

IF INDEX(VAR1,'C') THEN VAR2 = 'CONTAINS LETTERS A B or C'

But is it possible to simplify it?

 

I simplified like this:

IF INDEX(VAR1,'A') or INDEX(VAR1,'B') or INDEX(VAR1,'C') THEN VAR2 = 'CONTAINS LETTERS A B or C'

but is there another way tah you know more shorten? some like:

IF INDEX(VAR1,'A','B','C') THEN VAR2 = 'CONTAINS LETTERS A B or C'

 

Thanks

Jagadishkatam
Amethyst | Level 16

Please try

 

if prxmatch('m/[abc]/i',VAR1) then VAR2="CONTAINS LETTER A or B or C";
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

alternatively we could try the perl regular expression like below

 

if prxmatch('m/[a]/i',VAR1) then VAR2="CONTAINS LETTER A";
Thanks,
Jag
art297
Opal | Level 21

the findc function allows you to both find if a string contains one or more of the variables in a list, as well as ignore case (if desired). e.g.:

data want;
informat string $23.;
input string &;
if findc(string, 'abc', "i") then
want='Contains A, B or C';
datalines;
Microsoft Office Visio
Microsoft Office Word
Adobe Photoshop
Adobe PhotoshopPro
Internet Explorer
;

Art, CEO, AnalystFinder.com 

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
  • 5 replies
  • 808 views
  • 2 likes
  • 3 in conversation