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

st: Writing macro contents to a file

From   Roger Newson <>
Subject   st: Writing macro contents to a file
Date   Wed, 25 Jun 2003 17:13:20 +0100

Hello All

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


then the -file- command shown above will write, to the file, the string


(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


This string should in turn be converted by quoting in the -file- command, which should write to the file the string


which is what I wanted it to write. Unfortunately, this does not happen. For some reason, under these conditions, Stata writes to the file


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)



. file open `uhandle' using nutz.txt,write text replace

. local linecur "\textit{Make}&\textit{Weight}\\\\"

. local lencur:length local linecur

. disp `lencur'

. local linecur:subinstr local linecur "\" "\\\\", all

. local lencur:length local linecur

. disp `lencur'

. file write `uhandle' `"`linecur'"' _n

. file close `uhandle'


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

Opinions expressed are those of the author, not the institution.

* For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index