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

st: Re: Macros and -subinstr-


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: Macros and -subinstr-
Date   Mon, 26 Jul 2004 21:40:49 -0400

You should not use an equals sign in processing a long string or Stata will
evaluate it and truncate it to 80 or 244 (?) characters depending on whether
you are running SE or not.  Instead, look at macro extended functions.  In
particular there is a macro extended version of subinstr.  For your
application, it looks like you should check out the many functions available
with macro lists (help macrolists) which include the abaility to exclude the
lements of one macro from another.



Michael Blasnik
[email protected]

----- Original Message ----- 
From: "Friedrich Huebler" <[email protected]>
To: <[email protected]>
Sent: Monday, July 26, 2004 7:36 PM
Subject: st: Macros and -subinstr-


> I would like to automate the processing of some data files. First, I
> create a macro that contains a list of all data files. Then I try to
> exlude one or more files from the list. For example, let's say my
> directory contains these data files, in addition to some other files:
>
> france 2002.dta
> germany 2001.dta
> united states 2002.dta
>
> With the commands below I create a list of files and then try to
> remove the listing for the United States.
>
> . local files: dir . files "*.dta"
> . di `files'
> france 2002.dtagermany 2001.dtaunited states 2002.dta
> . local files2 = subinstr(`files',"united states 2002.dta","",.)
> . di "`files2'"
> france 2002.dta
>
> Is the way I create the list of files correct? Why does the macro
> "files2" only contain France? Finally, why do I have to use compound
> quotes to list the content of the macro "files2"?
>
> One solution I came up with is to add an -if- condition to my loop
> but I would like to understand how I can remove the United States
> from the macro "files" before the -foreach- loop.
>
> . foreach file of local files {
> .   if "`file'" != "united states 2002.dta" {
> .     ...
> .   }
> . }
>
> Many thanks in advance,
>
> Friedrich Huebler


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