Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: St: loop within a loop

 From Adam Guerrero To statalist@hsphsun2.harvard.edu Subject Re: st: St: loop within a loop Date Tue, 30 Apr 2013 11:50:17 -0400

```Regardless, the dialogue has been extremely helpful, so thanks nonetheless!!

On Tue, Apr 30, 2013 at 11:34 AM, Christopher Zbrozek <zbrozek@gmail.com> wrote:
> Nick is of course correct - my apologies!
>
> On Tue, Apr 30, 2013 at 11:29 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>> Further, this is not a problem in nested loops; the loops are parallel.
>> Nick
>> njcoxstata@gmail.com
>>
>>
>> On 30 April 2013 16:27, Nick Cox <njcoxstata@gmail.com> wrote:
>>> Not so. Second time round the inner loop, -generate- will fail as
>>>
>>> Also, eth = `mynum' needs to use == not =.
>>> Nick
>>> njcoxstata@gmail.com
>>>
>>>
>>> On 30 April 2013 16:22, Christopher Zbrozek <zbrozek@gmail.com> wrote:
>>>>
>>>> Something along these lines should work:
>>>>
>>>> foreach varname in "white" "black" "hispanic" "asian" "mixed" {
>>>>     forvalues mynum = 1/5 {
>>>>         gen `varname' = 0
>>>>         replace `varname' = 1 if eth = `mynum'
>>>>     }
>>>> }
>>>>
>>>> Best,
>>>> Christopher Zbrozek
>>>>
>>>> On Tue, Apr 30, 2013 at 11:17 AM, Adam Guerrero <adamgue@gmail.com> wrote:
>>>>> Hello,
>>>>>
>>>>> I am trying to create a loop within a loop for the following block of
>>>>> code (loop through variables white, black, hispanic, asian, and mixed
>>>>> from values 1 to 5):
>>>>>
>>>>> replace white=1 if eth==1
>>>>> generate black=0
>>>>> replace black=1 if eth==2
>>>>> generate hispanic=0
>>>>> replace hispanic=1 if eth==3
>>>>> generate asian=0
>>>>> replace asian=1 if eth==4
>>>>> generate mixed=0
>>>>> replace mixed=1 if eth==5
>>>>>
>>>>> I know decode would be easier, but please consider responding to help
>>>>> me out learning how to loop.
>>>>>
>>>>> I'm relatively new to Stata, so any suggestions would be very much appreciated.
>>>>>
>>>>>
>>>>> *
>>>>> *   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/
>> *
>> *   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/

--