Announcement

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

  • Drop rows with all missing value

    Hi all,

    How can I drop observations with all missing value?

    Thank you,

    Jack Wangliang

  • #2
    Code:
    egen nmcount = rownonmiss(_all)
    drop if nmcount == 0
    Note: If some of your variables are strings, you will need to add the -strok- option to the -egen- command.

    Comment


    • #3
      Code:
      tempvar t
      ds
      egen `t'=rownonmiss(`r(varlist)'), strok
      keep if `t'>0
      drop `t'

      Comment


      • #4
        Good advice already; nevertheless https://www.statalist.org/forums/for...aging-missings tells you about missings from SSC and Stata Journal which allows

        Code:
        missings dropobs
        while checking whether you've saved the dataset in memory.

        Comment


        • #5
          Thank you all!

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Good advice already; nevertheless https://www.statalist.org/forums/for...aging-missings tells you about missings from SSC and Stata Journal which allows

            Code:
            missings dropobs
            while checking whether you've saved the dataset in memory.
            Hi Nick,

            I have a similar issue, but I want to drop the observations that has missing values in any of the variables. I have too many variables and I don't want to use the if option for all these variables. Do you know how to realize this?

            Thanks!

            Comment


            • #7
              Code:
              egen mcount = rowmiss(_all)
              drop if mcount

              Comment


              • #8
                Originally posted by Melody Brown View Post

                Hi Nick,

                I have a similar issue, but I want to drop the observations that has missing values in any of the variables. I have too many variables and I don't want to use the if option for all these variables. Do you know how to realize this?

                Thanks!

                I tried to use the code below, and find that there are 1302 obs with missing values. But it says no obs qualify. Do you know why this happened?
                . missings dropobs, force

                Checking missings in numdate moviename days1 days1_1 theaters1 theaters1_1 box1 box1_1 competitors competitors_1 volumecrit_tillt
                volumefresh_tillt volumetop_tillt volumewomen_tillt volumemen_tillt volumewhite_tillt volumeafrican_tillt volumehispanic_tillt
                volumeasian_tillt cpercentfresh_tillt cpercenttop_tillt cpercentwomen_tillt cpercentmen_tillt cpercentwhite_tillt cpercentblack_tillt
                cpercenthispanic_tillt cpercentasian_tillt cpercentfresh_tillt_1 cpercenttop_tillt_1 cpercentwomen_tillt_1 cpercentmen_tillt_1
                cpercentwhite_tillt_1 cpercenthispanic_1 cpercentasian_tillt_1 cpercentblack_1 volumeaud_tillt volumeverify_tillt volumewomenaud_tillt
                volumemenaud_tillt volumewhiteaud_tillt volumeafricanaud_tillt volumeasianaud_tillt volumehispanicaud_tillt apercentverify_tillt
                apercentwomen_tillt apercentmen_tillt apercentwhite_tillt apercentafrican_tillt apercenthispanic_tillt apercentasian_tillt
                volumeaud_tillt_1 apercentverify_tillt_1 apercentwomen_tillt_1 apercentmen_tillt_1 apercentwhite_tillt_1 apercenthispanic_1
                apercentasian_tillt_1 apercentafrican_tillt_1 volumenogender_tillt volumenorace_tillt apercentnorace_tillt apercentnogender_tillt
                apercentnorace_tillt_1 apercentnogender_tillt_1:
                1302 observations with missing values

                note: no observations qualify

                Comment


                • #9
                  -missings dropobs- removes those observations that have missing values on all variables. You asked to remove observations with a missing value on any variable.

                  I suspect there is a way to do this with -missings-, but I'm not a user of that program and wouldn't know how. See my response in #7 which will do what you ask and is simple to implement.

                  Comment


                  • #10
                    Clyde Schechter is right on the main point. Here is the help for missings, with some emphasis added.

                    missings dropobs drops any observations that are missing on all values in varlist.

                    Creating entirely empty observations (rows) and variables (columns) is a habit of many spreadsheet users, but neither is
                    helpful in Stata datasets. The subcommands dropobs and dropvars should help users clean up. Conversely, there is no explicit
                    support here for dropping observations or variables with some missing and some nonmissing values. Users so minded will find
                    other subcommands of use as an intermediate step, but multiple imputation might be a better way forward.


                    It is easy enough to find out how to find out with missings which observations have any values missing, but the much older method explained by Clyde is good enough. .


                    Comment


                    • #11
                      Originally posted by Clyde Schechter View Post
                      -missings dropobs- removes those observations that have missing values on all variables. You asked to remove observations with a missing value on any variable.

                      I suspect there is a way to do this with -missings-, but I'm not a user of that program and wouldn't know how. See my response in #7 which will do what you ask and is simple to implement.
                      Hi Clyde,

                      That works perfectly! Thanks a lot for your code

                      Comment


                      • #12
                        Originally posted by Nick Cox View Post
                        Clyde Schechter is right on the main point. Here is the help for missings, with some emphasis added.



                        It is easy enough to find out how to find out with missings which observations have any values missing, but the much older method explained by Clyde is good enough. .

                        Thanks, Nick. I was just confused why it won't delete the obs with missings. No the issue is solved. Thanks a lot!

                        Comment

                        Working...
                        X