Announcement

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

  • how to count the number of variables after split?

    Dear All,

    How to generate a new variable that equals the number of variables generated after split? I need to record the number each time I run split for a different variable in a loop. Thanks a lot!

    Vivian

  • #2
    See the help.

    split stores the following in r():

    Scalars
    r(nvars) number of new variables created
    r(varlist) names of newly created variables

    Code:
    clear
    input str42 foo
    "frog toad" 
    "frog toad newt" 
    end 
    
    split foo 
    
    local n1 = r(nvars) 
    di `n1'
    The result shown is 3.

    Putting the result in a variable is legal but unnecessary. Use a local macro.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      See the help.




      Code:
      clear
      input str42 foo
      "frog toad"
      "frog toad newt"
      end
      
      split foo
      
      local n1 = r(nvars)
      di `n1'
      The result shown is 3.

      Putting the result in a variable is legal but unnecessary. Use a local macro.
      Thank you Nick, problem solved. I skipped this part of the help manual, will be more cautious in the future.

      Comment

      Working...
      X