Reading Multiple Records Sequentially

The Forward Slash (/) Line Pointer Control

Use the forward slash (/) line pointer control to read multiple records sequentially. The / advances the input pointer to the next record. The / line pointer control moves the input pointer forward only. Note that it must be specified after the instructions for reading the values in the current record.
The single INPUT statement below reads the values for Lname and Fname in the first record, followed by the values for Department and JobCode in the second record. Then the value for Salary is read in the third record.
input Lname $ 1-8 Fname $ 10-15 / 
      Department $ 1-12 JobCode $ 15-19 /  
      Salary comma10.;
Figure 20.2 Multiple Records Comprising Each Observation
Raw data file showing each observation spread out over several records.

Using the / Line Pointer Control

The raw data file Memdata contains the mailing list of a professional organization. Here are the steps for combining the information for each member into a single observation.
  1. As you write the instructions to read the values for Fname and Lname, notice that not all of the values for Lname begin in the same column. Use standard list input to read these values.
     data perm.members;    
        infile memdata truncover; 
        input Fname $ Lname $
    Tip
    Use the TRUNCOVER option in your INFILE statement so that SAS can read data for the variables until it reaches the end of the data line. Otherwise, your data will not be produced with the correct variables in the correct columns.
    Figure 20.3 Raw Data File Memdata
    Raw data file Memdata showing each observation spread out over several records.
  2. Now read the values for Address from the second record. The / line pointer control advances the input pointer to the next record. At this point the INPUT statement is incomplete, so you should not place a semicolon after the line pointer control.
    data perm.members; 
       infile memdata truncover; 
       input Fname $ Lname $ / 
    Figure 20.4 Raw Data File Memdata
    Raw data file Memdata showing each observation spread out over several records.
  3. You can use column input to read the values in the next record as one variable that is named Address. Then add a line pointer control to move the input pointer to the next record.
    data perm.members; 
       infile memdata truncover; 
       input Fname $ Lname $ / 
             Address $ 1-20 /
    Figure 20.5 Line Pointer Control Advanced One Record
    Raw data file Memdata showing line pointer control advanced one record.
  4. As you write the statements to read the values for City, notice that some of the values are longer than eight characters and contain embedded blanks. Also note that each value is followed by two consecutive blanks. To read these values, you should use modified list input with the ampersand (&) modifier.
    The values for State and the values for Zip do not begin in the same column. Therefore, you should use list input to read these values.
    data perm.members;  
       infile memdata truncover; 
       input Fname $ Lname $ / 
             Address $ 1-20 / 
             City :& $10. State $ Zip $; 
    run;
    Figure 20.6 Line Pointer Control Advanced Another Record
    Raw data file Memdata showing line pointer control advanced another record.

Sequential Processing of Multiple Records in the DATA Step

The values in the first record are read, and the / line pointer control moves the input pointer to the second record.
Figure 20.7 Reading the First Record of the First Observation
SAS code example, raw data file, and Program Data Vector showing line control pointer on the first record of the first observation.
The values for Address are read, and the second / line pointer control advances the input pointer to the third record.
Figure 20.8 Reading the Second Record of the First Observation
SAS code example, raw data file, and Program Data Vector showing line control pointer on the second record of the first observation.
The values for City, State, and Zip are read. The INPUT statement is complete.
Figure 20.9 Reading the Third Record of the First Observation
SAS code example, raw data file, and Program Data Vector showing line control pointer on the third record of the first observation.
The values in the program data vector are written to the data set as the first observation.
Figure 20.10 The Program Data Vector and the SAS Data Set Perm.Members
The program data vector and the first observation written to SAS data set Perm.Members.
Control returns to the top of the DATA step. The variable values are reinitialized to missing.
Figure 20.11 Reinitializing the Variable Values to Missing
SAS code example, raw data file, and Program Data Vector showing the variable values reinitialized to missing.
During the second iteration, values for Fname and Lname are read beginning in column 1 of the fourth record.
Figure 20.12 Reading the First Record of the Second Observation
SAS code example, raw data file, and Program Data Vector showing line control pointer on the first record of the second observation.
The values for Address are read. The / line pointer control advances the input pointer to the fifth record.
Figure 20.13 Reading the Second Record of the Second Observation
SAS code example, raw data file, and Program Data Vector showing line control pointer on the second record of the second observation.
The values for City, State, and Zip are read. The INPUT statement is complete.
Figure 20.14 Reading the Third Record of the First Observation
SAS code example, raw data file, and Program Data Vector showing line control pointer on the third record of the second observation.
The values in the program data vector are written to the data set as the second observation.
Figure 20.15 The Program Data Vector and the SAS Data Set Perm.Members
The program data vector and the second observation written to SAS data set Perm.Members.
After the data set is complete, PROC PRINT output for Perm.Members shows that each observation contains the complete information for one member.
proc print data=perm.members;
run;
Figure 20.16 PROC PRINT Output of the Complete Perm.Members Data Set
PROC PRINT output of the complete Perm.Members data set.

The Number of Records per Observation

Note: A raw data file must contain the same number of records for each observation.
Suppose there are only two records for the second member. Remember, the INPUT statement is set up to read three records.
data perm.members; 
   infile memdata truncover; 
   input Fname $ Lname $ / 
   Address $ 1-20 / 
   City :& $10. State $ Zip $;
Figure 20.17 Raw Data File Memdata
Raw data file Memdata with highlighting for the two records of second observation.
In this case, the second member's name and address are read and assigned to corresponding variables. Then the input pointer advances to the next record, as directed by the INPUT statement, and the third member's name is read as a value for City.
The INPUT statement looks for a value for State and Zip, so the input pointer advances to the next record and reads the member's address.
Before you write the INPUT statement, check to see whether the raw data file contains the same number of records for each observation. In this raw data file there are now three records for each observation.
Figure 20.18 Verifying the Number of Records for Each Observation
Raw data file Memdata with the records numbered for each observation.
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.133.122.68