DATA Step, Macro, Functions and more

End of line anchor in regular expression does not seem to work

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 107
Accepted Solution

End of line anchor in regular expression does not seem to work

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. 


Accepted Solutions
Solution
‎01-23-2018 11:23 AM
Super Contributor
Posts: 359

Re: End of line anchor in regular expression does not seem to work

Posted in reply to tomcmacdonald
Hello,

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

View solution in original post


All Replies
Solution
‎01-23-2018 11:23 AM
Super Contributor
Posts: 359

Re: End of line anchor in regular expression does not seem to work

Posted in reply to tomcmacdonald
Hello,

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

Re: End of line anchor in regular expression does not seem to work

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? 

Respected Advisor
Posts: 4,802

Re: End of line anchor in regular expression does not seem to work

[ Edited ]
Posted in reply to tomcmacdonald

@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*$

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 183 views
  • 1 like
  • 3 in conversation