# 'egen' command: Does Stata select the right level from an attribute?

#### nmarti222

##### New Member
I´m doing my Master thesis in Hybrid Choice model with the example of a fitness center.

My dataset has 81 respondents who have to choose 12 times (12 choice tasks) among 3 alternatives ("alternative 1", "alternative 2" and "nochoice"). There are different attributes for each alternative considered (type of access, weekly access, days per week and price). For the first alternative, the attributes were labelled as alt1_type, alt1_weac, alt1_dayp, alt1_pri. However, for the second alternative the attributes were labelled as alt2_type, alt2_weac, alt2_dayp, alt2_pri. The attribute I want to focus on is the "type of access" (present in alt1_type and alt2_type) and the corresponding levels are "mixed gender" and "female only".

EXPLANATION OF THE PROBLEM --> The level ("mixed gender" and "female only") of each attribute ("type of access" present in "alt1_type" and "alt2_type") changes randomly between alternative 1 and 2. For example, in the first choice task (from a total of 12 choice tasks) a respondent can choose "female only" that appears under "alt1_type" and obviously "mixed gender" appears under "alt2_type". In the second choice task "mixed gender" appears under "alt1_type" and obviously "female only" appears under "alt2_type". Levels are randomly assigned to each alternative until 12 choice tasks are completed by the survey respondent. For nochoice is always the same level, so no problem in here.

PROBLEM --> With the code below I'm assuming that the first alternative is always "female only" (or "mixed gender") and the second is always "mixed gender" (or "female only") and the third is always "nochoice" (that in that case is always the same, so no problem).

In the dataset I have a variable called "choice" that equals 1 to the first alternative selected: alternative 1. Choice equals 2 to the second alternative selected: alternative 2 and 3 to the third alternative "nochoice". How can I chose the correct level from each attribute?

MY "WRONG" CODE IN Stata -->
Code:
egen choice1 = sum(choice == 1), by(userid)
egen choice2 = sum(choice == 2), by(userid)
egen nochoice = sum(choice == 3), by(userid)
And after the code follows like this in order to know which is the correct distribution of the selected alternative 1, 2 and nochoice.

Code:
gen choice1n = round(choice1/12,.01)
gen choice2n = round(choice2/12,.01)
gen nochoicen = round(nochoice/12,.01)
gen choice12n = choice1n+choice2n
Any idea is more than welcome!

Many thanks!
Nerea