BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

data ex;
name='kumar(ex)' ;
output;
name='shiva' ;
output;
if name ^=:'(ex)' then rank=1;
else if name =:'(ex)' then rank=2;
run;

 

how to use like and not like operations in if statement

2 REPLIES 2
Astounding
PROC Star
The syntax is fine, but the logic is faulty. RANK will be missing because the program outputs observations before assigning a value to RANK.
ballardw
Super User

It a data step LIKE , as you intend to use it, is only available with WHERE, not if.

 

In a data step you can use other functions to identify the presence of a sequence of characters as part of the values such as INDEX or FIND. If you have more complex pattern then PRXMATCH perhaps.

The Index and Find functions return the position of found value or 0 if not found.

Notice the use of datalines to provide values and the different results for Find with and without the 'i', case insensitive, comparison.

data ex;
   input name :$15.;
   /* checks for presence of ex anywhere in name*/
   if find(name,'ex')=0 then rank=1;
   else rank=2;
   /* presence of ex case insensitive*/
   if find(name,'ex','i')=0 then rank2=1;
   else rank2=2;

datalines;
kumar(ex)
shiva
jonhEXfg
mareXbo
PATExxxx
;

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