I have a character variable that looks like this:
Var1
Order1
order2 OR order 3
Something else
Order3 or Order5
Text3
Order4 Or Order8
How do I select just the observations that contain 'or' in the middle of the text string, which could between two, three, or four words? Moreover, is there a way for it not to be case sensitive? I tried proc sql:
proc sql;
select * from data
where var1 like '%OR%' or var1 like '%or%'
or var1 like '%Or%;
quit;
But that includes records which have the word 'Order', which I don't want. I thought maybe SAS would recognize the Or if I put a space around it:
proc sql; select * from data where var1 like '% OR %' or var1 like '% or %' or var1 like '% Or %'; quit;
But it doesn't. I really don't want to write the text out in full, because it's very long. I appreciate any help.
Try regular expression matching:
where prxmatch("/\w\s+or\s+\w/i", var1)
Note : The "i" makes the match case insensitive.
data have;
input Var1 $30.;
cards;
Order1
order2 OR order 3
Something else
Order3 or Order5
Text3
Order4 Or Order8
;
data want;
set have;
if upcase(scan(var1,int(median(1,countw(var1,' '))),' '))='OR' then output;
run;
proc print noobs;run;
Var1 |
---|
order2 OR order 3 |
Order3 or Order5 |
Order4 Or Order8 |
data want;
set have;
want=upcase(scan(var1,int(median(1,countw(var1,' '))),' '));
if want='OR';
run;
proc print noobs;run;
Var1 | want |
---|---|
order2 OR order 3 | OR |
Order3 or Order5 | OR |
Order4 Or Order8 | OR |
Try regular expression matching:
where prxmatch("/\w\s+or\s+\w/i", var1)
Note : The "i" makes the match case insensitive.
Thanks! The find function with upcase didn't work for some reason. It only selected observations with lower case 'or'.
Use the find function, works in data step or sql:
data want;
set have;
if find(upcase(var),' OR '); /* OR has a blank before and after in quotes */
run;
Why didn't the version with the spaces work?
Anyway use INDEXW() or FINDW() if you only want to match full words.
Use UPCASE() function or the i modifier of the FINDW() function if you want to match OR, or, Or or oR.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.