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 Booth <[email protected]>
To   [email protected]
Subject   Re: st: RE: Features for Stata 14
Date   Tue, 10 Sep 2013 12:02:47 -0500

<>

Ahh, I havent used -project- before nor seen your previous message about this (also when I was looking for this solution -project- didn't exist or else this would have saved me some time).  
Thanks Robert.

- Eric

On Sep 10, 2013, at 11:57 AM, Robert Picard <[email protected]> wrote:

> And apologies for repeating myself but -project- (SSC) can return both
> the name of the current do-file AND the path to the directory that
> contains the current do-file. When do-files are nested, the concept of
> the currently running do-file is ambiguous as there can be more than
> one do-file running at the same time. To correctly track the path to
> the "top-most" do-file, you have to maintain a LIFO stack and update
> it as a new do-file starts and when it exits. That's what -project-
> does (and much more).
> 
> Of course StataCorp could track each -do- and -run- call and implement
> the same functionality.
> 
> On Tue, Sep 10, 2013 at 12:09 PM, Eric A. Booth <[email protected]> wrote:
>> <>
>> 
>> An important note on my previous post --
>> I took out a preserve/restore in the last part of my .ado file before
>> I pasted it to the list b/c I was looking at what it was doing, but
>> that can destroy data in memory if you used it later in your do-file
>> (which I caution against anyways), so you can put the preserve/restore
>> back in by using the ado below instead.
>> 
>> 
>> Also, the reason I use -file write- to save the one command -#review
>> 4- and then run it separately, is that when I tried to just include
>> #review in the program and log it's output it was not logging
>> properly.  So a quick solution for me at the time was to save it in a
>> separate .do file and then run that file and log the output.  There's
>> likely a faster/better way to do all this , but it served it's purpose
>> for me at the time.
>> 
>> 
>> *********************!updated  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
>> 
>> preserve
>> 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]'"'
>> restore
>> 
>> end
>> ***************************! END
>> 
>> - Eric
>> 
>> On Tue, Sep 10, 2013 at 10:52 AM, Eric A. Booth <[email protected]> wrote:
>>> <>
>>> 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/
> 
> *
> *   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/


*
*   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