select specific rows by row number in sas

2019-07-13 14:23发布

问题:

I am new to SAS I have SAS data like (It does not contain Obs column)

Obs    ID    Name    Score1    Score2    Score3

1     101               90        95        98
2     203               78        77        75
3     223               88        67        75
4     280               68        87        75
.
.
.
.
100   468               78        77        75

I want data having row number 2 6 8 10 34. Output should look like

Obs    ID    Name    Score1    Score2    Score3

1     203               78        77        75
2     227               88        67        75
3     280               68        87        75
.
.
.

Thanks in advance.

回答1:

You can loop through each line of data with a data step and only output the lines when you are in the n'th loop with a condition like this.

data test;
    set LIB.TABLE;
    if _N_ in (2, 6, 8, 10, 34) then output;
    run;

where _N_ will correspond to the number of the line in this case.



回答2:

The other answer is ok for small tables, but if you are working with a very large table it's inefficient as it reads every row in the table to see whether it has the right row number. Here's a more direct approach:

data example;
    do i = 2, 6, 8, 10;
        set sashelp.class point = i;
        output;
    end;
    stop;
run;

This picks out just the rows you actually want and doesn't read all the others.



标签: sas