Character cases in SAS
In SAS you can modify a character’s case with the LOWCASE, UPCASE, and PROPCASE functions. This article answers the question of how to change a string into lowercase, uppercase, or proper case in SAS. Also, SAS has functions to check whether a string contains (or not) any lowercase / uppercase characters. Furthermore, we explain the syntax and the differences between the functions with examples.
The article discusses in total 7 functions. All these functions have 1 obligatory argument. This argument is a character variable, constant or expression.
Differences & Examples
We will use the data set below to explain the differences between these functions. The data set contains 4 rows. All rows contain (a combination of) lowercase / uppercase characters and blank spaces. Remember that SAS considers a blank space as a character. However, a blank space is neither lowercase nor uppercase.
data work.ds; input string $ 1-20; datalines; a string AN OTHER STRING a thIrd String THe FOURTH string ; run;
The LOWCASE, UPCASE & PROPCASE functions
In SAS you can use the LOWCASE function to convert a string to lowercase. Likewise, you can use the UPCASE function to convert a string in uppercase. With the UPCASE function, you can make a string proper case, i.e. the first letter of each word is in uppercase while the remainder is in lowercase.
data work.ds_lower; set work.ds; lower_case = lowcase(string); run; data work.ds_upper; set work.ds; upper_case = upcase(string); run; data work.ds_proper; set work.ds; proper_case = propcase(string); run;
With the other functions of this article (ANYLOWER, NOTLOWER, ANYUPPER, and NOTUPPER), you can check whether a string contains (or not) a lower- or uppercase character. These functions return the position of the first character that is (not) lowercase/uppercase. However, if SAS can’t find a match, it returns a 0. As said before, SAS considers blanks as characters, but without a case.
data work.ds_any_not_lower; set work.ds; any_lower = anylower(string); not_lower = notlower(string); run;
data work.ds_any_not_upper; set work.ds; any_upper = anyupper(string); not_upper = notupper(string); run;
So, to summarize, this article explained how to change a string in SAS to lowercase, uppercase, or proper case. Also, this article recommended some functions to check whether a character is (not) lower- or uppercase:
- LOWCASE: Converts all characters of a string to lowercase.
- UPCASE: Converts all characters of a string to uppercase.
- PROPCASE: Converts the string to proper case.
- ANYLOWER: Returns the position of the first lowercase character of a string.
- NOTLOWER: Returns the position of the first not lowercase character of a string.
- ANYUPPER: Returns the position of the first uppercase character of a string.
- NOTUPPER: Returns the position of the first not uppercase character of a string.
Besides the functions in this article, you can find an overview of more SAS functions and examples here.