Your cart is currently empty!
I have written before about using c-locals in Stata that contain information on the operating system and other stuff. In some (maybe rare) instances it can be helpful to know the filename of the current file. In my case, I’m looping over different regressions that may (or may not) be using different dataset to run.
To save run time, I would like to first check whether the file I’m about to open is the same as the one that I need next. If it is the same, I want to *not* open the dataset again. To do so, simply store the file name in a local:
local currentdataset="`c(filename)'"di "`currentdataset'"
This saves the entire path and filename (including .dta) into the local. Exactly the same result is actually already stored in the system global $S_FN.
My guess is that you can also *only* identify the filename without the path by using string functions and removing the part from the first character to the last “\”. But for being on the safe side I would always recommend to use the file name *with* the entire path – only this uniquely identifies the file.