Code using percentiles of a subset
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | SET MPRINT=ON. * Define a data file for illustration purposes. NEW file. input program. set seed=987654321. loop casenum=1 to 100. compute draw=uniform(100). compute class1=rnd(uniform(1)). end case. end loop. end file. end input program. execute. *It is important to define this dummy variable in the main data file. COMPUTE dummy=1. SAVE OUTFILE='temp.sav'. * Say you need to determine deciles (divide data in 10 pieces) based on cases where class1=1. * and that you must then recode ALL values of draw usign the break points obtained using cases where class1=0. * Raynald Levesque. SELECT IF class1=0. SORT CASES BY draw. * Number cases and calculate number of cases. COMPUTE nb=$casenum. RANK draw /n INTO n. * In next line, change the number 10 by the number you need. COMPUTE #delta=n/(10-.0001). * balance of paragraph finds the cut off points. COMPUTE flag=TRUNC(nb/#delta). AGGREGATE /OUTFILE=* /BREAK=flag /cut_off = FIRST(draw). * prepare cut off points data for merger with main data file. FLIP VARIABLES=cut_off. COMPUTE dummy=1. SAVE OUTFILE='cutoff points.sav'. *add cut_off points to original data file. GET FILE='temp.sav'. MATCH FILES /FILE=* /TABLE='C:\\Program Files\\SPSS\\cutoff points.sav' /BY dummy. DEFINE !rank_it (vname=!TOKENS(1) /nb_bins=!TOKENS(1)) VECTOR cutoff=var001 TO !CONCAT('var',!nb_bins). COMPUTE rank1=1. LOOP #cnt=2 TO !nb_bins. COMPUTE rank1=rank1 + (!vname>cutoff(#cnt)). END LOOP. EXECUTE. !ENDDEFINE. * Note: in the macro calls, the argument of nb_bins must have 3 characters, eg for 5 use 005, for 15 use 015, etc. !rank_it vname=draw nb_bins=010 . |