DATA Step, Macro, Functions and more

Reg: Like

Reply
Regular Contributor
Posts: 229

Reg: Like


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

Valued Guide
Posts: 2,175

Reg: Like

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

SAS Employee
Posts: 104

Reg: Like

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;
Super User
Posts: 9,681

Reg: Like

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

Ask a Question
Discussion stats
  • 3 replies
  • 142 views
  • 0 likes
  • 4 in conversation