A Statalist post from 2011 lists several options for counting variables in a varlist. Quote:
This does not match what I get in Stata 14.2. In the example below, r(k) stores the number of variables in the auto data, not the number of variables in the describe command. Has the behavior of describe changed since 2011?
help describe is not clear on this because the exact meaning of "number of variables" in the excerpt below is not known. It could be the number of variables in the dataset or the number of variables in the describe command. From the help file:
For the record, another option in the above-mentioned Statalist post yields the correct number of variables:
After
. describe <varlist>
r(k) contains the number of variables in <varlist>.
. describe <varlist>
r(k) contains the number of variables in <varlist>.
Code:
. sysuse auto (1978 Automobile Data) . d make price storage display value variable name type format label variable label ---------------------------------------------------------------------------------------- make str18 %-18s Make and Model price int %8.0gc Price . return list scalars: r(changed) = 0 r(width) = 43 r(k) = 12 r(N) = 74
describe stores the following in r():
Scalars
r(N) number of observations
r(k) number of variables
Scalars
r(N) number of observations
r(k) number of variables
Code:
. unab vars : make price . di `: word count `vars'' 2
Comment