Announcement

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

  • subinstr local does not work

    Why does this code not work to remove X from the list? The variable is still in the list..

    Code:
    ds, has(type numeric)
    local r(varlist) : subinstr local r(varlist) "X" "", all
    foreach var of varlist `r(varlist)' {
    su `var'
    assert r(sd) > 0
    }
    Last edited by Henry Strawforrd; 13 Mar 2023, 03:01.

  • #2
    Like this is works

    Code:
     
     ds, has(type numeric) local vars "`r(varlist)'" local vars : subinstr local vars "X" "", all foreach var in `vars' { su `var' assert r(sd) > 0 }

    Comment


    • #3
      r(varlist) is not the name of a local macro. It is true that you can refer to its contents with syntax like that used to reference local macros, but in any strict sense r(varlist) is not a local macro.

      ds doesn't have an option to push its results directly into a local macro. (This lack was remedied in its successor, findname from the Stata Journal.)

      That being so, you need something more like

      Code:
      ds, has(type numeric) 
      local varlist `r(varlist)'
      and then you can work on that.

      If you want to find numeric variables that are constant, here is another way:

      Code:
      . sysuse auto, clear
      (1978 automobile data)
      
      . gen foo = 42
      
      . gen bar = 666
      
      . findname, type(numeric) all(@==@[1]) local(constant)
      foo  bar
      
      . di "`constant'"
      foo bar

      Comment

      Working...
      X