data y; filename y 'yuan92.dat'; infile y; input distnum distmag candid candpar candvot; *distmag = district magnitude; *candid = candidate id number; *candpar = 1 for KMT nominated = 2 for KMT approved = 3 for KMT member, neither nominated nor approved = 4 for DPP nominated = 5 for DPP member, not nominated = 6 for other; *candvot = votes cast for this candidate; run; proc sort; by distnum descending candvot; run; data y; set y; by distnum descending candvot; *calculate rank of finish and won/lost dummy variable, wl; if first.distnum then do; rank=0; end; rank=rank+1; retain rank; if rank le distmag then wl=1; else wl=0; run;