I have a dataset with a variable surg_type (“surgery type”) that can have the values 0, 1 or 2 and a string variable ops_ko3 (“ops code”) with 20 possible values (see below for an example of the dataset). I want to find the top 5 ops codes that occur most frequently for surg_type == 2 and then find out how frequently those 5 codes occur for all 3 surgery types (i.e. I want to find that ops_ko “501” is the most frequent ops code for surgery type 2 and find that it occurs “X” times for surgery type 2, “Y” times for surgery type 1 and “Z” times for surgery type 0).
Notes:
- Unfortunately, I cannot use any community-contributed Stata commands (so nmodes is not an option), as I will need to send the code to run elsewhere and cannot request any installations
- In the example the values for ops_ko3 all happen to be numerical, however, they can also be alphanumerical
bysort surg_type ops_ko3 : gen num=_N
bysort surg_type ops_ko3 : gen tmp= num if _n == 1
gsort -surg_type -tmp
gen top5num = tmp[5]
gen byte top5ops=num>=top10num if surg_type == 2
drop tmp top5num
tab ops_ko3 if top5ops == 1, sort
By doing this I find the top 5 values of ops_ko for surgery type 2. However, I have not been able to write a code to find how frequently those 5 ops codes occur for surgery types 0 and 1. Is there a way to flag all ops codes that are flagged for surg_type == 2 ?
I would appreciate any help you can offer! Thank you.
Chrissa
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float surg_type str3 ops_ko3 0 "529" 0 "565" 0 "555" 1 "589" 2 "542" 0 "501" 1 "523" 1 "578" 1 "589" 0 "578" 0 "542" 0 "542" 1 "589" 0 "542" 1 "589" 0 "578" 0 "542" 0 "565" 0 "578" 0 "542" 0 "578" 0 "555" 2 "529" 0 "542" 0 "578" 0 "542" 0 "506" 2 "523" 0 "501" 0 "518" 1 "589" 0 "542" 0 "532" 0 "542" 0 "555" 0 "535" 0 "532" 0 "535" 0 "555" 0 "542" 0 "578" 2 "578" 1 "578" 0 "523" 0 "542" 0 "542" 0 "542" 0 "523" 0 "555" 0 "578" 2 "501" 0 "560" 0 "535" 1 "589" 0 "578" 0 "542" 0 "560" 0 "535" 0 "578" 2 "565" 0 "560" 1 "589" 1 "589" 0 "542" 1 "589" 0 "542" 0 "565" 0 "542" 1 "589" 2 "501" 0 "542" 0 "542" 0 "560" 0 "542" 0 "578" 1 "589" 0 "578" 0 "542" 2 "501" 0 "560" 0 "578" 0 "578" 0 "560" 0 "555" 0 "542" 0 "560" 0 "542" 0 "542" 0 "542" 2 "578" 0 "578" 0 "578" 0 "578" 0 "578" 1 "589" 0 "542" 2 "508" 0 "560" 0 "542" 0 "535" end
0 Response to Flag most frequent values in a subgroup & apply flag to same values in other subgroups
Post a Comment