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: RE: Features for Stata 14


From   "Eric A. Booth" <[email protected]>
To   [email protected]
Subject   Re: st: RE: Features for Stata 14
Date   Tue, 10 Sep 2013 10:52:02 -0500

<>
On Mon, Sep 9, 2013 at 9:55 PM, Timothy Mak <[email protected]> wrote:

> 1. A system macro that gives the directory of the executing do-file. This feature has been asked for in a number of recent posts, and it would make organizing do-files so much easier. see e.g. http://www.stata.com/statalist/archive/2013-07/msg00667.html


Hi Timothy - Though probably rarely needed,  I agree that a system
macro containing the current do file name would be useful.
FWIW, I had a situation where I needed this,  so I came up with the
kludge solution below to store the do file name.  I havent fully error
checked it, but basically it used the #review interactive command (see
-help #review-) to display the do-file name you just ran and then
store that output in a global macro for later use.  I've pasted this
program below JIC it's useful for others.


So, you need to save the following program as an .ado file, type
discard, then make sure the do-file you are running is saved
(otherwise it is just given a tempfile name), and that this program
runs (just include the command -dofilename-) as the first line in your
do-file.  ((Really, you should be able to put the -dofilename-
anywhere in the file since the last command you ran in the 'command
window' for Stata was to -do- that dofile ...but I put it at the top
just to be sure))  Those are some pretty inconvenient
limitations/requirement for some, but it worked for what I needed.



*********************! save below as  dofilename.ado
cap program drop dofilename
program def dofilename
  syntax

  *-write #review to a do file**
cap file close j1
tempfile j2
file open j1 using `j2'.do, replace write
file write j1 `" #review 4 "' _n
file close j1

  *-run j2 to get the #review output
   tempfile t
   cap log close _t
   qui log using `t', replace text name(_t)
      do `"`j2'.do"', nostop
   log close _t

  *-parse the j2 file output to get the .do file name
insheet using `t', clear nonames
tempvar i
g `i' = 1 if index(v1, ".do")
cap keep if `i'==1
cap keep in 1
if _N!=1 di as err "Make sure your current dofile is saved, and run
-dofilename- as first command in file!"
global dofilename `"`=v1[1]'"'
end

****************************! END


then run something like this in your do-file:


*********************! sample do-file
dofilename

sysuse auto, clear
desc, sh
...
di `"${dofilename}"'
*********************! sample do-file

HTH,

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


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