Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: does stata create an index that can be used in loops?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: does stata create an index that can be used in loops?
Date   Thu, 8 Feb 2007 10:56:36 -0000

The specific question has been well answered. 
In this case, there is an easy solution. 

The more general issue I take to be how to loop
through two or more lists in parallel. This isn't 
really the best way to think about Brent's problem, 
but he thought it was. Matters are not always quite 
so easy. There is a fairly lengthy discussion in 

Cox, N.J. 2003. Problems with lists. Stata Journal 3(2): 185--202    

Nick 
[email protected] 

Brent Fulton
 
> I am using stata 9 and want to know whether stata creates an 
> index in loops
> that I can use to pull from a local macro. For my end result, 
> I would like
> the following variables to be created:
> var1_county, var2_county, var3_county
> 
> I did the following:
> 
> local a "var1 var2 var3"
> foreach x of local a{
> local b "`b' `x'"
> }
> *the loop above results in the following: local b "var1_zip var2_zip
> var3_zip"
> 
> sort county
> foreach x of local b{
> by county: egen `x'_county=total(`x')
> }
> But this creates the variables: var1_zip_county, var2_zip_county,
> var3_zip_county
> 
> 
> Is there a way to do this: see*
> sort county
> foreach x of local b{
> by county: egen *grab the appropriate element of local 
> a*_county=total(`x')
> }

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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