I need to save an output file under a filename based on the path of the current directory. For example, I have the current directory and the initial dataset defined as follows:
// current directory
cd "C:\Users\Raw data AA SYS BEST"
// the initial master dataset in "analysis" subfolder
use "analysis\master.dta" , clear
I need the output file to be saved under the name "analysis\output_AA_SYS_BEST.dta" in the same "analysis" subfolder.
There were two problems that I have encountered:
(1) how to extract the portion of the directory name. I managed to find some useful information as to how to use the substr()
function to extract the portion of the list of filenames stored within the same directory
(http://www.ats.ucla.edu/stat/stata/faq/append_many_files.htm), but not the directory itself.
(2) I have tried a simpler way of defining a local macro upfront and then concatenating it with the file name, but it did not work, presumably because of the syntax errors:
local x "AA SYS BEST"
save "analysis\test"_"`x'"_".dta"