Hi, I am currently working with ICD10 codes and getting stuck on a question I feel like probably has a simple answer! Currently, I have a wide dataset, with diagnosis codes listed as dx_. encounter_id is the unique identifier and diagnosis codes range from dx1-dx45. Some of these diagnosis codes represent fractures. For each encounter_id, I want to sum up how many fractures they have. I have a list of relevant string prefixes that correspond to fractures in unique locations. For example, "S02", "S22", "S82", "S72".

How can I generate a variable, fracture_number, that counts how many times an individual has a dx = any of the strings listed above? Some people may have 4 fractures in different locations and I want to capture their total number.

thank you so much! Data example below

* Example generated by -dataex-. To install: ssc install dataex
clear
input int encounter_id str8(dx1 dx2 dx3 dx4 dx5 dx6)
1 "S22.22XB" "S51.802A" "W32.0XXA" "S27.2XXA" "S27.321A" "S21.301A"
2 "J96.90" "W33.01XA" "R40.2112" "R40.2212" "T14.90XA" "R40.2312"
3 "D64.9" "M79.7" "M86.18" "M86.68" "G82.20" "N31.9"
4 "S01.93XA" "Z66" "S01.93XA" "R40.2212" "R00.1" "X72.XXXA"
5 "Z51.5" "S02.40DB" "I82.621" "X73.0XXA" "R40.2363" "J98.11"
6 "Z68.37" "M89.78" "S02.40DS" "E66.9" "D64.9" "J98.11"
7 "S36.81XA" "Z78.1" "T81.4XXA" "K65.1" "S39.093A" "K56.7"
8 "S32.491B" "X94.0XXA" "D62" "B19.20" "S32.491B" "S32.391B"
9 "S32.89XB" "M21.371" "S39.81XA" "T79.7XXA" "S36.892A" "D72.829"
10 "S01.522A" "S01.502A" "R06.09" "S02.66XB" "Z78.1" "S01.542A"
11 "S21.242A" "S27.322A" "S24.111A" "S22.029A" "R40.2242" "T79.4XXA"
12 "S75.002A" "I82.432" "I10" "W32.0XXA" "D64.9" "R40.2142"
13 "S31.109A" "S36.114A" "S31.813A" "Z88.0" "S32.301B" "S31.635A"
14 "S72.434B" "Z18.89" "W32.0XXA" "M86.8X6" "M25.561" "S81.041A"
15 "F11.90" "R06.00" "O99.513" "R42" "O99.89" "G54.0"