我有不同的国家,需要每个国家/地区的最后2次观察
India 200
India 300
India 400
US 1000
US 2000
US 3000
US 4000
我应该 -
India 300
India 400
US 3000
US 4000
答案 0 :(得分:1)
可能会有一个更短的方式,但这将有效:
data have;
country = "INDIA";
pop = 200;
output;
country = "INDIA";
pop = 500;
output;
country = "INDIA";
pop = 300;
output;
country = "US";
pop = 1200;
output;
country = "US";
pop = 1400;
output;
country = "US";
pop = 900;
output;
country = "US";
pop = 1500;
output;
country = "INDIA";
pop = 700;
output;
run;
proc sort data=have;
by country descending pop;
run;
data have;
set have;
by country;
retain cnt;
if first.country then cnt = 1;
else cnt = cnt + 1;
run;
proc sql noprint;
create table want as
select country,pop from have
where cnt < 3;quit;
答案 1 :(得分:0)