Get the number of levels of a categorical variable as a single number in Stata -
i trying find way number of levels of categorical variable single number. example if have variable x 4 levels need somehow number. if type levelsof x following 1 2 3 4 can't number 4 there. there way using levelsof or command?
various commands give number of distinct values, kind of variable. ("categorical variable" statistical concept, rather stata concept.) perhaps simplest way one-off purposes ask one-way tabulation using tabulate. number of distinct values number of rows in table, returned r(r). note (1) can suppress table (which useful in program or file) , (2) missing values excluded default:
. sysuse auto, clear (1978 automobile data) . qui tab foreign . ret li scalars: r(n) = 74 r(r) = 2 . qui tab rep78 . ret li scalars: r(n) = 69 r(r) = 5 . qui tab rep78, missing . ret li scalars: r(n) = 74 r(r) = 6 an extended review of problem, pitched more generally, available here. paper introduces distinct command. uses include direct support looking @ number of distinct values systematically. search distinct in stata find download source recent version.
. distinct | observations | total distinct --------------+---------------------------- make | 74 74 price | 74 74 mpg | 74 21 rep78 | 69 5 headroom | 74 8 trunk | 74 18 weight | 74 64 length | 74 47 turn | 74 18 displacement | 74 31 gear_ratio | 74 36 foreign | 74 2
Comments
Post a Comment