A query re writing the contents of a Stata macro to a file. I have been
trying to do this by using -file write-, with a file open for writing as an
ASCII file, as I have not found a way of writing macro contents to a
binary file. For instance, if the macro is a local macro named -linecur-,
and the file handle is stored in another local macro named -uhandle-, then
I might write
file write `uhandle' `"`linecur'"' _n
to write the contents of the macro -linecur- to the file whose handle is
stored in -uhandle-.
There is one problem with this approach, namely that it involves quoting.
When Stata quotes a string, it substitutes each occurrence of the substring
\\ (double backslash) with a \ (single backslash). This will cause problems
if my macro -linecur- contains double backslashes. For instance, if
-linecur- contains the string
\textit{Make}&\textit{Weight}\\
then the -file- command shown above will write, to the file, the string
\textit{Make}&\textit{Weight}\
(note that the double slash at the end is now single), and this will cause
problems if the file being written is intended to be a LaTeX document.
I therefore decided to try using the -subinstr- macro extended function. If
I write
local linecur:subinstr local linecur "\" "\\\\", all
file write `uhandle' `"`linecur'"' _n
then I might expect all single backslashes in the value of -linecur- to be
replaced with double backslashes (because the quadruple backslash \\\\ has
been reduced by quoting to a double backslash \\), causing -linecur- to
contain the string
\\textit{Make}&\\textit{Weight}\\\\
This string should in turn be converted by quoting in the -file- command,
which should write to the file the string
\textit{Make}&\textit{Weight}\\
which is what I wanted it to write. Unfortunately, this does not happen.
For some reason, under these conditions, Stata writes to the file
\\textit{Make}&\textit{Weight}\\
which I did not want. (Note the extra slash at the beginning of the string.)
Why should this be the case? And does anybody out there know of a better
way to output the contents of a macro to a file? I am using Stata 8 under
Windows 98 SE. The full log of what I did is listed below.
Best wishes (and thanks in advance)
Roger
*BEGINNING OF STATA LOG - CUT HERE!!!!
. file open `uhandle' using nutz.txt,write text replace
. local linecur "\textit{Make}&\textit{Weight}\\\\"
. local lencur:length local linecur
. disp `lencur'
31
. local linecur:subinstr local linecur "\" "\\\\", all
. local lencur:length local linecur
. disp `lencur'
35
. file write `uhandle' `"`linecur'"' _n
. file close `uhandle'
*END OF STATA LOG - CUT HERE!!!!
--
Roger Newson
Lecturer in Medical Statistics
Department of Public Health Sciences
King's College London
5th Floor, Capital House
42 Weston Street
London SE1 3QD
United Kingdom
Tel: 020 7848 6648 International +44 20 7848 6648
Fax: 020 7848 6620 International +44 20 7848 6620
or 020 7848 6605 International +44 20 7848 6605
Email: [email protected]
Website: http://www.kcl-phs.org.uk/rogernewson
Opinions expressed are those of the author, not the institution.