Announcement

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

  • Delete all missing values in even and odd positions in two variables

    Dear Stata users,

    I have two variables of male and female. For variable male, it has missing value in every even position. For variable female, it has missing value in every odd position. I want to get a data that delete all missing value in male and female, and at the same time, allow the two variables be juxtaposed. I had supposed it is an easy task, however I can't find a solution. Thank you very much.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(male female)
     42208      .
         .  37775
    293543      .
         . 260119
    348331      .
         . 290133
    324226      .
         . 270798
    303669      .
         . 273668
    230968      .
         . 218585
    end
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(male female)
     42208  37775
    293543 260119
    348331 290133
    324226 270798
    303669 273668
    230968 218585
    end

  • #2
    I recall that there there are cuter solutions to this, but for what it's worth here's one straightforward possibility.
    Code:
    generate long rid = sum(mod(_n, 2))
    bysort rid (male): replace male = male[1]
    drop if mi(female)
    At least this one doesn't require a -reshape-, which is a consideration if you have a large dataset.

    Comment


    • #3
      Dear Joseph Coveney thank you so much. Actually this data is a by-product of NOT using -reshape-, since you mention it, I think I should use -reshape- from the beginning to avoid producing such structure as in #1.

      Comment


      • #4
        Just another way to do it:

        Code:
        gen long id = ceil(_n/2)
        collapse (lastnm) male female , by(id)

        Comment


        • #5
          Dear Nick Cox, thank you very much. That's cool!

          Comment

          Working...
          X