Announcement

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

  • number of columns (variables)?

    Dear All, I found this question here. The question is that how we count the number of columns in the data set. For example,
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 X float Y str5 Z str3(P Q)
    "一" 1 "one"   "Ⅰ" "①"
    "二" 2 "two"   "Ⅱ" "②"
    "三" 3 "three" "Ⅲ" "③"
    "四" 4 "four"  "Ⅳ" "④"
    "五" 5 "five"  "Ⅴ" "⑤"
    "六" 6 "six"   "Ⅵ" "⑥"
    end
    there are 5 columns. Any suggestions are appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    I may well be missing something in your question, but if you use -describe-, possibly with options to reduce the output or even -quietly-, the number of variables is returned in "r(k)" - there are other commands that also return this information

    Comment


    • #3
      Rich Goldstein is right; and let's add that creturn list hints that


      Code:
      di c(k)
      returns the number of variables too.

      Comment


      • #4
        The number of variables in the dataset currently active is stored in Stata's c-class value returned by c(k).
        Code:
        . clear all
        
        . display c(k)
        0
        
        . sysuse auto
        (1978 Automobile Data)
        
        . display c(k)
        12
        
        . frame put make price, into(cars)
        
        . display c(k)
        12
        
        . frame cars: display c(k)
        2

        Comment


        • #5
          Dear @Rich Goldstein, @Nick Cox, and @William Lisowski, Thank you all for these helpful suggestions.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment

          Working...
          X