Announcement

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

  • Data Setting: dropping empty variables in a quickly way

    Good morning to everyone,
    i was wondering if there is a command that tells Stata to drop variables if contains only missing values.

    I have a database with numerous variables and I would like to delete the ones that are completely empty without having to check them one by one.

    Many thanks in advance for your time.
    Wishing you a happy weekend.
    Last edited by Chiara Tasselli; 02 Jul 2021, 05:07.

  • #2
    you want the user-written -missings- command (use -search- of -findit- to locate and install); when looking at the help file you will see that one of the sub-commands is "dropvars" and that is what you want

    Comment


    • #3
      With the -missing()- function, this is easy as you do not need to differentiate between numeric variables and strings.

      Code:
      foreach var of varlist *{
          sort `var'
          if missing(`var'[1]) & missing(`var'[_N]){
              drop `var'
          }
      }
      Last edited by Andrew Musau; 02 Jul 2021, 09:20.

      Comment


      • #4
        With findname (Stata Journal) you can go


        Code:
        findname, all(missing(@)) 
        drop `r(varlist)'

        Comment


        • #5
          thank you all very much,
          I received lots of useful advice and solved my problem.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            With findname (Stata Journal) you can go


            Code:
            findname, all(missing(@))
            drop `r(varlist)'
            hi,Nick! Can you explain the meaning Of @ for me? I don't understand this syntax.

            Comment


            • #7
              See the help. Any condition used in an all() or any() option is a true-or-false condition defined by an expression in which variable
              names are represented by @. For example, any(@ < 0) selects numeric variables in which any values are
              negative.
              Last edited by Nick Cox; 27 Mar 2022, 11:23.

              Comment


              • #8
                Originally posted by Nick Cox View Post
                See the help. Any condition used in an all() or any() option is a true-or-false condition defined by an expression in which variable
                names are represented by @. For example, any(@ < 0) selects numeric variables in which any values are
                negative.
                Is it only applicable in this command?I mean ,does it relate to matrix or something else,for example, it represents all the values of one variable?

                Comment


                • #9
                  @ syntax is not generally available. If it is, that will be documented in the help. Nor does @ syntax always mean the same thing. Compare mylabels from SSC and coefplot from Stata Journal and SSC.

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    @ syntax is not generally available. If it is, that will be documented in the help. Nor does @ syntax always mean the same thing. Compare mylabels from SSC and coefplot from Stata Journal and SSC.
                    I see, thank you very much!

                    Comment

                    Working...
                    X