Stata profile.do: nice tips

Each time you start a new Stata session, Stata looks for two files, sysprofile.do and profile.do. If these finds are found, Stata executes their contents. This post is about how to modity profile.do, in which you can put anything you want Stata to do before you start working with it.

From time to time, I upload my profile.do into my GitHub repository (randrescastaneda/Stata_profile), so you can check it out and download it for your own purposes. Alternatively, I provide my current version below (April, 2019), and a short explanation of what it does.

First, I set up some Stata parameters such as the default schem of by graphs, the fonds, and the display formatting of the results window.

Second, change the behavior of the F keys in my keyboard. In my case, I like to work with the command pause for debugging my ado-files, so I modifye F2 to execute and end and F9 to execute BREAK. You may learn more about how to use pause in my post Efficient debugging: using pause in ado-files. I also use F5 and F6 to change my current directly, but I recently learned about the new command savecd by Scott Long (2018),1 which might be a good alternative to the F keys.

Third, I set some globals that are either necessary for other programs or for my own work. Then, I open some programs that I use while working in Stata. In particular, I use Total Commander for file management and NotePad++ for editing my Stata scripts. In my post My Stata editor: NotePad++ I explain the features of this amazing text editor.

Fifth, I make a rule for setting the current directory depending on whether or not I am connected to the intranet of my job. Finally, I set all the differetn adopaths of the ado-files in which I contribute in Github or bare repositories internally in my job. I explain this final procedure in my post Working with GitHub and Stata.

I found this post very interesting in Statalist.org about userful tips for the profile.do.

/*===========================================================================
project: profile
Author: Andres Castaneda 
---------------------------------------------------------------------------
Creation Date: September 2, 2014 
===========================================================================*/

/*==============================================================================
    Program set up 
==============================================================================*/

set matsize 2000
set more off, permanently 
set r on, permanently                         // display execution time
* set scheme plotplain, permanently
set scheme plotplainblind, permanently       // nice scheme for graphs avaialable in ssc
graph set window fontface "Times New Roman"  // font for graphs
set checksum off, permanently                // to clean api queries
set tracenumber on, permanently              // numbers on trace
set tracedepth 3  

/*==============================================================================
    shortcuts
==============================================================================*/

global F2 `"end;"'  // to work with pause
global F4 `"disp _dup(20) "-" "End of section" _dup(20) "-" _n(200) _dup(20) "-" "New section" _dup(20) "-";"' // clear results windows
global F5 `"cd "c:\Users\wb384996\OneDrive - WBG\temp\stata";"'   // cd 1
global F6 `"cd "x:\01.personal\wb384996\temporal\stata";"'        // cd 2 when working on network drive
global F9 `"BREAK;"'                                              // working with pause

/*==============================================================================
    Globals
==============================================================================*/

global google_api ""    // store my api keys

* interaction with R
global Rterm_path    `"c:\Program Files\R\R-3.4.0\bin\x64\Rterm.exe"'
global Rterm_options `"--vanilla"'

/*==============================================================================
    Open Important programs
==============================================================================*/

* winexec "C:\Program Files\Microsoft Office 15\root\office15\OUTLOOK.EXE"  // start outlook
* sleep 5000

winexec "C:\Users\wb384996\Documents\Totalcmd\TOTALCMD64.EXE"   // start total commander
sleep 5000

winexec "C:\Program Files\Notepad++\notepad++.exe"     // start notepad ++
global MYEDITOR winexec notepad++

* winexec "c:\Users\wb384996\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Axosoft, LLC\GitKraken.lnk" // start GitKraken

/*==============================================================================
    Current directory depending on my Intenert connection
==============================================================================*/

* set cd depending on connectivity
cap cd "x:\01.personal\wb384996\temporal\stata"
if (_rc) {
    disp in g "you're not connect to the intranet." _n ///
        "Current directory set to:" _n                   ///
        in y "c:\Users\wb384996\OneDrive - WBG\temp\stata\"
    cd "c:\Users\wb384996\OneDrive - WBG\temp\stata\"
}

/*==============================================================================
    Working directories in GitHub
==============================================================================*/

* set adopaths for working with Github and Git
local adodir1 "c:\Users\wb384996\OneDrive - WBG\GTSD\02.core_team\01.programs\01.ado"
local adodir2 "c:\Users\wb384996\OneDrive - WBG\ado\github_contr"
local adodir3 "c:\Users\wb384996\OneDrive - WBG\ado\myados"

local i = 1 
while ("`adodir`i''" != "") {
    local adodirs  `"`adodirs' "`adodir`i''" "' 
    local ++i
}

foreach adodir of local adodirs {
    
    local dirs: dir "`adodir'" dir "*"
    
    gettoken dir dirs : dirs
    while ("`dir'" != "")  {
        local files ""
        if regexm("`dir'", "^\.git|^_") {
            gettoken dir dirs : dirs
            continue
        }
        
        local ados: dir "`adodir'/`dir'" files "*.ado"
        local help: dir "`adodir'/`dir'" files "*.sthlp"
        local files = `"`ados'`help'"'
        
        if (`"`files'"' != "") {
            qui adopath ++ "`adodir'/`dir'"
            disp "`adodir'/`dir'"
        }
        
        local subdirs: dir "`adodir'/`dir'" dir "*"
        
        if (`"`subdirs'"' != "") {
            foreach subdir of local subdirs {
                if regexm("`subdir'", "^\.git|^_") continue
                local dirs "`dirs' `dir'/`subdir'"
            }       
        }
        gettoken dir dirs : dirs
    }
}



exit
/* End of do-file */

Scott Long. 2018. “Commands for Changing the Working Directory.” The Stata Journal Draft (August).


  1. This seems to be a forthcoming paper, but it has not been announce yet in the STata Journal. Yet, this is link to the pdf.

Economist/Data Scientist

My research interests include … matter.