Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • number of variables in varlist with -describe-

    The help of -describe- states that the number of variables in varlist will be stored in r(k). However, when I try
    Code:
    sysuse auto, clear
    qui d
    di `r(k)'      // r(k)=12 -- OK
    foreach v of varlist _all {
       clonevar clone_`v' = `v'
    }
    qui d
    di `r(k)'      // r(k)=24 -- OK
    qui d clone_*
    di `r(k)'      // r(k)=24 -- but should be 12
    the last display command should show 12 (as I would expect from the help of -describe-) -- not 24. Previously (as of 2011) the -describe- command still seemed to work as I would expect, see Nick's answer in the old Statalist: https://www.stata.com/statalist/arch.../msg00744.html .

    What am I doing wrong or what has changed, since?
    Last edited by Dirk Enzmann; 13 Mar 2020, 12:42.

  • #2
    Thanks for bringing this up. I noticed this fact a few years ago but had forgotten: The display from -describe- conforms to the varlist specfied, but r(k) reflects the entire data set, e.g., try -describe weight- and one gets r(k) = 12. The help description is perhaps ambiguous about the intended behavior as regards r():

    describe stores the following in r():

    Scalars
    r(N) number of observations
    r(k) number of variables
    r(width) width of dataset
    r(changed) flag indicating data have changed since last save


    Comment


    • #3
      Thanks for granting that my interpretation of the help of -describe- is at least understandable (I am in good company if Nick stumbled about his, as well).

      Thus, -describe- can't be used directly to count the number of variables in a var*-list. One solution would be
      Code:
      qui ds clone*
      di `: word count `r(varlist)''
      In my example above this would result in the expected number of 12.
      Last edited by Dirk Enzmann; 13 Mar 2020, 13:46.

      Comment


      • #4
        I didn't have to grant anything <grin>. I take your side and I think the documentation is not good here.

        Comment

        Working...
        X