Dear members of Statalist,

I am fairly new to Stata and I have been struggling with an issue I believe is related to the "foreach" command. In my dataset, I have a variable (related_diagnosis) which contains various ICD10-codes where I am interested to make a new categorical variable which encodes 1 if there is a value within that variable containing a specific letter ("G"). related_diagnosis is coded as a string variable. The variable contains thousands of observations, and looking for observations containing "G" would take a very long time. I suppose I should use a loop such as "foreach" to simplify this.

I've tried to search for a similar topic without success. If this is answered in another topic, I would be grateful if someone could provide the link discussing this topic.

Best,
Haakon