Hi all,
I am new to Stata and I am trying to use loops to merge pairs of datasets for 4 countries. In other words, I intend to merge year1 data with year2 data for country1, year1 data with year2 data for country2, etc. I have two subfolders named "year_(x)", x being either 1 or 2, and each of them gathers one folder for each of my countries of study. This folder contains only one .dta file that is not necessarily the same across countries. Here is my code:
Unfortunately, when I try to run this code I get the following output:
I suspect this error is linked with the -use- command line. When I add a pair of brackets between `masterfile', as follows:
I get the following output:
Which is the name of my file for Guinea.
Could someone help me with advices on how to run this code properly? Many thanks for your time and precious help!
I am new to Stata and I am trying to use loops to merge pairs of datasets for 4 countries. In other words, I intend to merge year1 data with year2 data for country1, year1 data with year2 data for country2, etc. I have two subfolders named "year_(x)", x being either 1 or 2, and each of them gathers one folder for each of my countries of study. This folder contains only one .dta file that is not necessarily the same across countries. Here is my code:
Code:
global path "F:/Research" local countries Guinea Togo Mozambique Zambia foreach country of local countries { local masterfile: dir "$path/year_1/`country'/" files "*.dta" // If I understood correctly, this command stores in a macro the names of the .dta files contained in the folder "`country'". As a reminder, there is only one dataset per folder "`country'". display `filelist' cd "$path/year_1/`country'" use "./`masterfile'", clear local usingfile: dir "$path/year_2/`country'/" files "*.dta" cd "$path/year_2/`country'" merge 1:m id using "./`country'/`usingfile" cd "$path/outputs" save `country'_merged }
Code:
invalid 'guinea' r(198);
Code:
use "./"`masterfile'"", clear
Code:
invalid '"guinea-2016-full-data.dta' r(198);
Could someone help me with advices on how to run this code properly? Many thanks for your time and precious help!
Comment