Dear all
Please consider this example where I would like to attain the values in overalltype1, overalltype2 and overalltype3:
An overalltype should return a value where the value is the same amongst all observations per id (group). Otherwise, it should return a blank. (If the same values are blanks, it should also return a blank.)
I have tried the following code which almost works because it gives me extra values on top of the correct ones:
Thank you.
Please consider this example where I would like to attain the values in overalltype1, overalltype2 and overalltype3:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte(id number) str7 type1 str5 type2 str7 type3 str5(overalltype1 overalltype2) str7 overalltype3 1 1 "alpha" "bravo" "" "alpha" "bravo" "" 1 2 "alpha" "bravo" "" "alpha" "bravo" "" 2 1 "alpha" "alpha" "charlie" "" "alpha" "charlie" 2 2 "bravo" "alpha" "charlie" "" "alpha" "charlie" 2 3 "charlie" "alpha" "charlie" "" "alpha" "charlie" 3 1 "charlie" "delta" "charlie" "" "delta" "" 3 2 "delta" "delta" "" "" "delta" "" 3 3 "delta" "delta" "" "" "delta" "" 4 1 "" "alpha" "" "" "alpha" "" 4 2 "" "alpha" "alpha" "" "alpha" "" 4 3 "" "alpha" "" "" "alpha" "" end
I have tried the following code which almost works because it gives me extra values on top of the correct ones:
Code:
egen countsku = count(number), by(id) forval i = 1/3 { egen count`i' = count(type`i') if type`i' == type `i', by(id) sort id type `i' by id: gen overalltype`i' = type `i'[_N] if count `i' == countsku }
Comment