Skip to Main Content

Reading a multiple-line-per-subject data file | SAS Code Fragments

There are at least two ways to read data into SAS when there are multiple
lines of data per subject in the raw data file.  In the first example
below, a / is used between the variables for each line.  This is handy when
there are only a few lines of data per subject.  The second example shows
the use of the # symbol to denote the lines on which the variables following it
can be found.  This method is more useful when there are many lines of data
per subject.

* 2 lines of data per subject;
data temp;
input a b c / d e f;
cards;
1 2 3
4 5 6
1 3 5
2 4 6
;
run;

proc print data = temp;
run;
Obs    a    b    c    d    e    f

 1     1    2    3    4    5    6
 2     1    3    5    2    4    6
* three lines of data per subject;
data temp2;
input #1 a b #2 c d #3 e f;
cards;
1 2 
3 4 
5 6 
1 3 
5 2 
4 6
9 8
7 6
5 4
;
run;

proc print data = temp2;
run;
Obs    a    b    c    d    e    f

 1     1    2    3    4    5    6
 2     1    3    5    2    4    6
 3     9    8    7    6    5    4
* example reading in a file;
* this example assumes that you have saved this data set as a text file in "C:multiple_line.txt";
100010133346851              1 1330              7954140200                 5069
1000102           201  7                         *    2 2 2  *  4  4  8 8 8    john
1000103            5-              -                                   533
1000104            59                2                                 633
1000105            5-              -                                   522
1000106            222222222222
1000107            34
1000108            2336
1000109            2336
1000110            2555
1000111            400                                                         2
100020113226652      1       1 1340              7952140200                 5069
1000202           101  7                         *    *      *  *  6  * 7 7    carl
1000203            2  1         4             2  1                    431401
1000204            2  1         4             2  1                    432404
1000205            5-                3                                 622
1000206            543343442243
1000207            44
1000208            122
1000209            1223
1000210            2555
1000211            260    
data temp3;
infile 'c:multiple_line.txt' missover lrecl=100;
input id 1-5 line 6-7 v1 8-15 #2 v2 19-21 v3 $ 58-59 v4 $ 79-83 #3 v5 20 #11 v6 20-24;
run;

proc print data= temp3;
run;
Obs      id     line       v1        v2    v3     v4     v5     v6

 1     10001      1     33346851    201    2     john     5    400
 2     10002      1     13226652    101          carl     2    260