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

Hi.  

 

I am trying to deleted spaces around commas inside a singe character string:

 

Have

Mary Had , A Little , Lamb

 

Want

Mary Had,A Little,Lamb

 

 

Any help is truly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ

I would use PRXCHANGE, but I'm sure there's a different function that could do it as well.

 

data want;
var = "Mary Had, A Little,Lamb";
var_want = prxchange("s/(\s\,)|(\,\s)/,/", -1, var);
run;

Basically saying, substitute a space followed by a comma OR a comma followed by a space, replace it with a single comma. -1 tells SAS to do the replacement for the entire field.

 

Not sure if this will apply to all cases, though - especially if you have multiple spaces. Then you will need to modify the \s. 

View solution in original post

4 REPLIES 4
maguiremq
SAS Super FREQ

I would use PRXCHANGE, but I'm sure there's a different function that could do it as well.

 

data want;
var = "Mary Had, A Little,Lamb";
var_want = prxchange("s/(\s\,)|(\,\s)/,/", -1, var);
run;

Basically saying, substitute a space followed by a comma OR a comma followed by a space, replace it with a single comma. -1 tells SAS to do the replacement for the entire field.

 

Not sure if this will apply to all cases, though - especially if you have multiple spaces. Then you will need to modify the \s. 

arde
Obsidian | Level 7
Thank you!
Tom
Super User Tom
Super User

The example data works fine with just TRANWRD() function.

want = tranwrd(have,' , ',',');

If the real data has multiple adjacent spaces you can try using COMPBL() first to collapse all places with multiple spaces into a single space.

 

If the real data has some commas with only leading or only trailing spaces then you might need to work a little harder.

data have;
  input string $50.;
cards;
Mary Had , A Little , Lamb
Mary Had  ,  A Little  ,  Lamb
Mary Had, A Little ,Lamb
Mary Had  ,A Little,  Lamb
; 

data want;
  set have;
  length newstr $50;
  newstr=tranwrd(tranwrd(compbl(string),', ',','),' ,',',');
run;

Result

Obs                string                        newstr

 1     Mary Had , A Little , Lamb        Mary Had,A Little,Lamb
 2     Mary Had  ,  A Little  ,  Lamb    Mary Had,A Little,Lamb
 3     Mary Had, A Little ,Lamb          Mary Had,A Little,Lamb
 4     Mary Had  ,A Little,  Lamb        Mary Had,A Little,Lamb
arde
Obsidian | Level 7
that's good to know, thank you

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 Updates

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
  • 4 replies
  • 1299 views
  • 1 like
  • 3 in conversation