Statalist


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

Re: st: Modify estimation results


From   n j cox <n.j.cox@durham.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Modify estimation results
Date   Fri, 27 Jul 2007 14:38:23 +0100

For completeness, note that -estout- is a program from
SSC.

Other postings pointed out a way of doing this with
-estout-, so Hendri is happy.

I read Hendri's problem and posed myself a closely related problem
as follows:

1. You have a text file of stuff. Or you want to create one.

2. You want to add a line while you are working in Stata.
(This will, naturally, be the first line in the file if the
file is new.)

3. You want to automate this. (If this does not apply, the
answer surely is to open up a text editor.)

4. The solution should be really easy, and be no more
than a single command line.

Just being able to add a line is a little deal, except that
you can do that repeatedly.

I may be missing something, but I couldn't think of an existing
solution. -postfile- doesn't qualify. Hence a file utility (futility?),
which is a kind of immediate command, called -filei-.

------------------------------------------------ filei.ado
*! NJC 1.0.0 27 July 2007
program filei
version 8.2

// syntax can be
// filei + "<text>" "<filename>" create or append
// filei - "<text>" "<filename>" prepend
// quotes can be omitted if there are no embedded spaces

tokenize `"`0'"'
if `"`2'"' == "" | `"`4'"' != "" error 198

if `"`1'"' == "+" {
capture confirm file "`3'"
local exists = _rc == 0
local file "`3'"
}
else if `"`1'"' == "-" {
confirm file "`3'"
local file "`3'"
local exists 1
}
else error 198

tempname ho hi

if !`exists' {
file open `ho' using "`file'", w
file write `ho' `"`2'"' _n
file close `ho'
exit 0
}
else {
if "`1'" == "+" {
file open `ho' using "`file'", w append
file write `ho' `"`2'"' _n
exit 0
}
else {
tempfile work
file open `ho' using `work', w
file write `ho' `"`2'"' _n
file open `hi' using "`file'", r
file read `hi' line

while r(eof) == 0 {
file write `ho' `"`macval(line)'"' _n
file read `hi' line
}

file close _all
copy `work' "`file'", replace
}
}
end
---------------------------------------------------

Look inside the file for syntax. Or, examples tell you all you need to know:

. filei + "stuff" filename.txt

. filei + "more stuff" filename.txt

. filei - "earlier stuff" filename.txt

. type filename.txt
earlier stuff
stuff
more stuff

. filei - "earlier stuff" "new filename.txt"
file new filename.txt not found
r(601);

The syntax is of course arguable. While + should make
sense as adding a line (at the end, which might be the
same as the beginning), - might be difficult
to accept as its opposite, adding at the beginning if
and only if the file already exists.

There might be a case for other features, but there is a very long
slippery slope in sight, of creating yet another text editor.

Nick
n.j.cox@durham.ac.uk

Hendri Adriaens

I would like to know if it is possible to modify estimation results when
storing them in memory. I do for instance:
sysuse auto
regress price mpg weight
estimates store temp
estout temp using c:\results.txt, cells("b se t p") replace

But I would like the results.txt to contain another line, with some
arbitrary text, for instance a comment. It exports something like:

temp
b se t p
mpg -49.51222 86.15604 -.5746808 .5673237
weight 1.746559 .6413538 2.723238 .0081298
_cons 1946.069 3597.05 .541018 .5901886

But I would be able to write

temp
% This is a test model
b se t p
mpg -49.51222 86.15604 -.5746808 .5673237
weight 1.746559 .6413538 2.723238 .0081298
_cons 1946.069 3597.05 .541018 .5901886

Would it be possible? And how?
*
* 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index