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: Working with the name of the do file


From   Steve Samuels <[email protected]>
To   [email protected]
Subject   Re: st: Working with the name of the do file
Date   Wed, 1 May 2013 07:50:40 -0500

Eric:

Use a text editor that does Stata syntax coloring and that makes the
file name accessible. In OS X, I use BBEdit (barebones.com), which has a
built-in clipping facility. The base-name of the current file is held in
the substitution placeholder #BASENAME#.

I created a clipping with the following lines and assigned it a KB
shortcut-it also appears in the clipping menu.

********************************
capture log close
local pname #BASENAME#
log using `pname', replace
********************************

When the clipping is inserted into, say, pg04.do, the text becomes:

********************************
capture log close
local pname pg04
log using pg04, replace
********************************

I use the macro to put the program name into graphs and
into data set characteristics. A version without the macro is: 

**********************************
capture log close
log using #BASENAME#, replace
**********************************

Steve




On Apr 30, 2013, at 5:20 PM, Eric DeRosia wrote:

I want to make my Stata do files easier to use and maintain.  Is there a way to access the name of the do file from within the do file itself, perhaps as a macro or a system variable?  Let me give an example of what I mean.  I have a do file such as the following:

filename: step1.do
-------------------
 /// some Stata code here
log using "step1.log", replace
 /// more Stata code here
display "Output from step1.do"
 /// more Stata code here
graph export "step1.emf", replace as(emf)


In this example, if I want to re-use the code by copying the file and creating "step 2.do", my current workflow process is to edit the file and replace all the instances of "step1" in the file with "step2". Beyond the obvious inconvenience of editing the file every time I re-use the code, I occasionally forget to make the change, so running step2.do accidentally overwrites step1.log, etc.  I'm looking for a better way.

I think something like the following would solve my problem.  Is there a way to accomplish this in Stata?


filename: step1.do
-------------------
 /// some Stata code here
log using "`_name_of_the_do_file'.log", replace
 /// more Stata code here
display "Output from `_name_of_the_do_file'.do"
 /// more Stata code here
graph export "`_name_of_the_do_file'.emf", replace as(emf)

In the code above, I made up "`_name_of_the_do_file'" out of thin air to illustrate what I would like to do.  My hope is to find a string that resolves to "step1.do" based on the filename.  (In this example, I didn't deal with the fact that ".do" that would be at the end of the string, but it's clear to me how I would deal with that once I had access to the filename.)  Is there any way for Stata to get access to the name of the do file?

In case it matters, I'm using Stata 12 in a Windows environment.

Thank you in advance for any help you can offer on this!

- Eric

-- 
Eric DeRosia, Ph.D.
Assistant Professor
Business Management
Marriott School of Management
Brigham Young University
671 TNRB
Provo, Utah 84602

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