Rank within cases
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
The following code will perform the within-case ranking for you. The basic approach is to string each case into 4 cases with 1 rating variable, while retaining the original case ID and the original variable position in this new file. The RANK procedure is used to do the ranking. The aggregate procedure is then used to 'compress' each ID into a single case with 4 rank variables. This new case-level file is then merged with the original. It may seem to involve a lot of file restructuring, but I think this is easier than writing the nested loops and conditional computes involved in mechanically doing the within-case sorting and variable ranking at the level of the original file. * Source unknown. * if your file is already sorted by an id variable * (we'll call ours ID) omit the sort and compute commands just below. * Otherwise, use whichever fits the condition of your id variable. * if you have an id variable but file is not sorted on id, * sort the file by id. sort cases by id. * if you don't have an id variable, make one. compute id = $casenum. save outfile = cesrates.sav . vector ces = cescon to cesho. loop cesvar = 1 to 4. compute cesrate = ces(cesvar) . xsave outfile = temp1.sav / keep = id cesvar cesrate . end loop. execute. get file = temp1.sav . rank variables = cesrate (d) by id / ties = low / rank into cesrank . numeric RANKCON RANKFA RANKACH RANKHO (f4.1). vector cesr = rankcon to rankho . compute cesr(cesvar) = cesrank. execute. aggregate outfile = * /presorted / break = id /rankcon rankfa rankach rankho = max(rankcon to rankho). MATCH FILES /FILE = cesrates.sav /FILE = * /BY id . execute.