My strategy would be to generate a string-variable and use the
string-function -index(s1,s2)-. The following might work (not tested):
foreach var of varlist Q1 Q2 Q3 { /* Starts a loop over vars */
gen str1 `var's= "" /* empty string var */
replace `var's = string(`var') /* Fill string with contents */
forval i=1/7 {
gen `var'_`i' = index(`var's,"1") ~= 0 /* var=1 if val exists, else 0 */
}
}
(This works already for seven choices. You might replace Q1 Q2 Q3 with the
names of the 80 variables.)
regards
Katsuhide Isa wrote
> I have a survey data which has the following structure.
> Each column contains the result of a survey question
> that allows MA(multiple answer).
>
> * data1
> Q1 Q2 Q3 Q4
> 1 1 1 $B!D(B
> 2 2 2
> 3 3 12
> 12 4 1
> 13 5 1
> 23 12 2
> 123 24 1
> 13 14 2
> 23 125 12
> 2 234 1
> $B!D(B $B!D(B $B!D(B
>
> In the example above, Q1 has three choices,
> Q2 five choices, and Q3 two choice.
> But, it should have been typed as below:
>
> * data2
> Q1_1 Q1_2 Q1_3 Q2_1 ...
> 1 0 0 1
> 0 1 0 0
> 0 0 1 0
> 1 1 0 0
> 1 0 1 0
> 0 1 1 1
> 1 1 1 0
> 1 0 1 1
> 0 1 1 1
> 0 1 0 0
> $B!D(B $B!D(B $B!D(B
> Q1_1 indecates the 1st choice of Q1, Q1_2 the 2nd
> choice, and so on.
> That is, I'd like each variable to take on one if a
> relevant choice is selected.
>
> The question is, is it possible to convert the data1 into
> data2 from within Stata?
> One immediate solution would be to create new
> variables using -generate- as below (in the case of
> three choices):
>
> gen Q1_1 = 1 if Q1 == 1 | Q1 == 12 | Q1 == 13 | Q1 == 123
> gen Q1_2 = 1 if Q1 == 2 | Q1 == 12 | Q1 == 23 | Q1 == 123
> gen Q1_3 = 1 if Q1 == 3 | Q1 == 13 | Q1 == 23 | Q1 == 123
>
> But this method is rather troublesome(the maximum
> number of choices is seven, and there are as many as
> more than 80 questions in the original data!) and easy
> to mistype.
> I'd like to know if there is some better way to do the
> same thing.
>
> Any suggestions welcome.
> Thanks in advance.
>
> K.I.
>
>
>
>
>
>
>
>
>
>
>
> *
> * For searches and help try:
> * http://www.stata.com/support/faqs/res/findit.html
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
>
--
[email protected]
http://www.sowi.uni-mannheim.de/lesas
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/