Statalist The Stata Listserver


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

st: Re: Re: double loop issue


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: Re: double loop issue
Date   Tue, 20 Feb 2007 09:11:03 -0500

I'm still not sure I understand what you are trying to do. You need to follow the logic of the loop in your head -- the first round through the inner loop it only changes the name of the first variable in the varlist (capital_spending1990) and then it executes the remanining statements in the inner loop. When it reaches the drop command near the end, there will only one one variable with tthe `p' suffix.

I think you may be better off trying my suggestion in my prior email. You should rename all of the variables at once for each p loop. Put a closing brace after

ren `var' `var_`p'

and then you need to create another loop across the varlist placed around the two replace statements near the end of the loop. In this approach, you create all of the renamed variables at once, merge it once and update the `var'_r versions for all.

Michael Blasnik


----- Original Message ----- From: "Molly Lipscomb" <[email protected]>
To: <[email protected]>
Sent: Monday, February 19, 2007 1:15 PM
Subject: st: Re: double loop issue



Thanks for your suggestions, I guess I wasn't as clear
as I meant to be.

The reason that I am renaming and merging the file in
so many times is that I need distance weighted
averages of about 150 variables over 55 counties for
each observation.  This would make the matrix size
much too large if I tried to do it in a single
command, so I am trying to use a look and use running
sums of each of the variables.

Here is the actual code that I was using (with some of
the variables in the loop removed to reduce space):

forvalues p=1/55 {
use C:\Brazil\Munisplits\mergingdata.dta
foreach var in capital_spending1990
health_sanitation_spending1990 investment1990
transfers1990 assessed_municipal_share1990 plus more
variables  {
ren `var' `var'_`p'
ren muni muni`p'
sort muni`p'
save C:\Brazil\Munisplits\averagesloop.dta, replace
use C:\Brazil\Munisplits\root.dta
sort muni`p'
merge muni`p' using
C:\Brazil\Munisplits\averagesloop.dta
tab _merge
drop if _merge==2
drop _merge
replace `var'_r=`var'_r+`var'_`p'*routelength`p' if
`var'_`p'!=. & routelength`p'!=.
replace `var'_d=`var'_d+routelength`p' if `var'_`p'!=.
& routelength`p'!=.

drop
capital_spending1990_`p'-adequate_drainage2000_`p'
save C:\Brazil\Munisplits\root.dta, replace
}
}

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