Statalist


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

st: RE: AW: RE: foreach commands


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: AW: RE: foreach commands
Date   Tue, 14 Apr 2009 15:41:45 +0100

Good catch. I didn't replicate that bit of code. 

Nick 
n.j.cox@durham.ac.uk 

Martin Weiss

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'
}
*************

Nick Cox

A few days ago, I posted some generic advice within 

<http://www.hsph.harvard.edu/cgi-bin/lwgate/STATALIST/archives/statalist.090
4/date/article-217.html> 

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:
*   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