Master SAS in 30 days!

A jargon-free, easy-to-learn SAS base course that is tailor-made for students with no prior knowledge of SAS.

Lesson 6: Common SAS Functions

This lesson will explain some of the common functions in SAS.

1. SUBSTR: Extract a portion of the text variable 

SUBSTR is normally used to break up a long string of text into different pieces of information.

Example

This dataset contains some of the subject codes used in a University. We are going to break them into subject (first 3 letters) and subject number (last 3 numbers).

DATA Class2;

Set Class;

Subject = Substr( Code, 1, 3);
Subject_no = Substr( Code, 4, 3);

RUN; 

DEFINITION
The Substr function contains 3 arguments:

SUBSTR ( Variable, Start, Length )

Variable: The variable where the function will be applied on
Start: The starting position to begin reading the variable. 
Length: Number of character to read


As you can see in our example, ‘Subject’ extracts the portion of the ‘Code’ variable from the 1st position to the 3rd position; while ‘Subject_no’ extracts the portion from the 4th position to the 6th position.

DONE! You have learned the SUBSTR function in SAS!  



Note: 
  1. If you want to read the variable till the end of the text such as what we did with the Subject_no variable, you can drop the last argument:

    Subject_no = Substr( Code, 4);
    This function will return the same result. 

  2. SUBSTR function can also be used in combination with the INDEX function. We will explain this shortly in later section of this lesson.

Master SAS in 30 Days

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
iconmail

Get latest articles from SASCrunch

SAS Base Certification Exam Prep Course

Two Certificate Prep Courses and 300+ Practice Exercises