Sorting values within cases (using the bubble sort algorithm)
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 | * Sorting values within cases. * Posted by Mike Lacy (probably to usenet). *************************. There's an elegant approach here, although I don't know that it would be any easier in the current situation: It's possible to implement a conventional sort algorithm in SPSS to sort variables within rows. I actually coded this up about 1 or 2 years ago and posted it in response to someone asking a similar question on the group. For a relatively small number of variables, I found that a bubble sort worked pretty fast. Unfortunately, I don't seem to have that code right at hand anymore, although I hope to dig it out of Deja News eventually. At least crudely, I think it went something like this: * Illustrate bubble-sorting variable values within cases . * There are 100 cases here, and 10 variables. * First create 100 cases with 10 random variables, x1 to x10 . input program . loop #i = 1 to 100 . do repeat x = x1 to x10 . compute x = rv.uniform(0,1) . end repeat . end case . end loop . end file . end input program . exe . * . * Sort X1 to X10 into ascending order . vector x = x1 to x10 . loop #i = 1 to 9 . /* top index is 1 less than number of variables loop #j = #i+1 to 10 . do if x(#i) >= x(#j) . compute #temp = x(#j) . compute x(#j) = x(#i) . compute x(#i) = #temp . end if . end loop . end loop . exe . It is possible, of course, to sort multiple variables within a case this way, i.e., swap values of Y,Z,W, depending on X. This would allow keeping a record indicating that X1, the smallest value, originally was variable 6; X2, the second smallest value, might originally have been variable 3, etc. Regards, =-=-=-=-=-=-=-=-=-==-=-=-= Mike Lacy, Ft Collins CO 80523 voice (970) 491-6721 fax (970) 491-2191 |
Related pages
...