SAS Enterprise Guide

Desktop productivity for business analysts and programmers
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;
Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!

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;
Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!
Linus69
Calcite | Level 5

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

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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