Hello
Please consider my data:
The goal is to create variables Y2 and Y3 seen in the data based on the variables Option*.
Y2:
If Y1 is 'Cow', Y2's value should begin with 'Cow' and would be the more common one between the two options, 'Cow milk' and 'Cow grass'. If it is an equal number like in the third observation, the first value (amongst Options 1 to 4) is chosen.
If Y1 is 'Iced', Y2's value should begin with 'Iced' and would be the more common one between the two options, 'Iced coffee' and 'Iced tea'. If it is an equal number like in the sixth observation, the first value (amongst Options 1 to 4) is chosen.
Y3:
If Y1 is 'Cow', Y3's value would be a blank.
If Y2 is 'Iced', Y3's value would be the most common (amongst Options 1 to 14). If there is an equal number like in the fourth observation, the first value (amongst Options 5 to 14) is chosen.
I have only been able to get Y3's value if Y1 is 'Cow' with
Thank you.
Please consider my data:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str4 Y1 str11(Option1 Option2 Option3) str9 Option4 str11(Option5 Option6 Option7 Option8 Option9 Option10 Option11 Option12 Option13 Option14 Y2) str9 Y3 "Cow" "Cow milk" "Cow grass" "Cow grass" "Cow grass" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Cow grass" "" "Cow" "Cow milk" "Cow milk" "Cow grass" "Cow milk" "Iced tea" "Iced tea" "Iced tea" "Iced tea" "Iced tea" "Iced tea" "Iced tea" "Iced tea" "Iced tea" "Iced tea" "Cow milk" "" "Cow" "Cow milk" "Cow milk" "Cow grass" "Cow grass" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Iced coffee" "Cow milk" "" "Iced" "Iced coffee" "Iced coffee" "Iced tea" "Iced tea" "Cow milk" "Cow milk" "Cow milk" "Cow milk" "Cow milk" "Cow grass" "Cow grass" "Cow grass" "Cow grass" "Cow grass" "Iced coffee" "Cow milk" "Iced" "Iced coffee" "Iced tea" "Iced tea" "Iced tea" "Cow milk" "Cow milk" "Cow milk" "Cow milk" "Cow milk" "Cow milk" "Cow grass" "Cow grass" "Cow grass" "Cow grass" "Iced tea" "Cow milk" "Iced" "Iced coffee" "Iced coffee" "Iced coffee" "Iced tea" "Cow grass" "Cow grass" "Cow grass" "Cow grass" "Cow grass" "Cow grass" "Cow milk" "Cow milk" "Cow milk" "Cow milk" "Iced coffee" "Cow grass" end
Y2:
If Y1 is 'Cow', Y2's value should begin with 'Cow' and would be the more common one between the two options, 'Cow milk' and 'Cow grass'. If it is an equal number like in the third observation, the first value (amongst Options 1 to 4) is chosen.
If Y1 is 'Iced', Y2's value should begin with 'Iced' and would be the more common one between the two options, 'Iced coffee' and 'Iced tea'. If it is an equal number like in the sixth observation, the first value (amongst Options 1 to 4) is chosen.
Y3:
If Y1 is 'Cow', Y3's value would be a blank.
If Y2 is 'Iced', Y3's value would be the most common (amongst Options 1 to 14). If there is an equal number like in the fourth observation, the first value (amongst Options 5 to 14) is chosen.
I have only been able to get Y3's value if Y1 is 'Cow' with
Code:
replace Y3 = "" if strpos(Y1,"Co") > 0
Comment