BookmarkSubscribeRSS Feed
Alexxxxxxx
Pyrite | Level 9

Hello,

I am trying to convert A. S. to AS by following codes. However, the code works well by the online regex but not through SAS. Could you please give me some suggestions about this?

 

data HAVE;
input name &:$300.;
infile datalines  missover;
datalines;
BUXTON, WILLIAM A. S.
SOL, ALISSON A. S.
BUXTON, WILLIAM A. S.
BUXTON A.S. WILLIAM
;
run;

data want;
	set HAVE;
name_S33=prxchange("s/ A\. *S\.$| A\.S\.*/ AS /o",1, name);
run;

 

3 REPLIES 3
PGStats
Opal | Level 21

Try using \b to detect word boundary and don't refer to the string end ($) as the string is 300 chars long:

 

"s/\bA\.\s?S\./AS/o"

 

 

PG
Patrick
Opal | Level 21

 

 name_S33=prxchange("s/\bA\. *\bS\./AS/oi",1, name);

 

The RegEx you've posted should work as well if you add strip(name) as else there will be trailing blanks in your string and the first bit of your RegEx will never match (the $ at the end means the of string so any trailing blank will render this as a non-match).

name_S33=prxchange("s/ A\. *S\.$| A\.S\.*/ AS /o",1, strip(name));
Tom
Super User Tom
Super User

SAS stores all character variables as fixed length. So shorter values are padded with spaces. So by using the $ symbol one of your patterns will only match strings with a period in position 300.

 

But your RegEx can be simplified.

prxchange("s/ *A\. *S\. */ AS /o",1, name)

Example:

data HAVE;
  infile datalines  truncover;
  input name $300.;
datalines;
BUXTON, WILLIAM A. S.
SOL, ALISSON A. S.
BUXTON, WILLIAM A. S.
BUXTON A.S. WILLIAM
BUXTON   A.   S.   WILLIAM
;

data want;
  set HAVE;
  name_S33=prxchange("s/ *A\. *S\. */ AS /o",1, name);
  put name :$quote. '-> ' name_s33 :$quote.;
run;
"BUXTON, WILLIAM A. S." -> "BUXTON, WILLIAM AS"
"SOL, ALISSON A. S." -> "SOL, ALISSON AS"
"BUXTON, WILLIAM A. S." -> "BUXTON, WILLIAM AS"
"BUXTON A.S. WILLIAM" -> "BUXTON AS WILLIAM"
"A.S. WILLIAM BUXTON" -> " AS WILLIAM BUXTON"
"BUXTON   A.   S.   WILLIAM" -> "BUXTON AS WILLIAM"

You might want to add a LEFT() function to eliminate that leading space on my next to last example.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Update

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 922 views
  • 0 likes
  • 4 in conversation