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

Hi!

 

We've just got access to SAS as a tool, and now I'm trying to translate my previous scripts from SQL. 

I'm having trouble finding a commando like "rlike" - if anyone could please help me?
I've tried to make an example her of what I'm trying to do:

 

PROC SQL;
Create table test as
Select CreatedDate, DiscountCode, MemberId, ChangedFields
from my.source
where ChangedFIelds rlike 'discountCode:[0-9][0-9][0-9][0-9][0-9]->null'
ORDER BY CreatedDate;

 

So.. How to do this in SAS EG?

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

I wasn't sure about the whitespace situation between 'discountCode:' and the digits, so this might work:

 

data have;
input changedfields $80.;
datalines;
discountCode:12345->null
discountCode:67890->null
discountCode: 87654->null
discountCode:1x345->null
something else
;

proc sql;
 create table want as select changedfields from have
  where prxmatch('/discountCode:(\s*(\d\d\d\d\d))->null/',changedfields)>0;
quit;
Become an Explorer! Join SAS Analytics Explorers to learn and complete challenges that earn rewards!

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

SAS does not provide such an operator (which looks like a LIKE with regular expressions to me).

The straightforward thing to do (IMO) is using basic comparisons with substrings and testing for numeric data:

data have;
input changedfields $80.;
datalines;
discountCode:12345->null
discountCode:67890->null
discountCode:1x345->null
something else
;

data want;
set have;
where
  index(ChangedFIelds,'discountCode:') = 1 and
  notdigit(substr(ChangedFIelds,14,5)) = 0 and
  substr(ChangedFIelds,19,6) = '->null'
;
run;

Someone who is an expert on SAS perl regular expressions may come up with a more elegant solution.

ChrisHemedinger
Community Manager

I wasn't sure about the whitespace situation between 'discountCode:' and the digits, so this might work:

 

data have;
input changedfields $80.;
datalines;
discountCode:12345->null
discountCode:67890->null
discountCode: 87654->null
discountCode:1x345->null
something else
;

proc sql;
 create table want as select changedfields from have
  where prxmatch('/discountCode:(\s*(\d\d\d\d\d))->null/',changedfields)>0;
quit;
Become an Explorer! Join SAS Analytics Explorers to learn and complete challenges that earn rewards!
Linus69
Calcite | Level 5

Thank you! That really sent me into the right direction Smiley Happy

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 2960 views
  • 3 likes
  • 3 in conversation