Announcement

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

  • Drop variable if varlabel contains certain expression

    Hey Statalisters,

    I am trying to drop all variables that have the expression "Domestic Currency" in their variable label.

    I guess I have to combine foreach and strpos and local macros - but I can't figure out how.

    Thank you so much!
    Johannes

  • #2
    Here's a pattern

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . ds, has(varlabel *mpg*)
    mpg
    
    . drop `r(varlist)'
    
    . ds
    make          rep78         trunk         length        displacement  foreign
    price         headroom      weight        turn          gear_ratio

    Comment


    • #3
      Thank you, Nick! I had thought about that before. But that code doesn't work for me, since I need to check for two words seperated by a blank.

      Comment


      • #4
        Code:
        foreach v of var *  { 
              if strpos("`: var label `v''", "Domestic Currency") local todrop `todrop' `v' 
        } 
        
        di "`todrop'" 
        
        drop `todrop'

        Comment


        • #5
          I do not see why ds would not work here. Maybe I am missing something.

          Code:
          sysuse auto, clear
          ds , has(varlabel "*r R*")
          drop `r(varlist)'
          Best
          Daniel

          Comment


          • #6
            daniel klein is right. I was missing out the " " -- and I am supposed to be the author of this command....

            Comment


            • #7
              Thank you guys so much! Daniel is absoultely right. I had just put the *" "* in the wrong order before ...

              Nick Cox: happens to the best of us

              Comment

              Working...
              X