Statalist The Stata Listserver

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

st: Re: double loop issue

From   "Michael Blasnik" <>
To   <>
Subject   st: Re: double loop issue
Date   Sat, 17 Feb 2007 11:26:24 -0500

I think you have your parentheses misplaced for the inner loop, but since you don't actually show us your real commands (at least what you show is clearly illegal syntax) and you don't describe what you want in much detail, it's hard to answer with much confidence. If the inner loop is just meant to rename all of the variables , then you want the closing brace immediately after the rename statement.

foreach var in capital spending investment transfers {
ren `var' `var'_`p'

Also, if you are actually trying to save a file with the same name as the existing file (as in: -save c:\filename.dta-), then it won't work the second time around without a replace option and if you do that then the variable names will be changed in the original file. Perhaps your actual code appends _`p' to the saved file name or uses a tempfile since it seems like you only use that file for a merge.

Please try to show the actual commands when asking for help. Also, your overall objective sounds like it may be similar to some existing Stata command (some variant on cross, joinby, and/or merge)

Michael Blasnik

----- Original Message ----- From: "Molly Lipscomb" <>
To: <>
Sent: Friday, February 16, 2007 4:38 PM
Subject: st: double loop issue

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.
How do I fix this?
*   For searches and help try:

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