Statalist The Stata Listserver


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

Re: st: foreach


From   "Neil Shephard" <nshephard@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: foreach
Date   Wed, 29 Mar 2006 15:24:34 +0800

On 3/29/06, A. Manzoni <a.manzoni@uvt.nl> wrote:
> 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).

Neil
--
"The best safety device in climbing is always situated between your
ears" - Ross Weiter, Perth Rock Climbing Guide (2002)

Email - nshephard@gmail.com / neilshep@cyllene.uwa.edu.au
Website - http://slack.ser.man.ac.uk/
Blog - http://slack---line.blogspot.com/
Flickr - http://www.flickr.com/photos/slackline/

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



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