Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: foreach commands


From   Dan Blanchette <dan.blanchette@duke.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: foreach commands
Date   Tue, 14 Apr 2009 10:49:30 -0400 (EDT)

Having a nested for loop is messing you up because
the lpindexprice11 will attempt to be generated each
time the loop:
 foreach w of varlist wrfoodexpy11- wrfoodexpy111 {
processes a variable.

Since you have such good variable names, I'd suggest
using a -forvalues- loop:

forvalues n = 11/111 {
	gen lpindexprice`n'=ln(pindexprice`n')
	gen ltpindexprice`n'=lpindexprice`n'*wrfoodexpy`n'
}

drop pindexprice11-pindexprice111



Dan Blanchette
Research Associate
Center of Entrepreneurship and Innovation
Duke University's Fuqua School of Business
dan.blanchette@duke.edu

From 	  nigussie Tefera <nitefera@yahoo.com>
To 	  statalist@hsphsun2.harvard.edu
Subject 	  st: foreach commands
Date 	  Tue, 14 Apr 2009 07:19:41 -0700 (PDT)

Dear stata list
I want to generate variables containing lnpindexprice11-lnpindexprice111 and multiply them by wrfoodexpy11-wrfoodexpy111 and replace lnpindexprce*, respectively. I used foreach commond as follows but it works only for the first column i.e lnpindexprice11 but not for the other. Would you assist me please

foreach w of varlist wrfoodexpy11- wrfoodexpy111 {
foreach x of varlist pindexprice11-pindexprice111 {
	gen l`x'=ln(`x')
	gen lt`x'=l`x'*`w'
}
drop `x'
}

Nigussie



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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