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

I don't think the functionality of the "$" symbol in regular expressions works in SAS.  The code I'm using:

 

data foo;
	input a :$1024.;
	datalines;
foobar
barfoo
food
westchester
freebeer
;
run;

proc sql;
	create table test as 
	select *
	from foo
	where prxmatch('m/^f.*r$/i', a);
quit;

I expect test to return "foobar" and "freebeer" however it returns an empty set. 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12
Hello,

This is because of trailing spaces :
prxmatch('m/^f.*r$/i', strip(a))

View solution in original post

3 REPLIES 3
gamotte
Rhodochrosite | Level 12
Hello,

This is because of trailing spaces :
prxmatch('m/^f.*r$/i', strip(a))
tomcmacdonald
Quartz | Level 8

OK, that seems to work.  I'm a bit confused about the syntax here.  I thought adding the ":" made the data type like a vary character with a maximum of 1024? 

 

Also when I use this code:

 

proc sql;
	create table test as 
	select length(a)
	from foo;
quit;

I don't see length of any additional whitespace added. 

 

When using this code:

 

proc sql;
	create table test as 
	select put(a, $hex16.)
	from foo;
quit;

I see the ASCII symbol for a space.  I take it SAS doesn't count white space in the length calculation? 

Patrick
Opal | Level 21

@tomcmacdonald

As documented the SAS function LENGTH() returns the length of a string up to the last non-blank character. In SAS Foundation version 9.x all character variables are of type CHAR. VARCHAR doesn't exist. That's why you've got most of the time trailing blanks.

 

For RegEx: Either TRIM() or STRIP() the variable or then add the potential trailing blanks to your RegEx using \s*$

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2584 views
  • 2 likes
  • 3 in conversation