Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: Re: st: foreach

From   n j cox <>
Subject   Re: Re: st: foreach
Date   Wed, 29 Mar 2006 10:46:14 +0100

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.


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:

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