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

Hi Experts,

 

Could you please help me how to remove comma (",") from a list within a character variable values 

for the following scenario ?

Var              Values 

Name         Michael, Mitta, George

 

Or you could tell me how to add a comma between words/strings in for the following scenario;

Var              Values 

Name         Michael  Mitta George

Thanks for support in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
HarrySnart
SAS Employee

Hi @inquistive , Is this list at the row level? You can use the TRANWRD function to replace the comma for a space for an empty string for example.

 

HarrySnart_0-1649175444330.png

data _null_;
have = "Michael, Mitta, George";
want = tranwrd(have,",","");
put have;
put want;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Use the TRANSTRN function:

data _null_;
string = "Michael, Mitta, George";
string = transtrn(string,", "," ");
put string= $quote.;
string = transtrn(string," ",", ");
put string= $quote.;
run;
HarrySnart
SAS Employee

Hi @inquistive , Is this list at the row level? You can use the TRANWRD function to replace the comma for a space for an empty string for example.

 

HarrySnart_0-1649175444330.png

data _null_;
have = "Michael, Mitta, George";
want = tranwrd(have,",","");
put have;
put want;
run;
ballardw
Super User

Remove a character : Compress function

data example;
   var = "Michael, Mitta, George";
   var = compress(var,',');
run;

You don't show what the "inserted" version might be. If you are actually adding characters, such as to replace a single space with comma and space you may need to create a new variable as when you add characters you run the risk of exceeding the defined length of the existing variable leading to truncation of the last word. If you only have exactly one space then perhaps this is easiest:

data example2;
   var= "Michael Mitta George";
   newvar= tranwrd(var,' ',', ');
run;

If you don't want to have comma and a space you can use Translate function to replace the single space with a single comma. Just switch the positions of the comma and space in the first example.

 

Tom
Super User Tom
Super User

How consistent are your source strings about what characters are between the "words" in the list?

If you always have comma and space then just removing the commas will work.

data want;
   set have;
   values = compress(values,',');
run;

Put if you sometimes have only the comma between the words then that would cause two words to merge.

You could replace the comma with a space

values=translate(values,' ',',');

But if you sometimes also have spaces then you might have two spaces between some pairs of words.

So perhaps you want to use COMPBL() to collapse those into one space.

values=compbl(translate(values,' ',','));

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
  • 5865 views
  • 4 likes
  • 5 in conversation