Announcement

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

  • numbering dates for reshape

    Hi
    I have a data where there are several observations (in this case diagnosis codes) for each patientid and admissiondate.
    ie
    patid diagnosis admissiondate
    1 heart failure 1jan02
    1 diabetes 1jan02
    1 angina 8march07
    1 bowel obstruction 10oct11
    2 hypertension 9sept04
    2 hernia 9sept04
    3 renal failure 08aug21
    3 head injury 07jun23
    3 diabetes 07jun23
    4 appendicitis 11march05
    4 migraine 11march 05


    i want to eventually make this data wide so the patid with same admission dates are all on one row. first I would like to number the admission dates for each patient so all the admission dates on 1st jan will be 1 and next admission 2 and so forth. Would anyone be able to help with this syntax?
    Many thanks

  • #2
    This may help, although long and fairly wide experience in Stata leads me to assert that long layout usually beats wide for flexibility of analysis.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float patid str42(diagnosis admissiondate)
    1 "heart failure"     "1jan02"   
    1 "diabetes"          "1jan02"   
    1 "angina"            "8march07" 
    1 "bowel obstruction" "10oct11"  
    2 "hypertension"      "9sept04"  
    2 "hernia"            "9sept04"  
    3 "renal failure"     "08aug21"  
    3 "head injury"       "07jun23"  
    3 "diabetes"          "07jun23"  
    4 "appendicitis"      "11march05"
    4 "migraine"          "11march05"
    end
    
    gen better_date = daily(admissiondate, "DM20Y")
    
    bysort patid (better_date) : gen wanted = _n 
    
    drop admissiondate 
    
    reshape wide diagnosis better_date, i(patid) j(wanted)
    
    list
    Do you really want to ignore year and just focus on day of year?

    Comment


    • #3
      Hi nick thanks for reply,
      in my data dates are in correct format with years so that isnt the issue. what I am struggling with is number admission dates occurring on the same day with the same number. For example, all the 1st jan dates for patient 1 will be n=1, then the next admission 8th march 07 will be n=2 as patients are given more than 1 diagnosis per admission if that makes sense? when i use the syntax 'gen wanted = _n' it just labels all the admission dates for patient 1, 1 2 3 4 despite some of them being the same.

      Comment


      • #4
        We can't tell what is true of your real data unless we see a real(istic) example.

        If you regard two or more diagnoses on the same day as belonging in the same variable, you should spell that out. How do you propose to combine diagnoses? They could be separated by e.g. commas, semi-colons or colons.

        I still don't follow whether you want conventional date order or time of year order.

        Comment

        Working...
        X