Bookmark and Share

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

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

Re: st: Working with the name of the do file

From   Robert Picard <>
To   "" <>
Subject   Re: st: Working with the name of the do file
Date   Wed, 1 May 2013 11:37:46 -0400

I have written a program that does this (get the do-file's name from
within the do-file) and much more. It is called -project- and is
currently available from my web site by typing in Stata:

net from

and clicking on the -project- link. Here's the short blurb from the
description page:

project automates the execution of do-files, skipping unchanged
do-files with unchanged dependencies (files used and created by
each do-file). All do-files are logged automatically. Stata's
current working directory is dynamically changed to match the
directory of the current do-file. Files in the do-file's
directory can always be accessed by file name while files
elsewhere can be accessed using a file path that is relative to
the project's main directory. Since full path names are not used,
the whole project directory can be moved or shared with someone
else without having to edit any file paths.

project can verify replication by comparing all files created by
the project (data, tables, graphs, log files, etc.) against
those produced by a replication run. Project files can be listed
in various ways, backed up, and shared with others. project can
also clean up the project directory by moving any file not linked
to the project to an archive directory.

A toy project that runs some examples from the version 12 Stata
reference manuals is included in the ancillary files below.
A reduced version that runs with Stata 9 is also included.

On Tue, Apr 30, 2013 at 6: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:
> -------------------
>    /// some Stata code here
> log using "step1.log", replace
>    /// more Stata code here
> display "Output from"
>    /// 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", 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 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:
> -------------------
>    /// 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 "" 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:
> *
> *
> *
*   For searches and help try:

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