Announcement

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

  • Drop variables if label is empty

    Hi all,

    unfortunately i am not that familiar with stata. so this is my problem: i would like to delete all variables in my data set which have an empty (variable) label.
    this is how far i got:

    foreach v of varlist * {
    local x : variable label `v'
    }

    Is it possible to include sth like this:
    drop `v' if `x'==""
    unfortunately this doesn't work. does anyone know how to do this?

    thanks and kind regards

  • #2
    The idea is a good start but would need two fixes:

    Code:
    foreach v of varlist * {
           local x : variable label `v'
            if "`x'" == "" drop `v' 
    }
    Here is something which loops for you:


    Code:
    ds, not(varlabel)
    drop `r(varlist)'
    See also findname (SJ). The syntax is similar


    Code:
    findname, varlabel not 
    drop `r(varlist)'
    Last edited by Nick Cox; 20 May 2016, 08:38.

    Comment


    • #3
      Thanks a lot! I will try your code as soon as i am back in the office.

      Comment


      • #4
        Copying and pasting led to your font being copied too. This should look clearer. (Please follow FAQ Advice on using CODE delimiters.)

        Code:
        foreach v of varlist * {
            local x : variable label `v'
            if "`x'" == "" drop `v'
        }
        Nevertheless, I recommend ds or findname. findname is in effect an update of ds, but ds is official and findname is user-written and must be installed. I think the update improves the syntax; it certainly adds more features for other problems.
        Last edited by Nick Cox; 21 May 2016, 03:52.

        Comment


        • #5
          Hi Nick, your code worked perfectly. Thanks a lot!

          Comment

          Working...
          X