Thanks a lot Nick for the typing mistake you noticed.
I found a solution, thanks to Stefan Gawrich (thanks a lot, so much work
saved!).
I have to type this, using \\ and no quotes:
foreach x in a b c d e f g h i j k l m n o p q r s t u {
use "D:\Project\Data_anal\GSOEP\CD1984-2004\\`x'kind.dta"
sort persnr
save "D:\Project\Data_anal\GSOEP\CD1984-2004\\`x'kind.dta", replace
use "D:\Project\Data_anal\GSOEP\My analyses\temp\\`x'p_mar.dta"
sort persnr
merge using "D:\Project\Data_anal\GSOEP\CD1984-2004\\`x'kind.dta", keep
(`x'kzahl `x'hhgr)
drop _merge
save "D:\Project\Data_anal\GSOEP\My analyses\temp\\`x'p_mar.dta", replace
}
Anna
-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of n j cox
Sent: woensdag 29 maart 2006 11:46
To: statalist@hsphsun2.harvard.edu
Subject: Re: Re: st: foreach
Two further notes:
You have a typo in your alphabet: "u" appears twice, and "i" not at all.
Just use /, not \, as Stata will explain to Windows. This is documented.
Nick
n.j.cox@durham.ac.uk
Neil Shephard replied to A. Manzoni
> Is it possible to use the foreach function for part of the name of a
file or part of a variable name?
> I would like to write something similar to what below (in which there is
> something wrong, but I do not know if it is only a matter of quotes
or so or
> it is not possible to do what I want to do), where the x indicates
the wave
> so that the file has the same name just differing in the wave they were
> collected, as well as the variable sin the files, they are the same
but they
> are preceeded by a letter indicating the wave.
>
Try the following...
foreach x in a b c d e f g h u j k l m n o p q r s t u {
use "D:\Project\Data_anal\GSOEP\CD1984-2004\`x'kind.dta"
sort persnr
save "D:\Project\Data_anal\GSOEP\CD1984-2004\`x'kind.dta", replace
use "D:\Project\Data_anal\GSOEP\My analyses\temp\`x'p_mar.dta"
sort persnr
merge using "D:\Project\Data_anal\GSOEP\CD1984-2004\`x'kind.dta"
keep persnr `x'kzahl `x'hhgr
save "D:\Project\Data_anal\GSOEP\My analyses\temp\`x'p_mar_new.dta",
replace
}
This is untested as I don't have your data/directory structure, but
the potential problems I could see were
1. You don't need to need to put double quotes around the local macro x
2. The keep statement was on the same line as the merge (although this
could have been due to email gremlins)
3. There would not have been any _merge variable to drop if you had
only kept the two variables in the previous line
4. You would only have been able to run the code once as you were
over-writing your `x'p_mar.dta file each time (and removing the persnr
variable on which you wanted to merge).
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/