Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Re: Macros and -subinstr-


From   Nick Winter <[email protected]>
To   [email protected]
Subject   Re: st: Re: Macros and -subinstr-
Date   Tue, 27 Jul 2004 10:44:44 -0400

At least part of the problem here is the way you are checking the contents of the local macro files ... the -dir- macro command encloses the file names in quotation marks, but you are not seeing those. Try:

. local files : dir . files "*.dta"

. di `"`files'"'
"france 2002.dta" "germany 2002.dta" "united states 2002.dta"

. local files2 : subinstr local files `""united states 2002.dta""' ""

. di `"`files2'"'
"france 2002.dta" "germany 2002.dta"

. local files3 : subinstr local files "united states 2002.dta" ""

. di `"`files3'"'
"france 2002.dta" "germany 2002.dta" ""


In your original, you are stripping out the "united states 2002.dta", but leaving behind the quotation marks that enclose it.

In line with Nick (Cox)'s suggestion (see -help macrolists-), I would do it this way:

. local files : dir . files "*.dta"
. local toskip `""united states 2002.dta""'
. local files2 : list files - toskip

This is then easily extendable to skip two countries, just by defining toskip differently:

. local toskip `""united states 2002.dta" "france 2002.dta""'

--Nick (Winter)

At 06:19 AM 7/27/2004 -0700, you wrote:


Thank you Michael and Nick. I will have a look at -fs- but first I
want to understand -subinstr-. I tried another way to strip "united
states 2002.dta" from the file list. However, when I pass this macro
to a loop I now get an error message because the macro "files2" still
contains three items, the last of which appears to be a blank.

. local files: dir . files "*.dta"
. di `files'
france 2002.dtagermany 2001.dtaunited states 2002.dta
. local files2: subinstr local files "united states 2002.dta" ""
. di `files2'
france 2002.dtagermany 2001.dta
. foreach file of local files2 {
  2. di "`file'"
  3. use "`file'", clear
  4. }
france 2002.dta
germany 2001.dta

invalid file specification
r(198);

Notice the blank line before "invalid file specification". If I
switch the second and third lines of the -foreach- loop, the blank
line does not appear.
--------------------------------------------------------
Nicholas Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government [email protected] e
308 White Hall falcon.arts.cornell.edu/nw53 w
Cornell University
Ithaca, NY 14853-4601

*
* 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index