Get the path

December 7, 2009 at 5:26 am 7 comments

| Gabriel |

When you’re scripting (whether in Stata or anything else) you need to tell the script where to look for things by giving it a directory path. As previously mentioned, I think it’s a good idea to treat the path as what Stata calls a “macro” and most other languages call a “variable.” That way you can define the path at the beginning of the script and if you later decide to change the target path you can change the one macro/variable rather than combing through the script looking for each instance.

Of course, this assumes that you know what the path is, which can be hard to remember if it’s a long path. There are a few ways to get it.

From within Stata, the local `c(pwd)' holds the current path and this info is also displayed in the interactive mode interface (in the toolbar on a Mac, at the bottom of the main window on Windows).

TextWrangler has a “copy path” feature in “get info”.

From the Mac Terminal you can get the path in the clipboard with pwd | pbcopy

In Snow Leopard, you can also do it as a Finder service. Follow these instructions, except substitute this shell script:

sed -e 's/:/\//g' -e 's/\ /%20/g' -e 's,[^/]*$,,' | pbcopy


Entry filed under: Uncategorized. Tags: .

Drop the entirely missing variables Links


  • 1. Matissa  |  December 7, 2009 at 11:54 am

    You can also use Stata’s menus to get the path of any file. Under the file menu choose “filename…”. This will bring up a file browsing window where you can navigate to the file you want (by default, Stata only displays .dta files in this window, but you can change the file type to other types or all files in the “file type” dropdown list). Once you select the file and click open, it pastes the full path into the command window of Stata, which you can then copy and use how you wish.

    This feature is particularly nice for getting paths without having to change the working directory. One of the few Stata menu options that I use frequently.

    The only issue with this is that recently I’ve been switching back and forth between Windows and Unix. In Windows this command gives the path with backslashes (\) while Unix only recognizes forward slashes (/). To make things more portable I just do a search and replace of all backslashes.

    • 2. gabrielrossman  |  December 7, 2009 at 2:48 pm

      if that works for you, great, but personally I’d find it easier to just type “pwd”. another advantage of using the command line is that you could write an ado file that saves you the step of changing the dirsep. here’s an example, just save it to your ado directory and you can type “pwdf” (the f is for “forward slash”)

      program capture drop pwdf
      program define pwdf
      disp subinstr("`c(pwd)'","\","/",.)

      • 3. gabrielrossman  |  December 7, 2009 at 2:55 pm

        ps, i see that i didn’t read your comment carefully enough, you’re interested in a) filenames and b) directories other than pwd. i agree that your technique is probably the most effective since i think the GUI is usually more efficient for browsing the file system than is the command line

  • 4. Eric A. Booth  |  December 7, 2009 at 11:29 pm

    Hi Gabriel:

    A couple of things I find useful:

    1. you can drag any file from Finder into the “Command” window in Stata & it will give you the path to copy/paste ((though I probably use the Text Wrangler “get path” function more often, this one comes in handy))

    2. I like to set global macros to keyboard shortcuts to start file paths for me. For instance, I set my main folder on my HD to the F5 key via:

    global F5 “/users/ebooth/documents/stata/”

    and F6 to a place on the network mounted drive like:

    global F6 “/volumes/Odrive/projects/code_repository/stata/”

    I put these two commands in my file.
    Then, if you are in the “Command” window, you can press F5 or F6 to get this path, if you still can’t remember the file you are looking for you can use this as a place to start by typing “grep file, path( )” or you can use “findfile” &/or some of the -dirtools- in the same way.

    If you want to call the global F5 or F6 file path from a do-file, just insert “$F5” into the command, like:
    cd “$F5/file/folder//”
    save “$F5/file/data.dta”, replace

    3. In your previous comment you use -subinstr()- to change the slashes for Windows vs. *nix environment, I also like inserting this at the top of my do-files to deal with the fact that I often use the do-files across platforms, this will define your file path macros for the dofile:

    if “`c(os)'” == “MacOSX” {
    local originaldata “/users/ebooth/documents/stata/odata//”
    local cleaneddata “/users/ebooth/documents/stata/cdata//”
    local analyses “/users/ebooth/documents/stata/analyses//”
    local network “/volumes/Odrive/projects/code_repository/stata/”
    if “`c(os)'” == “Windows” {
    local originaldata “Z:\ebooth\odata\\”
    local cleaneddata “Z:\ebooth\cdata\\”
    local analyses “Z:\ebooth\analyses\\”
    local network “O:\projects\code_repository\stata\\”

    then you can reference your paths without worrying, example:

    use “`originaldata’mydata.dta”, clear

    tabform x y using “`analyses’table1.xls”, by(z)

    esttab using “`network’/table2.rtf”, ///
    cell(” count “) varlabels(`e(labels)’) rtf

    save “`cleaneddata’mydata_clean.dta”, replace

    This saves me a lot of headache, especially when I pass these do files on to other colleagues in my department.

    4. If you cannot readily find the file & path you are looking for on Mac OSX, you can search the metadata sqlite server (the backdrop to spotlight, though this approach is more comprehensive) from Stata and grab the path you need:

    !mdfind “some string here”

    then copy and paste the path you need. Keep the posts coming, I enjoy the blog.

    ~ Eric

    • 5. gabrielrossman  |  December 8, 2009 at 3:19 pm

      wow, thanks for the thorough, clear, and useful advice.

  • 6. Sofie  |  March 2, 2011 at 9:32 am

    I have a Stata program (.ado) that calls different .do-files. If there is a bug in the program, it is never clear in which .do-file I have to look to fix the error.
    Does anyone know of a way to retrieve the path+filename of the file that Stata was last executing?


    • 7. gabrielrossman  |  March 2, 2011 at 9:43 am

      when i have a problem like this i usually write in a series of “disp” commands. the last one to be displayed before Stata crashes is the last good point in the code.

      good luck.

The Culture Geeks

%d bloggers like this: