How can I remove commas from a string using the SAS programming language? 2

How can I remove commas from a string using the SAS programming language?

The SAS programming language provides a straightforward method for removing commas from a string. This can be achieved by using the built-in function “compress” and specifying the “comma” character as the delimiter. The function will then remove all instances of commas from the string and return the result. This allows users to easily manipulate and clean up data in their SAS programs without the need for additional coding or external tools. By utilizing this function, SAS users can efficiently remove commas from strings and streamline their data processing tasks.

SAS: Remove Commas from String


The easiest way to remove commas from a string in SAS is to use the TRANSLATE function, which converts every occurrence of one character to another character.

You can use the following basic syntax to do so:

data new_data;
    set original_data;
    string_var = compress(translate(string_var,"",','));
run;

This particular example removes every comma from each string in the string_var variable in a dataset.

The following example shows how to use this syntax in practice.

Example: Remove Commas from String in SAS

Suppose we have the following dataset in SAS that contains information about various basketball teams:

/*create dataset*/
data my_data;
    input team $ points;
    datalines;
,Mavs, 113
Pacers 95
,Ca,vs 120
Lakers 114
Heat 123
King,s 119
Raptors 105
,Hawks 95
Ma,gic 103
Spu,,rs 119
;
run;

/*view dataset*/
proc printdata=my_data;

Notice that several of the strings in the team column contain commas in various places.

We can use the following syntax to remove all commas from the strings in the team column:

/*create new dataset where commas are removed from each string in team column*/
data new_data;
    set my_data;
    team = compress(translate(team,"",','));
run;

/*view new dataset*/
proc printdata=new_data;

Notice that the commas have been removed from each string in the team column.

Here is what this code actually did:

  • The TRANSLATE function replaced each comma with an empty space.
  • Then, the COMPRESS function removed the empty spaces from each string.

Note: You can find the complete documentation for the SAS TRANSLATE function .

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can I remove commas from a string using the SAS programming language?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-commas-from-a-string-using-the-sas-programming-language/

stats writer. "How can I remove commas from a string using the SAS programming language?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-commas-from-a-string-using-the-sas-programming-language/.

stats writer. "How can I remove commas from a string using the SAS programming language?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-commas-from-a-string-using-the-sas-programming-language/.

stats writer (2024) 'How can I remove commas from a string using the SAS programming language?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-commas-from-a-string-using-the-sas-programming-language/.

[1] stats writer, "How can I remove commas from a string using the SAS programming language?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I remove commas from a string using the SAS programming language?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top