Announcement

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

  • Reshape Long to Wide

    Hi,

    I am facing a problem in reshaping a dataset from long to wide format. The sample data example and commands are pasted below. The variables in my dataset are child ID, date, and session code. At the child level, I basically need to see the type of session (session code) delivered on a particular day or month. When I try reshaping, it gives an error message (values of variable date2 not unique within childid). Your suggestions/help would be much appreciated.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long childid float date2 long sess
    1100773 22713 10
    1100773 22720 11
    1100773 22685 12
    1100773 22699 14
    1100773 22706 15
    1100773 22726 16
    1100773 22729 17
    1100773 22718 24
    1100774 22713 10
    1100774 22720 11
    1100774 22685 12
    1100774 22699 14
    1100774 22706 15
    1100774 22726 16
    1100774 22729 17
    1100774 22718 24
    1100774 22714 25
    1100776 22713 10
    1100776 22720 11
    1100776 22685 12
    1100776 22692 13
    1100776 22699 14
    1100776 22706 15
    1100776 22726 16
    1100776 22729 17
    1100776 22714 25
    1100777 22713 10
    1100777 22720 11
    1100777 22685 12
    1100777 22692 13
    1100777 22699 14
    1100777 22706 15
    1100777 22726 16
    1100777 22729 17
    1100777 22714 25
    1101569 22713 10
    1101569 22720 11
    1101569 22685 12
    1101569 22692 13
    1101569 22699 14
    1101569 22706 15
    1101569 22726 16
    1101569 22729 17
    1101569 22714 25
    1101601 22713 10
    1101601 22720 11
    1101601 22685 12
    1101601 22692 13
    1101601 22706 15
    1101601 22726 16
    end
    format %d date2
    label values sess sess
    label def sess 10 "N21/NATP/Y2N/ATPG/S11", modify
    label def sess 11 "N21/NNF/Y2N/NFG/S13", modify
    label def sess 12 "N21/NNF/Y2N/NFG/S3", modify
    label def sess 13 "N21/NNF/Y2N/NFG/S5", modify
    label def sess 14 "N21/NNF/Y2N/NFG/S7", modify
    label def sess 15 "N21/NNF/Y2N/NFG/S9", modify
    label def sess 16 "N21/NPH/Y2N/PHG/S15", modify
    label def sess 17 "N21/NPH/Y2N/PHG/S17", modify
    label def sess 24 "N21/NWS/Y2N/WSY2/S4", modify
    label def sess 25 "N21/NWS/Y2N/WSY2/S6", modify



    Code:
    gen date2 = date(sessiondate, "DMY")
    format date2 %d
    
    drop if sessioncode =="null"
    encode sessioncode, gen(sess)
    
    keep sess childid date2
    
    reshape wide sess, i(childid) j(date2)

    Thanks,
    Harish

  • #2
    Try to use:

    Code:
    duplicates report childid date2
    to check if there are duplicates entries.

    Also, the proposed wide format will be extremely inefficient because there will be a variable for every single day, essentially a big field of missing (.) Since your dates are already there, it'd be easy to just use "if" to filter the date to get what you need.

    Comment


    • #3
      Thanks, Ken. It worked. On dates, I extracted only the month from date var and can see month-wise sessions, and it is very suitable for my overall purpose. Thanks much for your suggestion

      Comment

      Working...
      X