Michelle, maybe a different approach, something like this, will work.
Basically create a set of temporary variables equal to the original
set of variables which you recode to 1 and 0 for each race category,
sum them up, and drop them then do it again for the next race
category.
forvalues i=1/[number of race categories] {
forvalues j=1/28 {
gen temp`j'=sprace`j'
}
forvalues k=1/28{
replace temp`k'=1 if sprace`k'==`i'
replace temp`k'=0 if sprace`k'~=`i'
}
egen count`i'=rsum(temp1-temp28)
drop temp1-temp28
}
hope this helps, Tim
On Fri, Oct 31, 2008 at 2:53 PM, Michelle Debbink
<[email protected]> wrote:
> Hi All - New to the listserv and seem to be running into an issue with a forvalues loop. We have a categorical variable for the race/ethnicity of a list of contacts for each respondent to a survey up to 28 contacts. Some respondents have fewer than 28 contacts and so have missing data for some contact entries. We would like to sum the number of contacts of each race for each respondent (so as to have a variable for number of white contacts, number of black contacts, etc). In order to sum the contacts, we need to make a dummy variable for each contact regarding their race, and missing values must be replaced with zero so that the sum isn't returned as missing. We have tried each of the following forvalues loops but it seems to not be working (all return "_=invalid name") and we would appreciate any guidance you could give.
>
> // generate dummy variables for ethnicity of all sex partners in last 12mo and replace missing with 0
> forvalues i = 1/28 {
> tab sprace`i', gen(sprace`i'cat_)
> replace sprace`i'cat_* =0 if sprace`i'==.
> }
>
> //generate an identical variable that has no missing values (which we could then use to make the dummy variables to sum)
> forvalues `i'=1/28{
> if sprace`i'!=. {
> gen spracem`i'=sprace`i'
> }
> else{
> gen spracem`i'=0
> }
> }
>
> //sum across all the new dummy variables to get number of contacts of each race
> forvalues i=1/28{
> gen numwhitepart = sum(sprace`i'cat_1)
> gen numblkpart=sum(sprace`i'cat_2)
> gen numhispart=sum(sprace`i'cat_3)
> }
>
> Thank you for all your help!
> Michelle
>
> *********************************************************************
> Michelle L P Debbink
> MD/PhD student, University of Michigan Medical School/School of Public Health
> Michigan Health Justice Director, American Medical Student Association
> 734-904-9924 (phone)
> 440-274-9659 (fax)
> [email protected]
>
>
> **********************************************************
> Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/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/statalist/faq
* http://www.ats.ucla.edu/stat/stata/