SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Modifying Character Values with Functions

 
Using the SUBSTR function, you can extract the first letter of the MiddleName value to create the new variable MiddleInitial.


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


You write the SUBSTR function as:
     substr(middlename,1,1)

This function specifies that a character string be extracted from the value of MiddleName. The string to be extracted begins in position one and contains one character. Then, you place this function in an assignment statement in your DATA step.

     data work.newtemp(drop=middlename);
        set hrd.newtemp;
        MiddleInitial=substr(middlename,1,1);
     run;

Notice that the MiddleName variable is dropped from the new data set. And here you see the new MiddleInitial variable.


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


Using the SUBSTR function, you can extract a substring from any character value if you know the position of the substring.



|next


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

Terms of Use & Legal Information | Privacy Statement