SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Modifying Character Values with Functions

 
There is a second use for the SUBSTR function. This function can also be used to replace the contents of a character variable. For example, suppose the local phone exchange 622 was replaced by the exchange 433. You need to update the character variable Phone in Hrd.Temp to reflect this change.


SAS Data Set Hrd.Temp
City State Zip Phone BeginDate EndDate PayRate Days Hours
CARY NC 27513 6224549 14567 14621 10 11 88
CARY NC 27513 6223251 14524 14565 8 25 200
CHAPEL HILL NC 27514 9974749 14570 14608 40 26 208
RALEIGH NC 27612 6970450 14516 14527 15 10 80


You can complete this modification by using the SUBSTR function. The syntax of the SUBSTR function, when used to replace a variable's values, is identical to the syntax for extracting a substring.
SUBSTR(argument,position<,n>)

However, in this case,

  • the first argument specifies the character variable whose values are to be modified.

  • the second argument specifies the position at which the replacement is to begin.

  • the third argument specifies the number of characters to replace. If n is omitted, the remainder of the value is replaced.


Positioning the SUBSTR Function

SAS software uses the SUBSTR function to extract a substring or modify a variable's values based on the position of the function in the assignment statement.

When the function appears on the right side of an assignment statement, the function returns the requested string.

     MiddleInitial=substr(middlename,1,1);

But if you place the SUBSTR function on the left side of an assignment statement, the function is used to modify variable values.

     substr(region,1,3)='NNW';

When the SUBSTR function modifies variable values, the right side of the assignment statement must specify the value to place into the variable. For example, to replace the fourth and fifth characters of a variable named Test with the value 92, you write the following assignment statement:

     substr(test,4,2)='92';

     Test           Test

     S7381K2        S7392K2
     S7381K7        S7392K7

 
|next


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

Terms of Use & Legal Information | Privacy Statement