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

st: RE: tempfile & scope


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: tempfile & scope
Date   Thu, 25 Sep 2003 10:15:42 +0100

Danielle H. Ferry
> 
> Suppose I want to create 2 temporary datasets inside a 
> -forval- loop, with
> names indexed by the loop number (i.e., `results0', 
> `results1'). Once I get
> outside the loop, I want to append them using -dsconcat-, 
> but since I
> defined them inside the loop, they no longer exist.
> 
> A more concrete example:
> 
> forval k = 0(1)1 {
> 
>     tempname memhold
>     tempfile results
> 
>     postfile `memhold' var1 var2 var3 using `results'`k', replace
> 
>     forval y = 1998(1)2002 {
>         ...
> 
>         post `memhold' (`x') (`y') (`z')
>     }
> 
>     postclose `memhold'
> }
> 
> dsconcat `results0' `results1'
> 

This issue arises from a misunderstanding 
of how local macros are defined. 

If I go 

. tempfile results 

Stata opens up a file for writing. In my 
case if I have a look I find that's a file 
in an appropriate directory with a name like 

ST_05000001.tmp

Your name will differ, but it will be of 
similar form, I imagine, modulo any style 
difference dependent on platform. That is, the local 
macro -results- will contain something like

<path leading to>/ST_05000001.tmp 

Now suppose I have local macro -k- 
and I refer to 

`results'`k' 

and -k- is variously (to follow your example) 
0 and 1. 

Stata will read this as variously 

<path leading to>/ST_05000001.tmp0

and  

<path leading to>/ST_05000001.tmp1

which happen to be legal filenames 
as far as my platform is concerned, 
so I imagine there will be no problem 
yet as far as Stata is concerned. 
(N.B. this is a guess.) 

Later you refer to local macros 
`results0', `results1'. However,  
you never defined these macros, 
so they are interpreted as empty;  
and I imagine that -dsconcat- complains 
at some point, given it is, from 
its point of view, supplied with no 
information. 

To put the matter another way, forget 
about filenames: the fallacy here 
is presuming that 

`a'`b' 

is the same as 

`a`b'' 

It could be the same in practice, 
but in principle these are quite different
things. Indeed, `a' and `b' could both 
exist, yet `a`b'' could not exist at 
all. 

1. `a'`b' 
=========

`a'`b' is interpreted this way. Stata sees 
`a', substitutes the contents of local macro 
-a- (meaning, nothing if that does not 
exist); similarly with `b'. The result 
is concatenation of the contents of two 
macros, treated as strings. 

2. `a`b'' 
=========

`a`b'' is interpreted this way. Stata sees 
nested macro names, so works from the 
inside outwards, just as with parentheses 
in elementary mathematics. Again the 
same principle applies: if one or both 
of the macro names don't exist, then 
the result will be an empty string. 

After all that explanation, a fix is relatively easy, 
although I haven't tested this: 

forval k = 0/1 {
     tempname memhold
     tempfile results`k' 
     postfile `memhold' var1 var2 var3 using `results`k'', replace
     forval y = 1998(1)2002 {
          ...
 
         post `memhold' (`x') (`y') (`z')
     }
     postclose `memhold'
}
dsconcat `results0' `results1'

However that generalises awkwardly. Suppose you wanted 
to do this for 42 values: 

forval k = 0/41 {
     tempname memhold
     tempfile new
     local files `"`files'"`new'" "'  
     postfile `memhold' var1 var2 var3 using `new', replace
     forval y = 1998(1)2002 {
          ...
 
         post `memhold' (`x') (`y') (`z')
     }
     postclose `memhold'
}
dsconcat `files'

Be clear what is happening here. As Stata goes 
round the loop, every time it meets 

	tempfile new 

It opens a _new_ file with a new _real_ name. 
That is, the meaning of the local macro -new- 
changes. So, depending on what else is going, 
it might well be, in succession, 

<path leading to>/ST_05000001.tmp 
<path leading to>/ST_05000002.tmp 
<path leading to>/ST_05000003.tmp 

and so forth. All this is no problem, 
as long as you keep a list of the files, 
adding the names as they are created. 

Nick 
[email protected] 

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