SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Modifying Character Values with Functions


SCAN Function Syntax

Now that you are familiar with how the SCAN function works, let's examine the syntax of the function.


General form, SCAN function:
SCAN(argument,n<,delimiters>)

where

  • argument specifies the character variable or expression to scan
  • n specifies which word to read
  • delimiters are special characters that must be enclosed in single quotation marks (' ').


Use the SCAN function to create your new name variables for Hrd.Temp. To begin, first examine the values of the existing Name variable to determine what characters separate the names in the values. Notice that blanks and commas appear between the names and that the employee's last name appears first, then the first name, and then the middle name.

SAS Data Set Hrd.Temp
Agency ID Name
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


To create the LastName variable to store the employee's last name, you write an assignment statement that contains the following SCAN function:
     LastName=scan(name,1,' ,');

Note that a blank and a comma are specified as delimiters. You can also write the function without listing delimiters because the blank and comma are default delimiters.

     LastName=scan(name,1);

The complete DATA step that is needed to create LastName, FirstName, and MiddleName appears below. Notice that the original Name variable is dropped from the new data set.

     data hrd.newtemp(drop=name);
        set hrd.temp;
        LastName=scan(name,1);
        FirstName=scan(name,2);
        MiddleName=scan(name,3);
     run;


Specifying Variable Length

Note that the SCAN function assigns a length of 200 to each target variable. (Remember, a target variable is the variable that receives the result of the function.) So, if you submit the DATA step above, the LastName, FirstName, and MiddleName variables are each assigned a length of 200. This length is longer than necessary for these variables.

To save storage space, add a LENGTH statement to your DATA step to set an appropriate length for all three variables. Because SAS software sets the length of a new character variable the first time it is encountered in the DATA step, be sure to place the LENGTH statement before the assignment statements that contain the SCAN function.

     data hrd.newtemp(drop=name);
        set hrd.temp;
        length lastname firstname middlename $ 10;
        LastName=scan(name,1);
        FirstName=scan(name,2);
        MiddleName=scan(name,3);
     run;

|next


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

Terms of Use & Legal Information | Privacy Statement