Announcement

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

  • Extract variable names from factor variable fvvarlist

    How can I extract the list of original variable names from a fvvarlist, i.e. a list of factor variables, possibly with interactions?

    I need to do this for an ado file I'm writing so I want to be flexible enough to deal with various ways of expressing the fvvarlist. For example, maybe I have a varlist that looks like i.time##c.intensity or ib0.time##ib0.treatment. If I use fvunab I can separate out the interactions:
    Code:
    . fvunab myfv : ib0.time##ib0.treatment
    . di "`myfv'"
    ib0.bimestre ib0.wave ib0.bimestre#ib0.wave

    but what I want is to instead return a local with just the variable names, e.g.
    time intensity in the first example or time treatment in the second.

    I thought about using
    subsintr() to remove # and rexexm() or strpos() to separate the prefixes that precede a "." (period) in the variable name, but given the many ways a fvvarlist can be expressed, there must be a more robust way to do this. One complication is that each space in the fvvarlist does not necessarily separate a variable; for example a valid fvvarlist would be i(0 1)b0.treatment i(1 2 3 4 5 6)bn.time i(0 1)b0.treatment#i(1 2 3 4 5 6)bn.time

    Any ideas? Many thanks


  • #2
    Well, this approach is kludgy, and I'm not sure it covers every single possibility, but at least for the examples you provide it would work:

    Code:
    ds _all, has(type numeric)
    local numvars `r(varlist)'
    
    local result
    foreach v of local numvars {
        if strpos(`"`myfv'"', ".`v' ") | strpos(`"`myfv'"', ".`v'#") {
            local result `result' `v'
        }
    }
    local result: list uniq result
    display `"`result'"'
    My guess is there is a more elegant way to do this, but it probably requires some fairly complicated use of regular expressions and an extensive knowledge of all the possible configurations of factor variables. This approach is workable and quick to code.
    Last edited by Clyde Schechter; 14 Aug 2016, 19:17. Reason: Correct error in code

    Comment


    • #3
      Many thanks Clyde! This picked up everything with a small modification: changing `"`myfv'"' to `"`myfv' "' (adding a space at the end of the local) so that the first strpos() also picks up the final variable in the local macro. I also added strpos(`"`myfv' "', " `v' ") so that result would include variables that were included with no prefix in the fvvarlist.

      The final code I used is:

      Code:
      ds _all, has(type numeric)
      local numvars `r(varlist)'
          
      local result
      foreach v of local numvars {
         if strpos(`"`myfv' "', ".`v' ") | strpos(`"`myfv' "', ".`v'#") | strpos(`"`myfv' "', " `v' ") {
              local result `result' `v'
         }
      }
      local result : list uniq result
      display `"`result'"' // "


      Last edited by Sean Higgins; 14 Aug 2016, 19:54.

      Comment

      Working...
      X