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]

st: Working with the name of the do file


From   Eric DeRosia <[email protected]>
To   [email protected]
Subject   st: Working with the name of the do file
Date   Tue, 30 Apr 2013 16:20:26 -0600

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/


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