Reading Multiple Records Non-Sequentially

The #n Line Pointer Control

The #n specifies the absolute number of the line where you want to move the input pointer. The #n pointer control can read records in any order. Therefore, it must be specified before the instructions for reading values in a specific record.
The INPUT statement below first reads the values for Department and JobCode in the second record, and then reads the values for Lname and Fname in the first record. Finally, it reads the value for Salary in the third record.
input #2 Department $ 1-12 JobCode $ 15-19 
      #1 Lname $ Fname $ 
      #3 Salary comma10.;
Figure 20.19 The Records of the First Observation
Raw data file with the records for the first observation highlighted.

Using the #n Line Pointer Control

The raw data file Patdata contains information about the patients of a small group of general surgeons. The first three records contain a patient's name, address, city, state, and ZIP code. The fourth record contains the patient's ID number followed by the name of the primary physician.
Figure 20.20 Observation Records in a Raw Data File
Raw data file with the records numbered for the first observation.
Suppose you want to read each patient's information in the following order:
  1. ID number (ID)
  2. first name (Fname)
  3. last name (Lname)
  4. address (Address)
  5. city (City)
  6. state (State)
  7. zip (Zip)
  8. doctor (Doctor)
  • To read the values for ID in the fourth record, specify #4 before naming the variable and defining its attributes.
    data perm.patients; 
       infile patdata; 
       input #4 ID $5.
    Figure 20.21 Specifying the ID Value in the Fourth Record of an Observation
    Observation with the ID value of the fourth record highlighted.
  • To read the values for Fname and Lname in the first record, specify #1 before naming the variables and defining their attributes.
    data perm.patients; 
       infile patdata; 
       input #4 ID $5. 
             #1 Fname $ Lname $
    Figure 20.22 Specifying the First Record of an Observation
    Observation with the first record highlighted.
  • Use the #n line pointer control to move the input pointer to the second record and read the value for Address.
    data perm.patients; 
       infile patdata; 
       input #4 ID $5. 
             #1 Fname $ Lname $ 
             #2 Address $23.
    Figure 20.23 Specifying the Second Record of an Observation
    Observation with the second record highlighted.
  • Now move the input pointer to the third record and read the values for City, State, and Zip, in that order. In this raw data file, the values for City contain eight characters or fewer and do not contain embedded blanks. Therefore, you can use standard list input to read these values.
    data perm.patients; 
       infile patdata; 
       input #4 ID $5. 
             #1 Fname $ Lname $  
             #2 Address $23. 
             #3 City $ State $ Zip $
    Figure 20.24 Specifying the Third Record of an Observation
    Observation with the third record highlighted.
  • Now you need to move the input pointer down to the fourth record to read the values for Doctor, which begin in column 7. Be sure to add a semicolon at the end of the INPUT statement. A RUN statement completes the program.
    data perm.patients; 
       infile patdata; 
       input #4 ID $5. 
             #1 Fname $ Lname $  
             #2 Address $23. 
             #3 City $ State $ Zip $ 
             #4 @7 Doctor $6.; 
    run;
    Figure 20.25 Specifying the Doctor Value in the Fourth Record of an Observation
    Observation with the Doctor value of the fourth record highlighted.

Execution of the DATA Step

The #n pointer controls in the program below cause four records to be read for each execution of the DATA step.
data perm.patients; 
   infile patdata; 
   input #4 ID $5. 
         #1 Fname $ Lname $  
         #2 Address $23. 
         #3 City $ State $ Zip $ 
         #4 @7 Doctor $6.; 
run;
The first time the DATA step executes, the first four records are read, and an observation is written to the data set.
Figure 20.26 Raw Data File with the First Four Records Highlighted
Raw data file with the first four records highlighted.
During the second iteration, the next four records are read, and the second observation is written to the data set, and so on.
Figure 20.27 Raw Data File with the Next Four Records Highlighted
Raw data file with the next four records highlighted.
The PROC PRINT output of the data set shows how information that was spread over several records has been condensed into one observation.
proc print data=perm.patients noobs; 
run;
Figure 20.28 PROC PRINT Output of Data Set Perm.Patients
PROC PRINT output of data set Perm.Patients.
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.71.106