Dear all,

I have the following data with variable "seed_test" that have multiple answers.
I would like to create a dummy variable for each answer. And I use the following commands.
However, it is quite long. And I have many variables with multiple answers as "seed_test".

Can you please help me how to write a loop command from the below commands so that it will be shorter?

Thank you very much!



tab seed_test
gen seed_test_saltwater=1 if seed_test==2
replace seed_test_saltwater=0 if seed_test_saltwater==.
label var seed_test_saltwater "Select seeds by saltwater"

gen seed_test_water=1 if seed_test==3
replace seed_test_water=0 if seed_test_water==.
label var seed_test_water "Select seeds by water"

gen seed_test_notest=1 if seed_test==4
replace seed_test_notest=0 if seed_test_notest==.
label var seed_test_notest "No test for selecting seeds"

gen seed_test_other=1 if seed_test==5
replace seed_test_other=0 if seed_test_other==.
label var seed_test_other "Select seeds by other methods"

gen seed_test_wind=1 if seed_test==10
replace seed_test_wind=0 if seed_test_wind==.
label var seed_test_wind "Select seeds by wind"

----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte seed_test
10
 3
 .
 3
 .
10
 5
 3
 3
 3
 .
10
 3
 3
 .
 3
 .
 3
 .
10
10
 3
 .
10
 3
 .
10
10
 3
10
10
 3
 3
 3
10
 .
 3
10
 3
10
 .
 .
 .
 3
10
 3
 5
 .
 3
 .
 .
 3
 3
 .
 3
end
label values seed_test L_Seed_test
label def L_Seed_test 3 "3. Water", modify
label def L_Seed_test 5 "5. Other (specify)", modify
label def L_Seed_test 10 "10. Wind", modify
------------------ copy up to and including the previous line ------------------