sas - Keeping or deleting a group of observations based on a characteristic of a BY-group -
i answered sas question few minutes ago , realized there generalization might more useful 1 (here). didn't see question in stackoverflow.
the general question is: how can process , keep entire by-group based on characteristic of by-group might not know until have looked @ observations in group?
using input data similar earlier question:
* reason, tasked keeping observations * in groups of id_1 , id_2 contain @ least 1 obs * value of 0.; * in following data, following id , id_2 groups should * kept: * 2 (2 obs) * b 1 (3 obs) * b 3 (2 obs) * b 4 (1 obs) * resulting dataset have 8 observations.; data x; input id $ id_2 value; datalines; 1 1 1 1 1 1 2 0 2 1 b 1 0 b 1 1 b 1 3 b 2 1 b 3 0 b 3 0 b 4 0 c 2 4 ; run;
double dow loop solution:
data have; input id $ id_2 value; datalines; 1 1 1 1 1 1 2 0 2 1 b 1 0 b 1 1 b 1 3 b 2 1 b 3 0 b 3 0 b 4 0 c 2 4 ; run; data want; _n_ = 1 1 until(last.id_2); set have; id id_2; flag = sum(flag,value=0); end; _n_ = 1 _n_; set have; if flag output; end; drop flag; run;
i've tested against point
approach using ~55m rows , found no appreciable difference in performance. dataset used:
data have; id = 1 10000000; id_2 = 1 ceil(ranuni(1)*10); value = floor(ranuni(2) * 5); output; end; end; end; run;
Comments
Post a Comment