Statalist


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

st: Re: Syntax Error in forvalues loop


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Syntax Error in forvalues loop
Date   Fri, 31 Oct 2008 21:06:57 +0100

Welcome, good to see you guys on the list! You are handing -replace- a lot of variables which it cannot take. Give it only one per run through the loops! (see -h replace-) If necessary, nest those loops just as much as you nest a -forvalues- and an -if- loop in the second example.

Also, in that second loop, remove the quotes around `i' int he first line. Also familiarize yourselves with -egen- overall and with the -total()- option in particular.

HTH
Martin
_______________________
----- Original Message ----- From: "Michelle Debbink" <mdebbink@med.umich.edu>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, October 31, 2008 8:53 PM
Subject: st: Syntax Error in forvalues loop


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)
mdebbink@umich.edu


**********************************************************
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/



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