Announcement

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

  • Droping "NA"s

    Hi Stata users.

    I have 26 variables that contain some observations as NA. Is there a way (
    olther than "
    drop if var==NA" for each variable)
    to drop all observations that contain "NA"s for any variable?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double id str17 mtb0 str9(ebv0 debt0 ebit0 ta1 ni1) str8 cfo1
    302319 "NA"       "-1364"    "376"     "-1055"   "117"      "-476"    "-278"  
    303063 ".83"      "-544"     "952"     "-2402"   "1486"     "-667"    "-1365" 
    282874 "238.15"   "200229"   "32358"   "35823"   "260814"   "30219"   "1883"  
    214440 "261.88"   "164534"   "0"       "6766"    "166933"   "6752"    "-3199" 
    378443 "1394.28"  "2887794"  "0"       "-760338" "11451238" "-391191" "350907"
    301631 "81305.81" "16763000" "7864000" "7905000" "35994000" "5142000" "6999000"
    283489 "6.9"      "NA"       "NA"      "NA"      "9699"     "-21888"  "-9224" 
    end
    Thanks in advance.
    Best Regards,

    Pedro
    (StataMP 16 user)

  • #2
    I had a similar problem (importing a CSV file that otherwise would work on R...) and I solved with a loop:

    Code:
    foreach var of varlist * {
        replace `var'="" if `var'=="NA"
        }
    My code replaced NAs with missing. I believe you can adjust it to either drop the observation or have the procedure (say, -reg-) do it for you.

    Best,

    Iuri.

    Comment


    • #3
      Thanks for your suggestion Iuri. I also used this loop but somehow didn't manage to do what I need.
      Best Regards,

      Pedro
      (StataMP 16 user)

      Comment


      • #4
        Pedro:
        what follows will do the trick:
        Code:
        . foreach var of varlist mtb0-cfo1 {
          2. drop if `var'=="NA"
          3.  }
        (1 observation deleted)
        (1 observation deleted)
        (0 observations deleted)
        (0 observations deleted)
        (0 observations deleted)
        (0 observations deleted)
        (0 observations deleted)
        
        . list
        
             +---------------------------------------------------------------------------------+
             |     id       mtb0       ebv0     debt0     ebit0        ta1       ni1      cfo1 |
             |---------------------------------------------------------------------------------|
          1. | 303063        .83       -544       952     -2402       1486      -667     -1365 |
          2. | 282874     238.15     200229     32358     35823     260814     30219      1883 |
          3. | 214440     261.88     164534         0      6766     166933      6752     -3199 |
          4. | 378443    1394.28    2887794         0   -760338   11451238   -391191    350907 |
          5. | 301631   81305.81   16763000   7864000   7905000   35994000   5142000   6999000 |
             +---------------------------------------------------------------------------------+
        .
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Thank you Carlo, works just perfect!
          That's probably the adjust Iuri suggested but I didn't manage to find.

          Thank you both.
          Best Regards,

          Pedro
          (StataMP 16 user)

          Comment


          • #6
            Pedro:
            happy with reading that you solved your problem.
            For the future, when something (command, code, else) does not produce what you expected it to, please act on https://www.statalist.org/forums/help#technical , especially #12.1.
            If you follow that advice, interested listers can spot faster what's the matter with your command, code or else and reply positively. Thanks.
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Thanks for your advice, Carlo. I'll certainly follow it.

              Best regards.
              Best Regards,

              Pedro
              (StataMP 16 user)

              Comment

              Working...
              X