Announcement

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

  • stuck on long to wide format

    Hello good people,

    I need help with this situation here. Please give it a look.

    Below is my data, I have 100s of patients, and many more variables value1 value2 value3......etc.

    Now for each patient, I would like to create a row that has variables patientid date1 base_date2 base_value1 base_value2.....base_value25 final_date2 final_value1 final_value2 final_value3...final_value25

    Note: date1 is same for each unique patient. base & final are from variable base_final . Some patients have only base or final , in such case base_value/final_value could be missing in the wide data form. base_final has only base or final as observations.



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(PatientId date1 date2) byte(value1 value2) str5 base_final
    10664 20405 20396 3 3 "base"
    10664 20405 21732 2 6 "final"
    11412 22446 22460 2 4 "base"
    11412 22446 22596 2 2 "final"
    11543 21621 21621 1 4 "base"
    11543 21621 22677 1 2 "final"
    11551 21977 21958 6 1 "base"
    end
    format %td date1
    format %td date2
    Last edited by Nishan Lamichhane; 21 Mar 2022, 16:06. Reason: reshape

  • #2
    Code:
    ds date2 value*
    local to_widen `r(varlist)'
    local to_widen: subinstr local to_widen " " " @", all
    local to_widen @`to_widen'
    
    replace base_final = base_final + "_"
    
    reshape wide `to_widen', i(PatientId date1) j(base_final) string

    Comment


    • #3
      Worked perfectly Sir, Thank you so much.

      Comment

      Working...
      X