Home » SAS String

SAS- String

In the previous topic, we have learned the SAS variable. Now, in this topic, we are going to learn SAS String and its various operations.

Strings in SAS are those values which are enclosed in a pair of single quotes. In SAS, the string variable is declared by adding a space and the $ sign at the end of the variable declaration. There are several powerful functions to analyze and manipulate strings in SAS.

Declaring String Variable

We can declare the string variable and its value, as shown in the example below. In the code, we declared a string variable named String1.

Example:

Execute the above code in SAS studio:

SAS- String

Output:

SAS- String

As you can see in the output, “Hello” have been stored in the string variable String1.

How many string variables can be declared in one data set?

We can declare multiple numbers of string variables in one data set. Let’s see in the example below:

Execute the above code in SAS studio:

SAS- String

Output:

SAS- String

As you can see in the output, all values have been stored in the string variables, i.e., String1, String2, String3, String4, and String5.

Can we maintain the length of the string variable?

We can maintain storage capacity or the number of characters or length of the string variable by using length keyword. Once we have fixed the length, SAS will not store more characters than the fixed length. Let’s see in the example below:

Execute the above code in SAS studio:

SAS- String

Output:

SAS- String

As you can see in the output, all values correspond with a fixed length. The characters which are more than the fixed length have been removed.

Can we join multiple string variables?

We can join multiple string variables by using Joined_strings statements. Let’s see in the example below:

Execute the above code in SAS studio:

SAS- String

Output:

SAS- String

As you can see in the output, all strings have been concatenated.

Various String Functions

Following are the frequently used statements to perform string operations.

SUBSTRING Function

SUBSTRN statement is used to extract a substring by using the start and end positions.

Syntax:

Where,

  • SUBSTRN: It is a statement used to extract characters from the string.
  • String value: It is the value of the string variable.
  • p1: It is the start position of extraction.
  • p2 It is the end position of extraction.

There are two cases in extraction:

First Case: when we provide both positions i.e., start position and end position.

SAS uses the start position element to start the extraction, and end position element to recognize the total number of elements to be extracted. Let’s understand through an example:

Example:

Execute the above code in SAS studio:

SAS- String

Output:

SAS- String

As you can see in the output, extraction is started from position 1 i.e., A and it ends when total 4 elements i.e., ABCD has been extracted.

Second Case: When we provide only one position i.e., start position.

In the case of providing only the start position but not the end position, it extracts all the characters by the end of the string. Let’s understand through an example:

Execute the above code in SAS studio:

SAS- String

Output:

SAS- String

As you can see in the output, extraction is started from the position 4 i.e., D and it ends when the extraction of all the remaining elements i.e., DEFG has been completed.

TRIM

TRIMN Statement is used to remove the trailing space from a string.

Syntax:

Where,

  • TRIM: It is the statement which is used to trim the space from a string.
  • String value: It is the value of the string variable that needs to be trimmed.

Let’s understand through an example:

When we run the above code in the SAS Studio, we will get the output which shows the result of TRIM function.

SAS- String

Output:

SAS- String

As you can see in the output, the length of the string has been trimmed from 6 to 4.


Next TopicSAS-Array

You may also like