Hello,
I have a dataset with a variable that corresponds to the answer to the question "When do you normally check your social networks?".
The problem I have is that the variable comes as follows:
id Var0
1 Home, School
2 Home, School
3 School, Lunch, Home
4 Gym
5 Lunch, School, Gym
6 Library, School, Home, Gym, Lunch
I am interested in having these variables separated, what I tried is this:
*first split the variable into different variables (Var1, Var2, Var3, Var4, Var5)
split Var1 p(,)
*Asigning a label to each of the possible answers, for each of the new variables (generated by the plit)
*1
label define Label_1 1 "Library" 2 "Home" 3 "School" 4 "Gym" 5 "Lunch"
encode Var1, generate (E_Var1) label(Label_1)
generate Place1=0
replace Place1 = 1 if E_Var1==1
replace Place1 = 2 if E_Var1==2
replace Place1 = 3 if E_Var1==3
replace Place1 = 4 if E_Var1==4
replace Place1 = 5 if E_Var1==5
*2
encode Var2, generate (E_Var2) label(Label_1)
generate Place2=0
replace Place2 = 1 if E_Var2==1
replace Place2 = 2 if E_Var2==2
replace Place2 = 3 if E_Var2==3
replace Place2 = 4 if E_Var2==4
replace Place2 = 5 if E_Var2==5
*And the same for the next 3 Variables.
*Finally I tried to generate a new variable taking into account just one of the answers, this is a variable for Home, one for Library, one for Lunch, etc.
gen Library = 0
replace Library=1 if Place1== 1 | Place2 == 1 | Place3 == 1 | Place4 == 1 | Place5== 1
gen Home = 0
replace Home =1 if Place1== 2 | Place2 == 2 | Place3 == 2 | Place4 == 2 | Place5== 2
gen During_Lectures = 0
replace During_Lectures = 1 if Place1== 3 | Place2 == 3 | Place3 == 3 | Place4 == 3 | Place5== 3
*And so on...
The problem is that this code does not give me the correct result, Stata assigns different labels to "Home" for example, in each of the encoded variables.
I would really appreciate if you can help me with this.
I have a dataset with a variable that corresponds to the answer to the question "When do you normally check your social networks?".
The problem I have is that the variable comes as follows:
id Var0
1 Home, School
2 Home, School
3 School, Lunch, Home
4 Gym
5 Lunch, School, Gym
6 Library, School, Home, Gym, Lunch
I am interested in having these variables separated, what I tried is this:
*first split the variable into different variables (Var1, Var2, Var3, Var4, Var5)
split Var1 p(,)
*Asigning a label to each of the possible answers, for each of the new variables (generated by the plit)
*1
label define Label_1 1 "Library" 2 "Home" 3 "School" 4 "Gym" 5 "Lunch"
encode Var1, generate (E_Var1) label(Label_1)
generate Place1=0
replace Place1 = 1 if E_Var1==1
replace Place1 = 2 if E_Var1==2
replace Place1 = 3 if E_Var1==3
replace Place1 = 4 if E_Var1==4
replace Place1 = 5 if E_Var1==5
*2
encode Var2, generate (E_Var2) label(Label_1)
generate Place2=0
replace Place2 = 1 if E_Var2==1
replace Place2 = 2 if E_Var2==2
replace Place2 = 3 if E_Var2==3
replace Place2 = 4 if E_Var2==4
replace Place2 = 5 if E_Var2==5
*And the same for the next 3 Variables.
*Finally I tried to generate a new variable taking into account just one of the answers, this is a variable for Home, one for Library, one for Lunch, etc.
gen Library = 0
replace Library=1 if Place1== 1 | Place2 == 1 | Place3 == 1 | Place4 == 1 | Place5== 1
gen Home = 0
replace Home =1 if Place1== 2 | Place2 == 2 | Place3 == 2 | Place4 == 2 | Place5== 2
gen During_Lectures = 0
replace During_Lectures = 1 if Place1== 3 | Place2 == 3 | Place3 == 3 | Place4 == 3 | Place5== 3
*And so on...
The problem is that this code does not give me the correct result, Stata assigns different labels to "Home" for example, in each of the encoded variables.
I would really appreciate if you can help me with this.

Comment