SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Modifying Character Values with Functions


SUBSTR Function

The SUBSTR function can be used to

  • extract a portion of a character value
  • replace the contents of a character value.

Let's begin with the task of extracting a portion of a value. In the data set Hrd.Newtemp, the names of temporary employees are stored in three name variables, LastName, FirstName, and MiddleName.


SAS Data Set Hrd.Newtemp
Agency ID LastName FirstName MiddleName
Administrative Support, Inc. F274 CICHOCK ELIZABETH MARIE
Administrative Support, Inc. F101 BENINCASA HANNAH LEE
OD Consulting, Inc. F054 SHERE BRIAN THOMAS
New Time Temps Agency F077 HODNOFF RICHARD LEE


However, you decide to modify the data set to store only the middle initial instead of the full middle name. To do so, you must extract the first letter of the middle name values and assign these values to the new variable MiddleInitial.


SAS Data Set Work.Newtemp
Agency ID LastName FirstName MiddleInitial
Administrative Support, Inc. F274 CICHOCK ELIZABETH M
Administrative Support, Inc. F101 BENINCASA HANNAH L
OD Consulting, Inc. F054 SHERE BRIAN T
New Time Temps Agency F077 HODNOFF RICHARD L


The SUBSTR function enables you to extract any number of characters from a character string, starting at a specified position in the string.


General form, SUBSTR function:
SUBSTR(argument,position<,n>)

where

  • argument specifies the character variable or expression to scan.

  • position is the character position to start from.

  • n specifies the number of characters to extract. If n is omitted, all remaining characters are included in the substring.



|next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement