2016-12-26 4 views
0

は、私は別の国を持っており、それぞれの国の最後の2つの観測私が取得する必要があり、それぞれの国の最後の2つの観測

India 200 
India 300 
India 400 
US 1000 
US 2000 
US 3000 
US 4000 

が必要ゲット -

India 300 
India 400 
US 3000 
US 4000 
+0

私は言葉の壁があると知っているが、あなたは少しを明確にすることができますか? – johnny

+0

注文に基づいて最後の2つのレコード、または2番目の変数の金額によって '最後の2つ'を識別していますか? – Reeza

答えて

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; 
0

これは、データが国によってグループ化されていることを前提としています。私はあなたがこれをいくつかの並べ替えの先に見て呼び出すと思う。

data country; 
    input country $ x; 
    cards; 
India 200 
India 300 
India 400 
NZ 4567 
US 1000 
US 2000 
US 3000 
US 4000 
;;;; 
    run; 
data last2; 
    merge country country(firstobs=3 keep=country rename=(country=z)); 
    if country ne z; 
    run; 
proc print; 
    run; 

enter image description here

関連する問題