我想在sas中拉出每个ID的前3个观察

时间:2016-02-27 15:28:25

标签: sas


我想拉出每个ID的前三个观察点。我现在使用的代码是: - >

DATA NEW;
INFILE  VLV DLM = ' ';
INPUT x1 x2;
RUN;


PROC SORT DATA = NEW ; 
BY X1 X2; 
RUN;

PROC PRINT DATA = NEW;
RUN;


DATA VIS;
SET NEW;

RETAIN i 0;

IF FIRST.x1 THEN i=0; 

i+1;

RUN;


PROC PRINT DATA = VIS;RUN;

DATA NEW ;

SET VIS;
if I LT 2 THEN OUTPUT;

RUN;


TITLE 'OUTPUT';
PROC PRINT DATA = NEW;
RUN;

Infile我正在使用
18 1 18 2
18 4
18 6
18 3
19 7
19 6
19 4
20 66
20 67
20 3
20 7
21 6
21 7
21 8
36 2
36 3
36 3
36 1

我希望输出如下: - >
18 1 18 2
18 4
19 7
19 6
19 4
20 66
20 67
20 3
21 6
21 7
21 8
36 2
36 3
36 3

谢谢!

2 个答案:

答案 0 :(得分:1)

您的输出条件不正确。

 if I LT 2 THEN OUTPUT;

如果你想要前三名,它应该小于4.

 if I LT 4 THEN OUTPUT;

此外,虽然您可以反复使用相同的数据集名称,但不建议使用。我建议从NEW更改最终数据集名称,这样您就不会覆盖输入数据集。

答案 1 :(得分:0)

除了Reeza关于输出条件的正确观点之外,您还在第二个DATA步骤中缺少BY语句。因此first.x1将在日志中生成未初始化的注释,这意味着它不存在:

409  DATA VIS;
410    SET NEW;
411    RETAIN i 0;
412    IF FIRST.x1 THEN i=0;
413    i+1;
414  RUN;

NOTE: Variable FIRST.x1 is uninitialized.

最终结果是你的计数器变量i永远不会重置为0.我建议:

426  DATA VIS;
427    SET NEW;
428    by x1;   *** NEW line;
429    RETAIN i 0;
430    IF FIRST.x1 THEN i=0;
431    i+1;
432  RUN;

NOTE: There were 10 observations read from the data set WORK.NEW.
NOTE: The data set WORK.VIS has 10 observations and 3 variables.

433
434  DATA NEWNEW ;
435    SET VIS;
436    if I LT 4 THEN OUTPUT; ***CHANGED LINE;
437  RUN;

NOTE: There were 10 observations read from the data set WORK.VIS.
NOTE: The data set WORK.NEWNEW has 5 observations and 3 variables.

请注意,不需要RETAIN i 0;语句,它由sum语句i+1完成。