I want to generate 3 NEW variables using these variables in my data set:
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:
Can anyone suggest a code which can help me to generate these 3 variables using these 3 condition.
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 |
+------------------------------------------------------------------------------+