Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Syntax Error in forvalues loop


From   "Michelle Debbink" <[email protected]>
To   <[email protected]>
Subject   st: Syntax Error in forvalues loop
Date   Fri, 31 Oct 2008 15:53:35 -0400

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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index