SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Multiple Observations from a Single Record
Lesson Summary

This page contains

I. Text Summary

To go to the page where a task, programming feature, or concept was presented, select a link.


File Formats
One raw data record can contain enough information to produce several observations. Data is stored in this manner to reduce the size of the entire file.  The data can be organized into

Reading Repeating Blocks of Data
To create multiple observations from a record that contains repeating blocks of data, the DATA step needs to hold the current record until each block of data has been read and written to the data set as an observation. The DATA step should include statements that
  1. read the first block of values and  hold the current record with the @@ line-hold specifier
  2. optionally add a FORMAT statement to display date or time values with a specified format
  3. write the first block of values as an observation
  4. execute the DATA step until all repeating blocks have been read.

Reading the Same Number of Repeating Fields
To create multiple observations from a record that contains an ID field and the same number of repeating fields, you must execute the DATA step once for each record, repetitively reading and writing values in one iteration.The DATA step should include statements that
  1. read the ID field and hold the current record with the @ line-hold specifier
  2. execute SAS statements based on a variable value, using an iterative DO loop. The iterative DO loop repetitively processes statements that
    • read the next value of the repeating field and hold the record with the @ line-hold specifier
    • explicitly write an observation to the data set using an OUTPUT statement.
  3. complete the iterative DO loop with an END statement.

Reading a Varying Number of Repeating Fields
To create multiple observations from a record that contains an ID field and a varying number of repeating fields, you must execute the DATA step once for each record, repetitively reading and writing values in one iteration while the value of the repeating field exits.The DATA step should include statements that
  1. prevent the SAS System from reading the next record if missing values were encountered in the current record using the MISSOVER option
  2. read the ID field and the first repeating field, and then hold the record with the @ line-hold specifier
  3. optionally create a counter variable
  4. execute SAS statements while a condition is true, using a DO WHILE loop. A DO WHILE loop repetitively processes statements that
    • optionally increment the value of the counter variable using a sum statement
    • explicitly add an observation to the data set using an OUTPUT statement
    • read the next value of the repeating field and hold the record with the @ line-hold specifier.
  5. complete the DO WHILE loop with an END statement.

II. Syntax

To go to the page where a statement or option was presented, select a link.

Repeating Blocks of Data

LIBNAME libref  'SAS-data-library';
FILENAME fileref  'filename';
DATA SAS-data-set;
      INFILE raw-data-file;
      INPUT variables @@;
      FORMAT date/time-variable format;
RUN;

An ID Field Followed by the Same Number of Repeating Fields

LIBNAME libref  'SAS-data-library';
FILENAME fileref  'filename';
DATA SAS-data-set;
      INFILE raw-data-file;
      INPUT id-variable @;
      DO index-variable specification;
             INPUT repeating-variable @;
             OUTPUT;
      END;
RUN;

An ID Field Followed by a Varying Number of Repeating Fields

LIBNAME libref  'SAS-data-library';
FILENAME fileref 'filename';
DATA SAS-data-set;
      INFILE raw-data-file MISSOVER;
      INPUT id-variable  repeating-variable @;
      accumulator-variable=0
      DO WHILE (expression);
            accumulator-variable+1
            OUTPUT; 
            INPUT repeating-variable @;
       END;
RUN;


III. Sample Programs

Repeating Blocks of Data

     libname perm 'c:\records\weather';
     filename tempdata 'c:\records\weather\tempdata';
     data perm.april
        infile tempdata;          
        input Date : date. HighTemp @@;         
        format date date7.;        
     run;

An ID Field Followed by the Same Number of Repeating Fields
     libname perm 'c:\records\sales';
     filename data89 'c:\records\sales\1989.dat';
     data perm.sales89;
        infile data89;
        input ID $4. @;
        do Quarter=1 to 4;
           input Sales : comma. @;
           output;
        end;
     run;

An ID Field Followed by a Varying Number of Repeating Fields
     libname perm 'c:\records\sales';
     filename data89 'c:\records\sales\1989.dat';
     data perm.sales89;
        infile data89 missover;
        input ID $4. Sales : comma. @;
        Quarter=0;
        do while (sales ne .); 
           quarter+1;
           output;
           input sales : comma. @;
        end;
     run;


IV. Points to Remember
  • The double trailing @ (@@) holds a record across iterations of the DATA step until the end of the record is reached.

  • The single trailing @ releases a record when control returns to the top of the DATA step.

  • All data set names specified in the OUTPUT statement must also appear in the DATA statement.

  • Complete DO loops and DO WHILE loops with an END statement.



back||next

 

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

Terms of Use & Legal Information | Privacy Statement