Announcement

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

  • A variable and another variable located to its left

    Hello. Is there a way to do an operation in Stata that performs calculations from a variable and another variable located immediately to its left, and does not depend on the name of the latter? Thank you

  • #2
    Here's one way to get the name of the variable "on the left":

    Code:
    sysuse auto, clear
    
    local myvar weight
    
    unab allvars: *
    
    local myvarpos: list posof "`myvar'" in allvars
    local leftvar = word("`allvars'",`=`myvarpos'-1')
    
    . dis "The variable left of `myvar' is `leftvar'."
    The variable left of weight is trunk.

    Comment


    • #3
      Code:
      sysuse auto , clear
      
      local trunk trunk
      local ref `trunk'
      mata : st_local("left_of_`ref'", st_varname(st_varindex("`ref'")-1))
      
      gen s = trunk + `left_of_trunk'

      Comment

      Working...
      X