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

st: AW: RE: foreach commands

From   "Martin Weiss" <>
To   <>
Subject   st: AW: RE: foreach commands
Date   Tue, 14 Apr 2009 16:33:09 +0200


In my experiment, Stata chokes on the -drop-

sysuse auto, clear
foreach var of varlist pr-rep {
	foreach x of varlist trunk-length{
		gen `var'`x'=`var'*`x'
drop `x'


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Nick Cox
Gesendet: Dienstag, 14. April 2009 16:31
Betreff: st: RE: foreach commands

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

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:

*   For searches and help try:

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