Statalist


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

st: Re: Generating tempfiles within a program for later use


From   "P. Wilner Jeanty" <[email protected]>
To   [email protected]
Subject   st: Re: Generating tempfiles within a program for later use
Date   Mon, 7 Jan 2008 19:31:25 -0500

On Jan 7, 2008 3:04 PM, David Kantor <[email protected]> wrote:
> At 02:17 PM 1/7/2008, P. Wilner Jeanty wrote:
> >On Jan 6, 2008 4:40 PM, Beth Gifford <[email protected]> wrote:
> > > Hello all
> > >
> > > I have a data base with many tables (they represent students within
> > > schools and then provide additional details on students).
> > > What I want to do is to have a program for each  table ( that labels &
> > > recodes variables, etc) and that then creates a tempfile with a
> > > dataset that I can use to do the merging.  I'm very close but the
> > > tempfile gets overwritten when the next program runs.
> > > [...]
> >
> >What Beth got as result is legitimate. It is not that the school
> >tempfile is overwritten by the student tempfile, but the school
> >tempfile vanishes itself. To be plain, tempfiles cannot be created
> >within a program for later use by another program. Anything that is
> >Temp disappears once the program execution is complete. They are
> >available for use only within the program that creates them (correct
> >me anyone if I am wrong); meaning once created, if they are not used,
> >they will be gone.
> >[...]
>
> I'm not sure what the cause of Beth's problem is, but I believe that
> the assessment given by P. Wilner Jeanty was not correct.
> I have used a scheme similar to Beth's many times with much success.
> I believe that the tempfile is erased when the context that defined
> the local is exited -- not when the context that saved the file is
> exited. Someone correct me if I am wrong.
>
> --David

Thanks David. Maybe I was not clear enough. I think providing an
example and adding the word "code" would probably make me clear and
make you happy. When the program code (what is submitted to Stata)
creating the tempfiles finishes executing, if tempfiles are not used
they get vanished for good. Note the difference in the output produced
by the two pieces of program "code" below. The first one produces a
result similar to Beth's.

On Jan 7, 2008 3:08 PM, Austin Nichols <[email protected]> wrote:
> Beth Gifford <[email protected]>:
> Can you verify that the following experiment works on your system?  It
> sounds like there is a problem in picking tempfile names.
>
> program define p_school, rclass
>  clear
>  range x 0 1 2
>  sort x
>  save `0'
> end
> program define p_student
>  clear
>  range x 0 1 2
>  sort x
>  save `0'
> end
> tempfile d_school
> p_school `d_school'
> tempfile d_student
> p_student `d_student'
> merge x using `d_school'

Austin - As can be seen below, there is no problem in picking up
tempfile names. You have in each dataset only one variable (x) on
which you based the merge. So the output dataset should contain x and
_merge as variables.

** Begin example 1 *****
capture program drop p_school
program define p_school
 clear
 range x1 1 5 5
 gen id=_n
 sort id
 save `0'
end
capture program drop p_student
program define p_student
 clear
 range x2 .5 1 5
 gen id=_n
 sort id
 save `0'
end
tempfile d_school
p_school `d_school'
tempfile d_student
p_student `d_student'

**End example 1 *****
list

	+-----------+
	x2   id
	-----------
1.	.5    1
2.	.625    2
3.	.75    3
4.	.875    4
5.	1    5
	+-----------+
The first tempfile got vanished. Again, it is "not" that it got
overwritten by the second tempfile. And if David was successful in
using a tempfile already vanished  I would like to know how.


***Begin example 2 ***
capture program drop p_school
program define p_school
 clear
 range x1 1 5 5
 gen id=_n
 sort id
 save `0'
end
capture program drop p_student
program define p_student
 clear
 range x2 0.5 1 5
 gen id=_n
 sort id
 save `0'
end
tempfile d_school
p_school `d_school'
tempfile d_student
p_student `d_student'
merge id using `d_school'  // tempfiles put to use

** End example 2 ***

list

+-------------------------+
x2   id   x1   _merge
-------------------------
1.    .5    1    1        3
2.  .625    2    2        3
3.   .75    3    3        3
4.  .875    4    4        3
5.     1    5    5        3
+-------------------------+

This time the first tempfile was used before it got vanished.

Therefore, I still believe that Beth's school temporary file got vanished not
overwritten by the student temporary file. Again, please correct me if
I am still wrong.

-- 
P. Wilner Jeanty, Post-doctoral researcher
Dept. of Agricultural, Environmental, and Development Economics
The Ohio State University
2120 Fyffe Road
Columbus, Ohio 43210
(614) 292-6382 (Office)
*
*   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