Announcement

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

  • Getting rid of unwanted missing value in a variable

    Hi,

    I have the following variable "coeff"

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float coeff
    .0003917254
              .
     .005586416
     -.02751333
      .04031171
    -.018891364
    .0009078109
              .
              .
              .
    end
    The missing values on the 7th, 8th, and 9th rows are okay, but I want to get rid of the missing value on the 2nd row. I cannot simply drop the 2nd row since there are other variables in the data. In the end, I want something like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float coeff
    .0003917254
     .005586416
     -.02751333
      .04031171
    -.018891364
    .0009078109
              .
              .
              .
              .
    end
    Are there any good ways to accomplish this? Thanks a lot in advance!

  • #2
    I'm not sure if this is what you meant?

    Code:
    clear
    input float coeff
    .0003917254
              .
     .005586416
     -.02751333
      .04031171
    -.018891364
    .0009078109
              .
              .
              .
    end
    
    gen someothervar = _n
    
    gen wanted = .
    replace wanted = coeff if _n == 1
    replace wanted = coeff[_n+1] if _n >= 2
    
    list, sep(0)
    Results:
    Code:
         +----------------------------------+
         |     coeff   someot~r      wanted |
         |----------------------------------|
      1. |  .0003917          1    .0003917 |
      2. |         .          2    .0055864 |
      3. |  .0055864          3   -.0275133 |
      4. | -.0275133          4    .0403117 |
      5. |  .0403117          5   -.0188914 |
      6. | -.0188914          6    .0009078 |
      7. |  .0009078          7           . |
      8. |         .          8           . |
      9. |         .          9           . |
     10. |         .         10           . |
         +----------------------------------+

    Comment


    • #3
      Hi Ken,

      Thanks a lot for the answer. I forgot to mention that the unwanted missing value could possibly be on any row of the first 7 rows. Your code definitely works well for this example, but is there a way to generalize this? Thanks!

      Comment


      • #4
        Pretty clunky, but should work. Hopefully someone may have a better idea.

        Code:
        clear
        input float coeff
        .0003917254
                  .
         .005586416
         -.02751333
          .04031171
        -.018891364
        .0009078109
                  .
                  .
                  .
        end
        gen id = _n
        gen someothervar = rnormal()
        
        preserve
        keep coeff
        rename coeff wanted
        gen seq = _n
        gen tokeep = !missing(wanted)
        gsort -tokeep seq
        gen id = _n
        drop tokeep seq
        save tempcoef, replace
        restore
        
        merge 1:1 id using tempcoef

        Comment


        • #5
          Thanks Ken!

          Comment


          • #6
            How about this?

            Code:
            gen ismissing = missing(coeff)
            sort ismissing, stable
            Last edited by Nick Cox; 11 Aug 2021, 23:13.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              How about this?

              Code:
              gen ismissing = missing(coeff)
              sort ismissing, stable
              Thanks Nick! This works well

              Comment

              Working...
              X