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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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