# Working with GitHub and Stata

## The problem and the solution

Among the several ways available to load a program in Stata, the one that I like the most is to add the directory of my ado-file to the search path used by Stata, which is stored in the global macro S_ADO. This procedure is easily done by using the command adopath ++. I like this method because after I have modified and saved my ado-file, I only need to type discard and then run my code. Other methods require using program drop name-of-ado and/or running the whole ado from the text editor each time it is modified. One inconvenience of the adopath ++ approach is that the more ado-files you’re working on, the more tedious it becomes to add those directory paths to the search path used by Stata.

However, you may adapt and include the code bellow in your profile.do file in order to load all your ado-working directories into Stata (see my post about profile.do). In this way, you can open your ado-file directly using adoedit (Dan Blanchette 2012) and, more importantly, redirect Stata to load your version of the ado-file rather than the one stored in c:/ado/plus/. Some people may ask, why don’t you use the c:/ado/personal/ directory, which was intended for personal ado-files? I certaintly use the /personal/ directory, but I only use it for ado-files that I don’t intend to share with anybody. If I develop a program, I do it in conjuction with GitHub and in a different directory.

To modidy the code bellow, you only need to define the root directories in which you store your ado-files. I have three directories: one for the ado-files I develop jointly with my team at work; another one for my contributions to other people’s ado-files in Github; and another one for my own ado-files. You may add as many as you want, as long as you add the directory path in a new local macronamed adodir#, where # refers the subsequent number to the previous directory path.

* set adopaths for working with Github and Git

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

local dirs: dir "adodir'" dir "*"

gettoken dir dirs : dirs
while ("dir'" != "")  {
local files ""
if regexm("dir'", "^\.git|^_") { // exclude .git folders and those that start with _
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
}
}


## Explanation of the code

The code above searches for and adds to the search path of Stata all the subdirectories that contain .ado or .sthlp files within the directories specified in the locals adodir#. Given that the number of subdirectories is unknown, the magic of the code above resides on searching within the next directory using gettoken dir dirs : dirs rather than using a foreach loop. Then, each time Stata find a new directory to search into, it is added to the local dirs like this local dirs "dirs' dir'/subdir'". Then the while will stop when the local dir` is empty.

## References

Dan Blanchette. 2012. “ADOEDIT: Stata Module to Edit Ado File in Stata’s Do-File Editor.” Boston College Department of Economics. https://ideas.repec.org/c/boc/bocode/s430401.html.

###### Economist/Data Scientist

My research interests include … matter.