Announcement

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

  • Arranging Panel Data

    Hello Everyone.
    I am working with the DHS data. The data that I have below shows hhid, the number of eligible children in the HHs(hv014), and the BMI's of the children in that HHs. I want to arrange the data such that I want to keep only a single column for all the BMI's.
    This is what I want the data to look like.
    Example
    HHID hv014 BMI's
    111 0 .
    112 2 .
    113 2 15.9
    113 2 17.8
    120 3 14.65
    120 3 17.49
    120 3 19.009


    ORIGINAL DATA
    Code:
    input str12 hhid byte hv014 float(bmi_01 bmi_02 bmi_03)
    "111"  0         .         .         .
    "112"  2         .         .         .
    "113"  2  15.90261 17.800783         .
    "114"  4         .         .         .
    "115"  2         .         .         .
    "116"  0         .         .         .
    "117"  0         .         .         .
    "118"  1  14.77411         .         .
    "119"  2 16.219723 17.066282         .
    "120"  3 14.651367  17.49031  19.00928
    "121"  7         .         .         .
    "122"  0         .         .         .
    "2 1"  2         .         .         .
    "2 2"  1         .         .         .
    "2 3"  0         .         .         .
    "2 4"  1         .         .         .
    "2 5"  0         .         .         .
    "2 7"  0         .         .         .
    "2 8"  0         .         .         .
    "2 9"  2         .         .         .
    "210"  0         .         .         .
    "211"  5         .         .         .
    "212"  2 14.107908         .         .
    "213"  3         .         .         .
    "214"  2   13.7203 13.128113         .
    "215"  0         .         .         .
    "216"  0         .         .         .
    "217"  0         .         .         .
    "218"  0         .         .         .
    "221"  0         .         .         .
    "222"  0         .         .         .
    "3 2"  0         .         .         .
    "3 3"  2         .         .         .
    "3 4"  2         .         .         .
    "3 5"  2 17.407408 16.464987         .
    "3 6"  2         .         .         .
    "3 7"  1 15.089164         .         .
    "3 8"  5         .         .         .
    "3 9"  4         .         .         .
    "310"  2         .         .         .
    "311"  2         .         .         .
    "312"  1  16.49102         .         .
    "313"  1         .         .         .
    "314"  2 15.684154 15.457788         .
    "315"  2 13.522594  13.69613         .
    "316" 12         .         .         .
    "317"  2         .         .         .
    "318"  0         .         .         .
    "319"  1 15.017857         .         .
    "320"  3 18.779316    18.125 15.370906
    "321"  0         .         .         .
    "322"  3         .         .         .
    "4 2"  0         .         .         .
    "4 3"  2         .         .         .
    "4 4"  1         .         .         .
    "4 5"  5         . 16.296824 13.606987
    "4 6"  0         .         .         .
    "4 7"  4         .         .         .
    "4 8"  1 16.351185         .         .
    "4 9"  0         .         .         .
    "410"  0         .         .         .
    "411"  1         .         .         .
    "412"  1         .         .         .
    "413"  0         .         .         .
    "414"  0         .         .         .
    "415"  3  14.20412         . 18.456871
    "416"  1         .         .         .
    "417"  0         .         .         .
    "418"  1   15.7193         .         .
    "419"  0         .         .         .
    "420"  3   17.3323         .         .
    "421"  1         .         .         .
    "422"  0         .         .         .
    "5 2"  1         .         .         .
    "5 3"  3         .         .         .
    "5 4"  5 15.112695         . 15.961038
    "5 5"  0         .         .         .
    "5 6"  5         .         .         .
    "5 7"  1 15.876577         .         .
    "5 8"  2 14.526503         .         .
    "5 9"  3         .         .         .
    "510"  5         .         .         .
    "511"  2  15.14626  12.70304         .
    "512"  3         .         .         .
    "513"  2         .         .         .
    "514"  0         .         .         .
    "515"  2         .         .         .
    "516"  2 14.922585         .         .
    "517"  3 16.890621 18.194687         .
    "518"  0         .         .         .
    "519"  3         .         .         .
    "520"  1         .         .         .
    "521"  2         .         .         .
    "522"  2 15.811314 15.967484         .
    "6 1"  1         .         .         .
    "6 2"  2 16.142904  13.97502         .
    "6 3"  4         .         .         .
    "6 4"  1         .         .         .
    "6 5"  2         .         .         .
    "6 6"  2         .         .         .
    end
    Please help me with it.
    THANKS

  • #2
    Perhaps this will start you in a useful direction.
    Code:
    reshape long bmi_, i(hhid) j(child) string
    destring child, replace
    drop if child>max(1,hv014)
    replace child = . if hv014==0
    rename bmi_ bmi
    order hv014, after(hhid)
    list in 1/18, sepby(hhid)
    Code:
    . list in 1/18, sepby(hhid)
    
         +---------------------------------+
         | hhid   hv014   child        bmi |
         |---------------------------------|
      1. |  111       0       .          . |
         |---------------------------------|
      2. |  112       2       1          . |
      3. |  112       2       2          . |
         |---------------------------------|
      4. |  113       2       1   15.90261 |
      5. |  113       2       2   17.80078 |
         |---------------------------------|
      6. |  114       4       1          . |
      7. |  114       4       2          . |
      8. |  114       4       3          . |
         |---------------------------------|
      9. |  115       2       1          . |
     10. |  115       2       2          . |
         |---------------------------------|
     11. |  116       0       .          . |
         |---------------------------------|
     12. |  117       0       .          . |
         |---------------------------------|
     13. |  118       1       1   14.77411 |
         |---------------------------------|
     14. |  119       2       1   16.21972 |
     15. |  119       2       2   17.06628 |
         |---------------------------------|
     16. |  120       3       1   14.65137 |
     17. |  120       3       2   17.49031 |
     18. |  120       3       3   19.00928 |
         +---------------------------------+
    The "string" option on reshape was needed because the leading zeroes confuse the reshape command.
    Last edited by William Lisowski; 19 Jan 2022, 12:18. Reason: Corrected handling of hv014==0

    Comment


    • #3
      Thank you. This was really helpful.

      Comment

      Working...
      X