当数据组合在一起但是出现故障时,是否有一种方法可以通过SAS中的组处理来使用?
data sample;
input x;
datalines;
3
3
1
1
2
2
;
run;
Run Code Online (Sandbox Code Playgroud)
尝试打印出每组的第一个:
data _null_;
set sample;
by x;
if first.x then do;
put _all_;
end;
run;
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
x=3 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=1
ERROR: BY variables are not properly sorted on data set WORK.SAMPLE.
x=3 FIRST.x=1 LAST.x=0 _ERROR_=1 _N_=2
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 3 observations read from the data set WORK.SAMPLE.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds
Run Code Online (Sandbox Code Playgroud)
只是重申 - 我不想先对分组数据进行排序 - 我需要按此顺序处理它. 我知道我可以创建一个代理变量来排序使用中间datastep和一个retain语句或lag()函数,但我真的在寻找一个避免这一步骤的解决方案.另外,我想在我的分组处理中使用first和last关键字.
在BY语句中使用NOTSORTED选项:
data _null_;
set sample;
by x NOTSORTED;
if first.x then do;
put _all_;
end;
run;
Run Code Online (Sandbox Code Playgroud)