Home > Software design >  How to use multiple condition to generate a new variable in Stata?
How to use multiple condition to generate a new variable in Stata?

Time:11-26

I want to generate 3 NEW variables using these variables in my data set:

  1. Ucod
  2. 19 variables in series by this name: Record_2, Record_3......Record_20

Both of them have values in alphanumerical format in it, basically ICD codes i.e, I150 I want to generate 3 new variables satisfying each of three new condition:

  1. People dying primarily of COVID (Var1=1 if Ucod= U07.1)
  2. People dying of a non-COVID condition WITH covid (Var2=1 IF Ucod != U07.1 & Record_2/20= U07.1)
  3. People dying of a non-COVID condition WITHOUT covid (Var3=1 if Ucod != U07.1 & Record_2/20 != U07.1)

Can anyone suggest a code which can help me to generate these 3 variables using these 3 condition.

CodePudding user response:

This may help. Note how I needed to define a toy dataset to give flavour to the problem.

* Example generated by -dataex-. 
clear
input str5(Ucod Record_2) str4(Record_3 Record_4)
"U07.1" "U000"  "U111" "U222"
"U999"  "U07.1" "U444" "U333"
"U888"  "U777"  "U666" "U555"
end

gen wanted1 = Ucod == "U07.1" 

gen count = 0 

quietly foreach v of var Record_* { 
   replace count = count   (`v' == "U07.1")
} 

gen wanted2 = Ucod != "U07.1" & count > 0 

gen wanted3 = Ucod != "U07.1" & count == 0 

list 

      ------------------------------------------------------------------------------ 
     |  Ucod   Record_2   Record_3   Record_4   wanted1   count   wanted2   wanted3 |
     |------------------------------------------------------------------------------|
  1. | U07.1       U000       U111       U222         1       0         0         0 |
  2. |  U999      U07.1       U444       U333         0       1         1         0 |
  3. |  U888       U777       U666       U555         0       0         0         1 |
      ------------------------------------------------------------------------------ 

  • Related