BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HeidiDT
Quartz | Level 8

Hi All!

I am very new to regex - I am currently busy with an introductory course. But I have a problem where the regex works when I use it in a grep command, but when I translate it into SAS it doesn't work. Please don't leaugh at the very basic example lol, I have attached the input file and here is my code (extracting all lines that contains exactly 3 digits):

 

Grep command (returns the correct 3 lines):

grep -E '^[0-9]{3}$' regex18.txt

 

SAS code (returns 0 rows):

proc import file='/sas/batch/sasdata/users/heididt/regex/ExerciseInputFiles/regex18.txt' out=example replace;
getnames=NO;datarow=1;
run;

data example;
length var2 $8.;
set example;
var2=left(put(var1,8.));
run;

data example_out;
set example;
if prxmatch('/^[0-9]{3}$/',var2);
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS only has fixed length character variables.

So your test find find 3 digits will not work since instead of three digits you have 3 digits followed by 5 spaces.

 

You should be able to just add a TRIM() function call.

if prxmatch('/^[0-9]{3}$/',trim(var2));

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

SAS only has fixed length character variables.

So your test find find 3 digits will not work since instead of three digits you have 3 digits followed by 5 spaces.

 

You should be able to just add a TRIM() function call.

if prxmatch('/^[0-9]{3}$/',trim(var2));
HeidiDT
Quartz | Level 8

EXCELLENT - thanks so much!

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 170 views
  • 0 likes
  • 2 in conversation