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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 423 views
  • 1 like
  • 3 in conversation