How can I remove the first character from a string using SAS? 2

How can I remove the first character from a string using SAS?

The process of removing the first character from a string in SAS involves using the SUBSTR function to extract a portion of the string starting from the second character. This function takes the string, the starting position (2), and the desired length (length of the string minus one) as parameters. The result is a new string without the first character. This can be useful for data manipulation and cleaning tasks in SAS programming.

SAS: Remove First Character from String


The easiest way to remove the first character from a string in SAS is to use the function.

You can use the following basic syntax to do so:

data new_data;
    set original_data;
    string_var = substr(string_var, 2);
run;

This syntax extracts the substring starting from the second character to the end of the string, which has the effect of removing the first character from the string.

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

Example: Remove First Character 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;
xMavs 113
xPacers 95
xCavs 120
xLakers 114
xHeat 123
xKings 119
xRaptors 105
xHawks 95
xMagic 103
xSpurs 119
;
run;

/*view dataset*/
proc printdata=my_data;

Notice that each string in the team column contains an x as the first character.

We can use the SUBSTR function to remove this first character from each string in the team column:

/*create new dataset where first character in each string of team column is removed*/
data new_data;
    set my_data;
    team = substr(team, 2);
run;

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

Notice that the first character of each string in the team column has been removed.

Note that the SUBSTR function uses the following basic syntax:

SUBSTR(Source, Position, N)

where:

  • Source: The string to analyze
  • Position: The starting position to read
  • N: The number of characters to read

By using substr(team, 2) and not specifying a value for the last argument of N, we’re able to extract the substring from the string in the team column starting from the second character to the last character.

This has the effect of removing the first character from the string.

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

Cite this article

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

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

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

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

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

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

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