Announcement

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

  • Reshape wide with no time/order variable

    Hello. I am working with a dataset that has multiple observations for most of the identifiers labeled study_id. I would like to reshape wide but there is no variable indicating the order. Please see example of my data below. Is it possible to reshape such that the first medication is labeled med1, and the second (if any) is med2, etc.?

    [CODE]
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int study_id str34 med
      4 "cyclobenzaprine"       
      5 "lorazepam"             
      5 "promethazine"          
      5 "trazodone"             
      6 "alprazolam"            
      6 "eszopiclone"           
      7 "cyclobenzaprine"       
      7 "temazepam"             
      8 "citalopram"            
     9 "cyclobenzaprine"       
     22 "amitriptyline"         
     24 "venlafaxine"

  • #2
    Perhaps this will start you in a helpful direction.
    Code:
    // sort needed for "by study_id", stable option leaves observations within each study in the same order
    sort study_id, stable
    by study_id: generate mednum = _n
    reshape wide med, i(study_id) j(mednum)
    list, clean
    Code:
    . list, clean
    
           study_id              med1           med2        med3  
      1.          4   cyclobenzaprine                            
      2.          5         lorazepam   promethazine   trazodone  
      3.          6        alprazolam    eszopiclone              
      4.          7   cyclobenzaprine      temazepam              
      5.          8        citalopram                            
      6.          9   cyclobenzaprine                            
      7.         22     amitriptyline                            
      8.         24       venlafaxine
    Last edited by William Lisowski; 25 Aug 2022, 20:38.

    Comment


    • #3
      Katie, if the order of med is unimportant within an identifier, then I create an alphabetical order, as below, for the purpose of reshaping wide.

      Code:
      bys study_id (med): gen order = _n
      reshape wide med, i(study_id) j(order)
      Crossed with #2

      Comment


      • #4
        Thank you both! These both worked.
        Katie

        Comment

        Working...
        X