|
data temp01;
do id = 1 to 10000;
v = ceil(rannor(0)*10);
output;
end;
run;
%macro data_sampling(var, top_or_bottom_flg, percent);
proc sql;
drop table temp02;
drop table temp03;
quit;
data _null_;
set temp01 nobs=n;
call symputx('n', max(1, floor(n*&percent.)) );
stop;
run;
%do ii = 1 %to &n.;
%if &ii. = 1 %then %do;
data temp02(keep = a b);
retain a;
retain b;
set temp01 end=eof;
if _n_ = 1 then do;
a = id;
b = v;
end;else do;
%if "&top_or_bottom_flg." = "top" %then %do;
if b < v then do;
a = id;
b = v;
end;
%end;%else %do;
if b > v then do;
a = id;
b = v;
end;
%end;
end;
if eof then output;
run;
proc append base = temp03 data = temp02; run;
%end;%else %do;
data temp02(keep = a b);
retain a;
retain b;
if 0 then set temp03(keep = a);
if _n_ = 1 then do;
dcl hash h(dataset:"temp03");
h.definekey("a");
h.definedone();
end;
set temp01 end=eof;
if h.check(key:id) then do;
if _n_ = 1 then do;
a = id;
b = v;
end;else do;
%if "&top_or_bottom_flg." = "top" %then %do;
if b < v then do;
a = id;
b = v;
end;
%end;%else %do;
if b > v then do;
a = id;
b = v;
end;
%end;
end;
end;
if eof then output;
run;
proc append base = temp03 data = temp02; run;
%end;
%end;
%mend;
%data_sampling(var = v, top_or_bottom_flg = top, percent = 0.01);
|