.- help for ^labsort^ .- Recode a grouping variable according to a group statistic and reorder its value labels ------------------------------------------------------------- ^labsort^ groupvar yvar [ ^, s^tat^(^string^) g^en^(^newvar^) l^abel^ (^string^)^ ^sort(^string^)^] Description ----------- ^labsort^ recodes a numerical grouping (categorical) variable so that the levels are sorted according to a group statistic such as the mean or median (any statistic available from -egen , by()- ). The value labels are also reordered. The variable label of ^groupvar^, if any, is copied to ^newvar^. Primarily for ordering categories in tables or graphs. Remarks ------- Suppose we have data on wind speed for various cities over a number of years. The string variable ^city^ is encoded to the numerical variable ^C^ with value label ^C^ for use in anova or graphing. The value labels for ^C^ will be in alphabetical order. ^C^ can be recoded and the value labels reordered so that the cities are now in order of their mean wind speed (over years) by ^labsort C speed , stat(mean) gen(Csort) lab(Csort)^ Options ------- ^stat(^string^)^ specifies the statistic: mean, median, min, max, sd ... Anything available with -egen , by()- . Default mean. ^gen(^newvar^)^ the name of the new variable to be created. Default _newvar. ^lbl(^string^)^ the new value label name. Default _newlbl. ^sort(^string^)^ sort order as per gsort: + or - Example -------- From JA Rice p382. Yearly max. wind speed by city(n=21) and year(N=37). . ^encode city , gen(C) label(C)^ . ^labsort C speed , stat(mean) gen(Csort) lab(Csort)^ . ^graph Csort speed, ylab(1/21) s(O)^ . ^preserve^ . ^collapse (mean) speed , by(city C Csort)^ . ^sort Csort^ . ^list city C Csort speed^ . ^restore city C Csort speed 1. Baker Baker Baker 29.8649 2. Yuma Yuma Yuma 32.4595 3. Eureka Eureka Eureka 35.5946 [snip] 20. Tatoush Island Tatoush Island Tatoush Island 64.7297 21. North Head North Head North Head 69.2432 . ^list city C _C speed, nolabel^ city C Csort speed 1. Baker 2 1 29.8649 2. Yuma 21 2 32.4595 3. Eureka 11 3 35.5946 [snip] 20. Tatoush Island 17 20 64.7297 21. North Head 13 21 69.2432 Author ------ Ross Odell Graduate School of Biomedical Engineering University of New South Wales Sydney, Australia email: r.odell@unsw.edu.au Acknowledgements ---------------- I used -labcopy- by NJ Cox as a stepping stone. Also see -------- On-line: help for @label@, @encode@, @labcopy@ (if installed) Manual: ^[U] 15.6^, ^[R] label^, ^[R] encode^