在某些数据清理过程中,需要比较不同行之间的数据。例如,如果各行具有相同的countryID和subjectID,则保持最高温度:
CountryID SubjectID Temperature
1001 501 36
1001 501 38
1001 510 37
1013 501 36
1013 501 39
1095 532 36
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我将使用以下lag()功能。
proc sort table;
by CountryID SubjectID descending Temperature;
run;
data table_laged;
set table;
CountryID_lag = lag(CountryID);
SubjectID_lag = lag(SubjectID);
Temperature_lag = lag(Temperature);
if CountryID = CountryID_lag and SubjectID = SubjectID_lag then do;
if Temperature < Temperature_lag then delete;
end;
drop CountryID_lag SubjectID_lag Temperature_lag;
run;
Run Code Online (Sandbox Code Playgroud)
上面的代码可能有效。
但是我仍然想知道是否有更好的方法来解决此类问题?
我认为您使任务复杂化。您可以使用proc sql和max运行:
proc sql noprint;
create table table_laged as
select CountryID,SubjectID,max(Temperature)
from table
group by CountryID,SubjectID;
quit;
Run Code Online (Sandbox Code Playgroud)