Announcement

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

  • Replace values within the group in panel data

    Here is an example of how my panel data is structured. I want to calculate the proportion of Afib patients with MI. For that, I would want to replace mi==1 in the same row as afib==1 only if the patient (by dupersid) has both afib and mi. Any help is appreciated.

    dupersid icdcode afib mi

    1 I10 0 0
    1 I50 0 0
    1 I25 0 1
    1 M75 0 0
    1 E78 0 0
    1 I48 1 0
    2 I21 0 0
    2 I33 0 0

    Thanks,
    Harshith
    Last edited by Harshith Thyagaturu; 28 Aug 2023, 20:11.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte dupersid str3 icdcode byte(afib mi)
    1 "I10" 0 0
    1 "I50" 0 0
    1 "I25" 0 1
    1 "M75" 0 0
    1 "E78" 0 0
    1 "I48" 1 0
    2 "I21" 0 0
    2 "I33" 0 0
    end
    
    collapse (max) afib mi, by(dupersid)
    tab mi if afib
    Note: The -collapse- command destroys the original data, so you might want to -preserve- before hand and -restore- after.

    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X