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
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
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;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.