Announcement

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

  • Re-ordering Wide Data

    Hi,

    I have a dataset that I reshaped to be wide instead of long because I wanted a each row to be for a specific household ID and each column to represent age1, age2, age3, sex1, sex2, sex3, etc. Currently, age1 represents the age of the youngest child in the household and age2 is the second youngest, and so on. Sex1 is the sex of the youngest child. However, I want it to be ordered from oldest to youngest so that age 1 is the age of the oldest/1st child and so on. How can I do this?

    Before I reshaped the data, I sorted it to be in descending order per household ID. But after the reshape, that was all changed. Can anyone help with this?



    AC
    Attached Files

  • #2
    Before I reshaped the data, I sorted it to be in descending order per household ID. But after the reshape, that was all changed. Can anyone help with this?
    It'd be great if you had actually showed code and sample data of the long form. It's unclear if the yellow variables are of string or labeled numerical format, so it's not easy to help.

    However, do check if this example can help. You'll just need to create the appropriate "j" variable for each household:

    Code:
    clear
    input hhid age female
    1011 2 1
    1011 8 0
    1011 4 1
    1012 6 1
    1012 8 0
    1013 4 0
    end
    
    gsort hhid -age
    bysort hhid: gen seq = _n
    
    reshape wide age female, i(hhid) j(seq)
    Results:
    Code:
         +---------------------------------------------------------+
         | hhid   age1   female1   age2   female2   age3   female3 |
         |---------------------------------------------------------|
      1. | 1011      8         0      4         1      2         1 |
      2. | 1012      8         0      6         1      .         . |
      3. | 1013      4         0      .         .      .         . |
         +---------------------------------------------------------+

    Comment


    • #3
      Thank you so much! This worked!

      Comment

      Working...
      X