Hello,
I have a long and fairly complex Stata script which I run after first setting the cd (working directory) to a specific directory. I want the code to run in the first directory in the list, then in the second directory in the list( same Stata script- as the tables in the directories all have the same names and variable names etc) . I have created a loop so that the code will run the code using .dta files that are in the respective directories:
CD "P:\FOLDER1\FOLDER2 \ year _base"
CD "P:\FOLDER1\FOLDER2 \ year 1"
CD "P:\FOLDER1\FOLDER2 \ year 2"
CD "P:\FOLDER1\FOLDER2 \ year 3"
CD "P:\FOLDER1\FOLDER2 \ year 4"
CD "P:\FOLDER1\FOLDER2 \ year 5"
However, I would like to change the script, so that when it comes merging a table that contains the variables year_base, year_1, year_2, year_3, year_4 and year_5, it only keeps the variable that matches the directory name, so when it merges with a table it keeps "year_base" variable where the the directory is CD "P:\FOLDER1\FOLDER2 \ year _base" and variable year_2 when the current directory is CD "P:\FOLDER1\FOLDER2 \ year 2" etc. I would be happy to merge all 6 variables, if I could then drop the only one that is needed based on the current working directory name.
Is this at all possible in Stata?
My loop, if it helps is as follows:
foreach i of num 1/5 {
CD "P:\FOLDER1\FOLDER2 \ year `i'"
stata code here
}
I thought I could utilise the `i' in the loop in someway, but the loop does not work for CD "P:\FOLDER1\FOLDER2 \ year _base". I have not been able to solve that problem - so I am thinking of some other way of referencing the current working directory so that the code either picks up the right variable or only keeps the right variable.
I have a long and fairly complex Stata script which I run after first setting the cd (working directory) to a specific directory. I want the code to run in the first directory in the list, then in the second directory in the list( same Stata script- as the tables in the directories all have the same names and variable names etc) . I have created a loop so that the code will run the code using .dta files that are in the respective directories:
CD "P:\FOLDER1\FOLDER2 \ year _base"
CD "P:\FOLDER1\FOLDER2 \ year 1"
CD "P:\FOLDER1\FOLDER2 \ year 2"
CD "P:\FOLDER1\FOLDER2 \ year 3"
CD "P:\FOLDER1\FOLDER2 \ year 4"
CD "P:\FOLDER1\FOLDER2 \ year 5"
However, I would like to change the script, so that when it comes merging a table that contains the variables year_base, year_1, year_2, year_3, year_4 and year_5, it only keeps the variable that matches the directory name, so when it merges with a table it keeps "year_base" variable where the the directory is CD "P:\FOLDER1\FOLDER2 \ year _base" and variable year_2 when the current directory is CD "P:\FOLDER1\FOLDER2 \ year 2" etc. I would be happy to merge all 6 variables, if I could then drop the only one that is needed based on the current working directory name.
Is this at all possible in Stata?
My loop, if it helps is as follows:
foreach i of num 1/5 {
CD "P:\FOLDER1\FOLDER2 \ year `i'"
stata code here
}
I thought I could utilise the `i' in the loop in someway, but the loop does not work for CD "P:\FOLDER1\FOLDER2 \ year _base". I have not been able to solve that problem - so I am thinking of some other way of referencing the current working directory so that the code either picks up the right variable or only keeps the right variable.
Comment