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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 920 views
  • 0 likes
  • 4 in conversation