SAS String Functions | SAS Character Functions – 7 Mins Tutorial
Today, we will be learning about SAS strings and major SAS string functions with syntax and examples. We will also present you with an example representing a declaration of two string variables. The examples and syntaxes will help you to understand the string functions in SAS in a better way.
We hope you have gone through the previous SAS Strings Tutorial.
Keeping you updated with latest technology trends, Join DataFlair on Telegram
What is String in SAS?
Strings in SAS programming are the values that are enclosed within a pair of single quotes. String variables are declared by placing a $ sign at the end of the declaration of a variable. SAS has a vast repository of functions that can be applied to strings for analysis.
Declaring String Variables
Below is an example that shows the declaration of two string variables string 1 and string 2. As you can see a dollar sign has been placed at the end to denote that the variable is of character type. The numbers, 5 and 6 at the end of the string denote the length of the string.
data string_example; LENGTH string1 $ 6 String2 $ 5; /*String variables of length 6 and 5 */ String1 = 'Hello'; String2 = 'World';
Two columns with variable names string 1 and string 2 were created of character type. Hello, and World is the data values in the form of string inside the dataset.
Explore the concept of SAS variables in detail
SAS String Functions
SAS programming has a vast number of SAS string functions that can be applied to strings to make our analysis easier. Below we will be seeing some important and most frequently used SAS string functions.
1. SAS COMPBL Function
SAS String Functions – COMPBL Function
Purpose: Occurrences of two or more blanks is replaced with a single blank character. This is particularly useful for situations where names and addresses that have multiple blanks may have been entered.
Syntax: COMPBL (an expression that is to be compressed)
CHAR = "A D XYZ"
The function COMPBL(CHAR) will give the output “A D XYZ”
2. SAS STRIP Function
SAS String Functions – STRIP Function
Purpose: This function removes the leading and trailing spaces i.e spaces that occur before and after any character.
Syntax: STRIP( name of the character)
let CHAR = " XYZ "
i. The function STRIP( CHAR) will give the output as “ XYZ”
ii. STRIP( “ LEADING AND TRAILING” ) will give output as
“LEADING AND TRAILING”)
Must Learn – SAS User Interface
3. SAS COMPRESS Function
SAS String Functions – COMPRESS Function
Purpose: This function removes some specified characters that we wish to remove from the string instead of removing blanks.
Syntax: COMPRESS( name of the string, ‘ characters that we want to be removed’)
let CHAR = " AB C126734IJKXYZ789 "
i. The function COMPRESS( CHAR, “6789IJK”) will give the output as “ AB C1234XYZ”
ii. COMPRESS( “AB C126734IJKXYZ789” ) will give output as “ABC126734IJKXYZ789”
4. SAS LEFT Function
SAS String Functions – STRIP Function
Purpose: This function aligns the text or the string to the left instead of removing leading blanks.
Syntax: LEFT( EXPRESSION)
let CHAR = " XYZ "
i. The function LEFT( CHAR) will give the output as “XYZ ”
ii. LEFT( “ 678 ” ) will give output as “678 ”
5. SAS TRIM Function
SAS String Functions – TRIM Function
Purpose: This function is used for removing trailing blanks exclusively. It becomes useful when we want to concatenate two strings and both have trailing spaces.
Syntax: TRIM( name of the string)
let STRING1 = "XYZ " and STRING2 = " abc"
i. TRIM(STRING1) will trim trailing spaces and return “XYZ”.
ii. TRIM(” “) will display result as ” ” (length = 1)
iii. TRIM(STRING2) will return same string because there are no trailing spaces, only leading spaces ” abc”
Have you read? – Special & Built-in Data Set in SAS
6. SAS “CAT” Functions
SAS String Functions – CAT Function (CAT, CATS, CATT, and CATX)
Purpose: These functions are used to concatenate two strings (join them). The CAT function joins the two strings as it is, including spaces also.
Syntax: CAT( STRING1, STRING2, STRING3,….. STRINGN)
let A = "HELLO" , B = " HOW", C= ”YOU “ , D= ” DO “ where A, B, C, D are character variables.
i. CAT(A, B) gives “HELLO HOW”
ii. CAT(B, C, D) gives ” HOWYOU DO ”
iii. CAT(“HEY “, C) will give “HEYYOU ”
i. CATT – strips only trailing spaces.
ii. CATS – strips both leading and trailing blanks, and does not insert separators.
iii. CATX – strips both leading and trailing blanks, and inserts separators. The first argument to CATX specifies the separator.
7. SAS SUBSTR Function
SAS String Functions – SUBSTR Function
Purpose: This function extracts a part of a string.
Syntax: SUBSTR(string, start, length )
A Start is the starting position from where we want the string.
length is the number of characters to include in the substring.
If this argument omits, the SUBSTR function will return all the characters from the start position to the end of the string.
let STRING = "ABCXYZ"
i. SUBSTR(STRING,4,2 ) will return “XY”
ii. SUBSTR(STRING,4) will return “XYZ”
Do you know? – Important Structure of SAS Program
8. SAS LOWCASE, UPCASE, and PROPCASE
The SAS String Functions – LOWCASE, UPCASE, and PROPCASE Function
- The SAS LOWCASE Function – Converts the character string into lowercase character.
- SAS UPCASE Function – Converts the character string into uppercase character.
- SAS PROPCASE Function – Returns the word having uppercase in the first letter and lowercase in the rest of the letter (sentence format).
9. SAS LENGTH Function
Purpose: Determines the length of a character value( string), and does not count trailing blanks. If an argument is null, it returns a value of 1.
let CHAR = "BC "
i. LENGTH(“BC”) will give 2
ii. LENGTH(CHAR) will also give 2
iii. LENGTH(” “) will give 1 because it is null.
This was all about the SAS string functions. Hope you like our explanation.
We learned about SAS String, declaring String Variable in SAS, SAS String Function, types of String Functions in SAS Programming with their syntax and examples.
Clear with SAS string functions? Any queries, feel free to ask in the comment section.
Prepare for your SAS interview by completing SAS Interview Questions Tutorial