pmichna
pmichna

Reputation: 4888

SAS and line pointers in a loop

data test;
    infile datalines;
    input k1 k2 k3 k4 k5 k6 k7 k8 k9 k10;
    array a(*) k1-k10;
    do i=1 to 10;
        if a(i) eq . then stop;
        line=a(i);
        input #line k1 k2 k3 k4 k5 k6 k7 k8 k9 k10;
        output;
    end;
    stop;
    datalines;
        5 9 2 4 6 3 . . . .
        29 57 32 9 2 29 2 0 23 1
        83 34 28 1 43 3 24 2 6 2
        0 84 62 75 3 52 65 1 5 2
        0 2 12 45 92 3 60 24 6 2
        47 24 87 2 52 36 1 17 3 1
        90 93 2 1 40 20 75 2 5 14
        78 27 27 2 4 1 12 21 4 2
        21 40 3 21 3 19 3 2 4 2
        84 2 5 3 13 6 23 98 1 2 
    ;
run;

I want to read only the observations whose numbers are in the first row. The expected result:

0  2  12 45 92 3  60 24 6  2
21 40 3  21 3  19 3  2  4  2
29 57 32 9  2  29 2  0  23 1
0  84 62 75 3  52 65 1  5  2
47 24 87 2  52 36 1  17 3  1
83 34 28 1  43 3  24 2  6  2

The error I get after running my code:

ERROR: Old line 3387 wanted but SAS is at line 3391.
       Use: INFILE N=X; , with a suitable value of x.
RULE:       ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
3391                47 24 87 2 52 36 1 17 3 1
k1=0 k2=2 k3=12 k4=45 k5=92 k6=3 k7=60 k8=24 k9=6 k10=2 i=2 line=2 _ERROR_=1 _N_=1

What does "a suitable value of x" mean? What should I change in my code?

Upvotes: 0

Views: 136

Answers (2)

SRSwift
SRSwift

Reputation: 1710

You are overwriting the values in your array with your second input statement. Here they are read into different variables so as not to be overwritten.

data test;
    infile datalines n=100;
    input h1 h2 h3 h4 h5 h6 h7 h8 h9 h10;
    array h{*} h1-h10;
    do i = 1 to 10;
        line = h[i];
        if line then do;
            input #line k1 k2 k3 k4 k5 k6 k7 k8 k9 k10;
            output;
        end;
    end;
    keep k:;
    datalines;
        5 9 2 4 6 3 . . . .
        29 57 32 9 2 29 2 0 23 1
        83 34 28 1 43 3 24 2 6 2
        0 84 62 75 3 52 65 1 5 2
        0 2 12 45 92 3 60 24 6 2
        47 24 87 2 52 36 1 17 3 1
        90 93 2 1 40 20 75 2 5 14
        78 27 27 2 4 1 12 21 4 2
        21 40 3 21 3 19 3 2 4 2
        84 2 5 3 13 6 23 98 1 2 
    ;
run;

Upvotes: 2

user667489
user667489

Reputation: 9569

SAS is telling you that you need to amend your infile statement to allow it to read a sufficient number of lines ahead. For your code as written, n=10 should be ok, as none of variables you're using to get the line number have values greater than 10.

data test;
    /*Add the n= option to the infile statement as suggested by log message*/
    infile datalines n= 10;
    input k1 k2 k3 k4 k5 k6 k7 k8 k9 k10;
    array a(*) k1-k10;
    array b(*) b1-b10;
    /*Make a copy of the first row 
      that won't get overwritten by subsequent input statements*/
    do i=1 to 10; 
        b(i) = a(i);
    end;
    do i=1 to 10;
      if b(i) eq . then stop;
      line=b(i);
      input #line k1 k2 k3 k4 k5 k6 k7 k8 k9 k10;
      output;
    end;
    stop;
    datalines;
5 9 2 4 6 3 . . . .
29 57 32 9 2 29 2 0 23 1
83 34 28 1 43 3 24 2 6 2
0 84 62 75 3 52 65 1 5 2
0 2 12 45 92 3 60 24 6 2
47 24 87 2 52 36 1 17 3 1
90 93 2 1 40 20 75 2 5 14
78 27 27 2 4 1 12 21 4 2
21 40 3 21 3 19 3 2 4 2
84 2 5 3 13 6 23 98 1 2 
    ;
run;

Upvotes: 2

Related Questions