BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5


Data l;
input id$;
cards;
na_1
ra_1
na_2
na_*
gb_p
na_u
lo_*
run;

data l2;
set l;
/*if id like 'na_%' then p=1;*/
where id like 'na_%' ;
run;
proc print;
run;

i want if na_ like then to create new variable new_id=1

3 REPLIES 3
Peter_C
Rhodochrosite | Level 12

the comparison  if id=: 'na_'  (using the =: operator)  will find those ID beginning 'na_' and reject ID= 'na-...' but it is not the same as the LIKE operator

'=:' compares the shorter of the two lengths so ID='n' would also satisfy the test ID =: 'na_'

To select only ID which begin 'na_' you could test just the first 3 characters with

   substr( id, 1,3 ) = 'na_'

by the way, the LIKE operator treats '_' as a special indicator that any character must be present

     where name like 'Ja_e__'

extracts 3 rows from sashelp.class - names Jane, James and Janet.

So some extra care is needed when _ needs to be treated as _ in a LIKE clause.

Check out ESCAPE in the "Syntax of WHERE Expression" at

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000999255.htm

SASJedi
SAS Super FREQ

You can't use the LIKE operator in this context (this operator is only for use in WHERE statements) - but you CAN use a PERL regular expression and the PRXMATCH function to accomplish the same thing:

data l2;
   set l;
   if prxmatch('/na_\w*/',id) then p=1;
run;
Check out my Jedi SAS Tricks for SAS Users
Ksharp
Super User
Data l;
input id$;
cards;
na_1
ra_1
na_2
na_*
gb_p
na_u
lo_*
run;

 

data l2;
set l;
if lowcase(left(id)) eq: 'na_' then p=1;
run;

Ksharp

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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