Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Extracting numbers from code
From
Nick Winter <[email protected]>
To
[email protected]
Subject
Re: st: Extracting numbers from code
Date
Mon, 12 Nov 2012 11:01:17 -0500
If the codes are generated as you say, it seems to me that you can't
definitely identify which items were checked, for at least some
combinations. For example, the code "12" could indicate items 1 and 2,
or just item 12.
If the items less than 10 were coded with leading zeros (and the
resulting variable saved as a string to preserve a leading zero), then
something like this would work:
foreach code in 01 02 03 04 05 06 07 08 09 10 11 {
gen item`code'_dummy = strpos(originalvar,"`code'")>0
}
Nick Winter
On 11/12/2012 10:36 AM, [email protected] wrote:
Good day.
I have received a dataset from a survey in which numerous questions
asked respondents to check all that apply.
As an example, one such question had 11 possible items that
respondents could check.
The data is coded in such a way that if a respondent checked item 2,
item 4, item 7, item 10 and item 11, then the response in the dataset
is coded 2471011.
I need to create dummy variables for each item. As such, I need to
create 11 dummy variables, which equal 1 if the respondent checked
that item.
The respondent above would have.
item1_dummy = 0
item2_dummy = 1
item3_dummy = 0.... etc.
Until now, I have only encountered questions with up to 5 items, in
which case I manually coded them in the following way.
gen item1_dummy = 0
replace item1_dummy = 1 if item == 1 | (item>= 10 & item<= 19) | (item
= 100 & item <= 199) etc....
This is horribly inefficient though. Is there another, easier way to
do this? Is there a way to code something like
item2_dummy = 1 if item contains the number 2.
Any advice would be greatly appreciated.
Kind regards
Sanja
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/