Hi, I am trying to extract top 3 minimum and maximum actual sales of each country (by group) from the dataset sashelp.prdsal2. My code is not dynamic, If any one could suggest how to incorporate changes within a single datastep, that would be nice. The program should be dynamic (if instead of top-3 we need top-n then it should work by rplacing 3 by n). the output should look like the attached screenshot. My code is - proc sort data= sashelp.prdsal2 out=orig;
by country actual;
run;
Data stage1;
set orig;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;
Data orig_6;
set orig;
by country;
if first.country=1 then delete;
if last.country =1 then delete;
run;
data stage2;
set orig_6;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;
Data orig_12;
set orig_6;
by country;
if first.country=1 then delete;
if last.country =1 then delete;
run;
data stage3;
set orig_12;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;
Data finalstage;
set stage1 stage2 stage3;
proc sort data=finalstage ;
by country min max;
run;
... View more