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

st: RE: foreach commands

From   "Nick Cox" <>
To   <>
Subject   st: RE: foreach commands
Date   Tue, 14 Apr 2009 15:30:50 +0100

A few days ago, I posted some generic advice within 


Some of that applies here: 

Experiment with simple cases

If you're unsure about what a definition implies, experiment with simple
cases. Let Stata tell you. 

Illustrate your problem with mutually accessible data

We don't have your data, which makes it harder to get to the position you are in. 

Don't say "it didn't work"; say what happened

I have an evolving private document detailing (so far) 19 different
senses of "didn't work", collected from Statalist postings. Even Stata
experts familiar with what you are doing often can't tell what you mean
when you say "it didn't work". 

My own experiments don't indicate anything wrong with Nigussie's approach: 

. sysuse auto
(1978 Automobile Data)

. foreach w of var price- rep78 {
  2. foreach x of var trunk-length {
  3. di "`w' `x'"
  4. }
  5. }
price trunk
price weight
price length
mpg trunk
mpg weight
mpg length
rep78 trunk
rep78 weight
rep78 length

So, what didn't work, precisely? 


nigussie Tefera

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'


*   For searches and help try:

*   For searches and help try:

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