Announcement

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

  • Drop variables based on position

    Hi,

    I would like to know how I could drop all variables placed after a position, e.g. the 6th variable/column. How can I do this?

    Thanks in advance,
    John

  • #2
    Here's an illustration of how to do it with the auto.dta.

    Code:
    sysuse auto, clear
    
    unab all_vars: _all
    local nvars: word count `all_vars'
    
    forvalues i = 7/`nvars' {
        drop `:word `i' of `all_vars''
    }
    Note: This code relies on local macros. Consequently it cannot be run line-by-line or in chunks. You must run it without interruption from start to finish.

    Comment


    • #3
      Here's another way to do it using findname from the Stata Journal.


      Code:
      sysuse auto, clear
      
      
      findname, columns(1/6)
      keep `r(varlist)'

      Comment

      Working...
      X