Announcement

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

  • Drop base variables from fvexpand list

    I want to modify the variable list produced by fvexpand, e.g.

    Code:
    . webuse nhanes2f, clear
    
    . fvexpand i.sex i.race
    
    . return list
    
    macros:
                  r(fvops) : "true"
                r(varlist) : "1b.sex 2.sex 1b.race 2.race 3.race"
    I want to drop the base category variables from r(varlist), in this case 1b.sex and 1b.race. Is there any easy way to do this? The base category variables are causing problems with another program of mine (they aren't getting dropped like they should) so I want to see if this will solve my problem.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

  • #2
    I don't know of any elegant way, but brute force elimination works:

    Code:
    webuse nhanes2f, clear
    
    fvexpand i.sex i.race
    
    return list
    
    local full_list `r(varlist)'
    local reduced_list
    foreach t of local full_list {
        if strpos("`t'", "b.") == 0 {
            local reduced_list `reduced_list' `t'
        }
    }
    
    display `"`reduced_list'"'

    Comment


    • #3
      Probably not much of an improvement, since a loop is still needed, but I think this is the (undocumented) tool used by StataCorp.

      Code:
      webuse nhanes2f, clear
      
      fvexpand i.sex i.race
      local full_list `r(varlist)'
      
      local reduced_list
      foreach t of local full_list {
          _ms_parse_parts `t'
          if (r(base)) continue
          local reduced_list `reduced_list' `t'
      }
      
      display "`reduced_list'"
      Best
      Daniel

      Comment


      • #4
        Thanks Clyde and Daniel! _ms_parse_parts is one of those weird undocumented commands that are documented . You can read about it via

        Code:
        help _ms_parse_parts
        It turns out this may not actually solve the bigger problem I am having but I will keep playing around with it.
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        Stata Version: 17.0 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          For anyone arriving at this post.
          I recently found out that within the community contributed package "ftools", there is a command "ms_fvstrip" that does exactly this.
          Best Regards
          Fernando

          Comment

          Working...
          X