Relative paths are handled differently by Stata, R, and Matlab. Although some sample code is given below for your benefit, it is not the ONLY way to do relative paths. In fact, there may actually be better ones. But this will get you started.
Stata is the easiest of the 3 software to deal with relative paths. Nothing special must be done. Simply add the relative path to the file name when working with files in your .do file. See the following examples:
import excel subfolder/data.xlsx, sheet("Sheet1")
import excel subfolder/anotherfolder/data.xlsx, sheet("Sheet1")
import excel ../../folderhere/data.xlsx, sheet("Sheet1") this goes up two directories, then into a subfolder "folderhere"
Download Stata sample files
Matlab requires a little work in order to effectively use relative paths (to the executing script). To do this:
currentFile = mfilename( 'fullpath' );
[pathstr,~,~] = fileparts( currentFile );
inputfilename = fullfile( pathstr, 'raw/in-data.txt' );
from pathlib import Path
path = Path(__file__).parent / "data/transactions.csv"
R, unfortunately, is the most difficult to use with relative paths. This is because everything is relative to the Working Directory. There isn't a reliable cross-platform method to determine the file path of the executing script.One workaround, is to define an absolute path in a variable, set the Working Directory to that path, and then make all subsequent paths in the script relative to it. This single line of code will need to be changed if the file is executed in different locations. However, you can use a method of commenting in order to make it easier to deal with that situation.
# uncomment for mapped drive
#rootPath <- "Z:"# uncomment for LINCOMM
rootPath <- "/mnt/aae/users/jdoe
# uncomment for Mac (typical)
#rootPath <- "/volumes/aae/users/jdoe
setwd(paste(rootPath, "/MyRfiles", sep=""))
dt1 <- read.csv("./import/data.csv")