BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Caetreviop543
Obsidian | Level 7

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Try regular expression matching:

 

where prxmatch("/\w\s+or\s+\w/i", var1)

Note : The "i" makes the match case insensitive.

PG

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20


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

 

 

 

novinosrin
Tourmaline | Level 20
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
LeonidBatkhan
Lapis Lazuli | Level 10

I believe you just need

 

where find(upcase(var1),' OR ')

 

PGStats
Opal | Level 21

Try regular expression matching:

 

where prxmatch("/\w\s+or\s+\w/i", var1)

Note : The "i" makes the match case insensitive.

PG
Caetreviop543
Obsidian | Level 7

Thanks! The find function with upcase didn't work for some reason. It only selected observations with lower case 'or'.

DavePrinsloo
Pyrite | Level 9

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;
Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 3115 views
  • 2 likes
  • 6 in conversation