Announcement

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

  • Is there any command to list all varlists that have missing values?

    Dear Stata users,

    A quick question: Is there any command to list all varlists that have missing values? Maybe such command should also return `r(varlist)'. In the example data below, I want a command that could exactly list variable: Talla CIE Fuma EdadF. Put it concretely (suppose that command called foobar), when run foobar, it should do the following work:

    Code:
    . foobar _all
    Talla CIE Fuma EdadF
    
    . display `r(varlist)'
    Talla CIE Fuma EdadF
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 CP double Peso int Talla str6 CIE byte Fuma float(CProv ComAut) byte EdadF
    "08430"  64.7 169 "354.0"  2  8 8 16
    "08080" 101.5 170 "428.1"  .  8 8  .
    "08190"    55   . "012.22" 0  8 8 .z
    "08401"  66.7 175 "454.1"  2  8 8  .
    "29700"  75.5 170 "428.1"  2 29 1 .a
    "25002"    71 175 ""       1 25 8 16
    "17014"  67.5 177 "599.82" 0 17 8 .z
    "43365"  65.4 175 "466.11" 0 43 8 .z
    "29720"  71.1 181 "011.45" 2 29 1 14
    "08001"  69.8 178 "012.32" 2  8 8 14
    "08018"  53.8 162 "454.1"  0  8 8 .z
    "17430"  58.4 172 "014.01" 2 17 8 15
    "08005"  74.8 163 "038.11" 2  8 8 16
    "08012"  59.2 173 "055.8"  2  8 8 14
    "08530"    62 173 "651.01" 0  8 8 .z
    "08025"  66.9 176 "077.0"  2  8 8 17
    "08080"  59.8 168 "075"    2  8 8 18
    "17430"    58 164 "470"    2 17 8 16
    "08032"  61.2 170 "346.01" 0  8 8 .z
    "43365"  63.1 174 "428.9"  2 43 8 15
    "43365"  70.9 167 "425.3"  1 43 8 15
    "08005"  64.5 168 "381.03" 0  8 8 .z
    "25002"  92.3 189 "363.31" 2 25 8  .
    "08450"  69.8 167 "011.46" 1  8 8 17
    "08013"  82.2 171 ""       0  8 8 .z
    "17402"  67.8 180 "389.02" 0 17 8 .z
    "08720"  65.6 182 "514"    0  8 8 .z
    "25002"  71.5 184 "362.84" 0 25 8 .z
    "17014"  73.8 178 "483.8"  1 17 8 16
    "08016"  48.6 166 "487.8"  0  8 8 .z
    "08021"  61.6 172 "346.01" 0  8 8 .z
    "08222"  74.1 174 "466.11" 1  8 8 15
    "08014"  61.9 172 "466.19" 1  8 8 16
    "08005"  79.6 173 "072.7"  0  8 8 .z
    "17014"  66.1 176 "346.00" 0 17 8 .z
    "43365"  60.2 169 "360.21" 1 43 8 14
    "08002"  57.5 168 "361.32" 2  8 8 15
    "08031"  61.6 170 ""       2  8 8 13
    "08021"  84.7 173 "055.8"  2  8 8 13
    "17402"  70.2 167 "525.3"  2 17 8 12
    "08006"  64.1 168 "212.3"  2  8 8 14
    "25002"  85.2 185 "478.21" 2 25 8  .
    "25002"  54.5 159 "650"    0 25 8 .z
    "43365"  66.5 174 "599.3"  1 43 8 13
    "08850"    76 171 "389.02" 1  8 8 17
    "08911"  68.5 177 "381.02" 0  8 8 .z
    "17003"    65 174 "368.42" 0 17 8 .z
    "08031"  72.1 183 "389.03" 2  8 8  .
    "08850"  68.7 179 "362.34" 0  8 8 .z
    "08911"  57.2 166 "014.03" 0  8 8 .z
    "17003"  63.4 169 "599.1"  2 17 8 15
    "08031"  77.2 178 "600"    2  8 8 16
    end

  • #2
    missings from the Stata Journal may help.

    Code:
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input str5 CP double Peso int Talla str6 CIE byte Fuma float(CProv ComAut) byte EdadF
    
                CP        Peso     Talla        CIE      Fuma      CProv     ComAut     EdadF
      1. "08430"  64.7 169 "354.0"  2  8 8 16
      2. "08080" 101.5 170 "428.1"  .  8 8  .
      3. "08190"    55   . "012.22" 0  8 8 .z
      4. "08401"  66.7 175 "454.1"  2  8 8  .
      5. "29700"  75.5 170 "428.1"  2 29 1 .a
      6. "25002"    71 175 ""       1 25 8 16
      7. "17014"  67.5 177 "599.82" 0 17 8 .z
      8. "43365"  65.4 175 "466.11" 0 43 8 .z
      9. "29720"  71.1 181 "011.45" 2 29 1 14
     10. "08001"  69.8 178 "012.32" 2  8 8 14
     11. "08018"  53.8 162 "454.1"  0  8 8 .z
     12. "17430"  58.4 172 "014.01" 2 17 8 15
     13. "08005"  74.8 163 "038.11" 2  8 8 16
     14. "08012"  59.2 173 "055.8"  2  8 8 14
     15. "08530"    62 173 "651.01" 0  8 8 .z
     16. "08025"  66.9 176 "077.0"  2  8 8 17
     17. "08080"  59.8 168 "075"    2  8 8 18
     18. "17430"    58 164 "470"    2 17 8 16
     19. "08032"  61.2 170 "346.01" 0  8 8 .z
     20. "43365"  63.1 174 "428.9"  2 43 8 15
     21. "43365"  70.9 167 "425.3"  1 43 8 15
     22. "08005"  64.5 168 "381.03" 0  8 8 .z
     23. "25002"  92.3 189 "363.31" 2 25 8  .
     24. "08450"  69.8 167 "011.46" 1  8 8 17
     25. "08013"  82.2 171 ""       0  8 8 .z
     26. "17402"  67.8 180 "389.02" 0 17 8 .z
     27. "08720"  65.6 182 "514"    0  8 8 .z
     28. "25002"  71.5 184 "362.84" 0 25 8 .z
     29. "17014"  73.8 178 "483.8"  1 17 8 16
     30. "08016"  48.6 166 "487.8"  0  8 8 .z
     31. "08021"  61.6 172 "346.01" 0  8 8 .z
     32. "08222"  74.1 174 "466.11" 1  8 8 15
     33. "08014"  61.9 172 "466.19" 1  8 8 16
     34. "08005"  79.6 173 "072.7"  0  8 8 .z
     35. "17014"  66.1 176 "346.00" 0 17 8 .z
     36. "43365"  60.2 169 "360.21" 1 43 8 14
     37. "08002"  57.5 168 "361.32" 2  8 8 15
     38. "08031"  61.6 170 ""       2  8 8 13
     39. "08021"  84.7 173 "055.8"  2  8 8 13
     40. "17402"  70.2 167 "525.3"  2 17 8 12
     41. "08006"  64.1 168 "212.3"  2  8 8 14
     42. "25002"  85.2 185 "478.21" 2 25 8  .
     43. "25002"  54.5 159 "650"    0 25 8 .z
     44. "43365"  66.5 174 "599.3"  1 43 8 13
     45. "08850"    76 171 "389.02" 1  8 8 17
     46. "08911"  68.5 177 "381.02" 0  8 8 .z
     47. "17003"    65 174 "368.42" 0 17 8 .z
     48. "08031"  72.1 183 "389.03" 2  8 8  .
     49. "08850"  68.7 179 "362.34" 0  8 8 .z
     50. "08911"  57.2 166 "014.03" 0  8 8 .z
     51. "17003"  63.4 169 "599.1"  2 17 8 15
     52. "08031"  77.2 178 "600"    2  8 8 16
     53. end
    
    .
    . missings report
    
    Checking missings in all variables:
    28 / 52 observations with missing values
    
      +-------------------+
      |         # missing |
      |-------------------|
      | Talla           1 |
      |   CIE           3 |
      |  Fuma           1 |
      | EdadF          26 |
      +-------------------+
    
    .
    . missings list
    
    Checking missings in all variables:
    28 / 52 observations with missing values
    
         +-------------------------------+
         | Talla      CIE   Fuma   EdadF |
         |-------------------------------|
      2. |   170    428.1      .       . |
      3. |     .   012.22      0      .z |
      4. |   175    454.1      2       . |
      5. |   170    428.1      2      .a |
      6. |   175               1      16 |
         |-------------------------------|
      7. |   177   599.82      0      .z |
      8. |   175   466.11      0      .z |
     11. |   162    454.1      0      .z |
     15. |   173   651.01      0      .z |
     19. |   170   346.01      0      .z |
         |-------------------------------|
     22. |   168   381.03      0      .z |
     23. |   189   363.31      2       . |
     25. |   171               0      .z |
     26. |   180   389.02      0      .z |
     27. |   182      514      0      .z |
         |-------------------------------|
     28. |   184   362.84      0      .z |
     30. |   166    487.8      0      .z |
     31. |   172   346.01      0      .z |
     34. |   173    072.7      0      .z |
     35. |   176   346.00      0      .z |
         |-------------------------------|
     38. |   170               2      13 |
     42. |   185   478.21      2       . |
     43. |   159      650      0      .z |
     46. |   177   381.02      0      .z |
     47. |   174   368.42      0      .z |
         |-------------------------------|
     48. |   183   389.03      2       . |
     49. |   179   362.34      0      .z |
     50. |   166   014.03      0      .z |
         +-------------------------------+
    
    . missings table
    
    Checking missings in all variables:
    28 / 52 observations with missing values
    
           # of |
        missing |
         values |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |         24       46.15       46.15
              1 |         25       48.08       94.23
              2 |          3        5.77      100.00
    ------------+-----------------------------------
          Total |         52      100.00
    
    . missings breakdown
    
    Checking missings in all variables:
    28 / 52 observations with missing values
    
      +-----------------------------------------+
      |         # missing   empty   .   .a   .z |
      |-----------------------------------------|
      | Talla           1       .   1    0    0 |
      |   CIE           3       3   .    .    . |
      |  Fuma           1       .   1    0    0 |
      | EdadF          26       .   5    1   20 |
      +-----------------------------------------+
    The command name
    missings seems defensible but may be tricky in searches. Here is an otherwise unpredictable search term. The update to the command in Stata Journal 26(3) does not as yet show up in this version of Stata.

    Code:
    . search dm0085, entry
    
    Search of official help files, FAQs, Examples, and Stata Journals
    
    SJ-23-2 dm0085_3  . . . . . . . . . . . . . . . . Software update for missings
            (help missings if installed)  . . . . . . . . . . . . . . .  N. J. Cox
            Q2/23   SJ 23(2):595--596
            most important change is the addition of subcommand breakdown
    
    SJ-20-4 dm0085_2  . . . . . . . . . . . . . . . . Software update for missings
            (help missings if installed)  . . . . . . . . . . . . . . .  N. J. Cox
            Q4/20   SJ 20(4):1028--1030
            sorting has been extended for missings report
    
    SJ-17-3 dm0085_1  . . . . . . . . . . . . . . . . Software update for missings
            (help missings if installed)  . . . . . . . . . . . . . . .  N. J. Cox
            Q3/17   SJ 17(3):779
            identify() and sort options have been added
    
    SJ-15-4 dm0085  Speaking Stata: A set of utilities for managing missing values
            (help missings if installed)  . . . . . . . . . . . . . . .  N. J. Cox
            Q4/15   SJ 15(4):1174--1185
            provides command, missings, as a replacement for, and extension
            of, previous commands nmissing and dropmiss

    Comment


    • #3
      If a varlist suffices then findname (Stata Journal) would do it. More later.

      Comment


      • #4
        Code:
        . search findname, sj
        
        Search of official help files, FAQs, Examples, and Stata Journals
        
        SJ-23-4 dm0048_5  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q4/23   SJ 23(4):1096
                options vallabelcountdef() and vallabelcountuse() have been
                extended to allow zero (0) as an argument
        
        SJ-20-2 dm0048_4  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q2/20   SJ 20(2):504
                new options include columns()
        
        SJ-15-2 dm0048_3  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q2/15   SJ 15(2):605--606
                updated to be able to find strL variables
        
        SJ-12-1 dm0048_2  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q1/12   SJ 12(1):167
                correction for handling embedded double quote characters
        
        SJ-10-4 dm0048_1  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q4/10   SJ 10(4):691
                update for not option
        
        SJ-10-2 dm0048  . . . . . . . . . . . . . .  Speaking Stata: Finding variables
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q2/10   SJ 10(2):281--296
                produces a list of variable names showing which variables
                have specific properties, such as being of string type, or
                having value labels attached, or having a date format
        
        (end of search)
        
        . findname, any(missing(@))
        Talla  CIE    Fuma   EdadF

        Comment


        • #5
          Thank you very much Nick Cox, that's very helpful!
          Code:
          findname, any(missing(@))

          Comment


          • #6
            Some might want to see a solution for those with a disinclination or even disability (fearsome firewall) to install extras.

            Here is a suggestion:

            Code:
            foreach v of varlist * { 
                quietly count if missing(`v')
                if r(N) local toshow `toshow' `v'
            }
            
            di "{p}`toshow'{p_end}"
            This is essentially what is happening inside findname. Loop over all variables: count missings in each variable; if there are any, add the variable name to a local macro. When done, display the local.

            Comment


            • #7
              There is a delicate trade-off here. I can imagine wanting just to see the variable names for which there are missings, but at the same time why not see their frequencies too? And if you're seeing frequencies why not see a table of what the values are?

              My favourite missings subcommand now (usually) is missings breakdown, for which the code just grows a table based on which particular missing values occur.

              As documented, that subcommand was added in an update, and so is not covered in the original paper.

              Comment


              • #8
                Thank you very much Nick Cox. I will use missings, mdesc, nmissing, statmis, or any other missing detection commands. But sometimes, I just want to get a varlists that I can use in some latter procedure.

                Comment


                • #9
                  Noted. You will have seen that r(varlist) is a stored result.

                  Comment

                  Working...
                  X