Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: to preserve locals while debugging


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: to preserve locals while debugging
Date   Fri, 19 Mar 2010 09:15:11 -0400


Two ideas. One is to put the locals in a separate .do file
which you can -include- ; see the brilliantly outlined details
here

   http://www.stata-journal.com/article.html?article=pr0047

Another solution is to use something like this bit of code which
will write out all of the current locals to a file which you
can then -include- in order to recover them.


*------------- save locals ------------------
capture log close
log using mylocals.txt, text replace
macro list
log close
file open logfile using mylocals.txt, text read
file open mylocals using mylocals.do, text replace write
file read logfile line
while r(eof)==0 {
	local lname : word 1 of `line'
	if strpos("`lname'","_")==1 {
		local lval  : subinstr local line "`lname'" ""
		local lval=trim("`lval'")
		local lname : subinstr local lname "_" ""
		local lname : subinstr local lname ":" ""
		file write mylocals "local `lname' " _char(34) "`lval'" _char(34)  _n
	}
	file read logfile line
}
file close logfile
file close mylocals
*----------- done --------------------------

hth,
Jeph



[email protected] wrote:
Hi Statalisters,

I'm looking for a way to preserve locals while writing or debugging a
do-file . An example: I create a series of graphs in a loop. Graph parameters are provided by locals which are set inside the loop or somewhere else in the do-file.

Graph layout sometimes needs a lot of fine-tuning and visual checks.

Before running the graph again I have to redefine locals either by running the whole do-file or by temporarily defining all locals just above the graph command. Both alternatives are not very convenient.

I think of a command that stores all locals in a safe place (copy to global or something else) and breaks execution of the do file.

A complementary command restores the locals and makes them available for
the graph.

Anything already available or any ideas (beside "use globals")? ;-)

Stefan Gawrich



Simple code example
sysuse auto, clear
local filename "auto.dta"
local foreign = 1
forval x = 1/5 {
lpres // preserves locals for the session and breaks at run 1 ...
}


To run while working on the graph:

lrest // restores locals
hist length if foreign == `foreign' & rep78 == `x', ///
title("`filename'")
}






*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index