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.
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.
data _null_;
have = "Michael, Mitta, George";
want = tranwrd(have,",","");
put have;
put want;
run;
Use the TRANSTRN function:
data _null_;
string = "Michael, Mitta, George";
string = transtrn(string,", "," ");
put string= $quote.;
string = transtrn(string," ",", ");
put string= $quote.;
run;
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.
data _null_;
have = "Michael, Mitta, George";
want = tranwrd(have,",","");
put have;
put want;
run;
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.
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,' ',','));
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.