Statalist The Stata Listserver


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

Re: st: double loop issue


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: double loop issue
Date   Sat, 17 Feb 2007 08:39:12 +0000 (GMT)

--- Molly Lipscomb <molly_lipscomb@yahoo.com> wrote:
> I have a loop inside a loop so that I can merge a file into another
> file by several different variables (I have data on cities, and I
> need to merge in the data on cities by each city crossed in each
> observation).
> Right now I have the following code:
> forvalues p=1/55 {
> use C:\filename.dta
> foreach var in capital spending investment transfers ...
> ren `var' `var'_`p'
> save C:\filename.dta
> ..and then I open up the master data and merge in the renamed
> variables.
> }
> }
> 
> The problem is that stata will only rename the first variable in my
> variable list--here capital, it ignores the other variables.

I don't see why he picks up on only capital, I do see that this loop
will only work the first time round the "p loop" since you are
changeing the variable names in file filename, so the second time round
the p loop the variables will be called capital_1, spending_1, etc. So
you within the loop you should save the file under a different name
(generally speaking you should never save changes to you master
dataset, but save do-files that carry out the desired changes. So if
you made mistake(and believe me everybody does) they are easy to fix.)

hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


		
___________________________________________________________ 
All New Yahoo! Mail  Tired of unwanted email come-ons? Let our SpamGuard protect you. http://uk.docs.yahoo.com/nowyoucan.html
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index