<>
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'
}
*************
HTH
Martin
-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Dienstag, 14. April 2009 16:31
An: [email protected]
Betreff: st: RE: foreach commands
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?
Nick
[email protected]
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/
*
* 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/
*
* 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/