Dear all,
I am trying to write some code to shorten an operation that I am doing to merge data.
I am repeating this bit of code four times.
The bits that vary are
"rename yr gdp_growth", where "gdp_growth" is either itself, "gdp_exp","inflation", or "population"
and
"drop if series!=2" has 2 be one of 2, 3, 4, 5.
The bit of code I wrote to attempt to do this is the following
Of course, this does not work, but I was wondering if there was a way to have "y" be one of 2, 3, 4, 5, and "x" be one of "gdp_growth" "gdp_exp" "inflation" "population" within the same loop.
The only other way I could think of doing that was writing
but of course this doesn't work either, since the forvalues is running inside the foreach, and so is being run nine times rather than the three I want it to be.
N.B.: I am aware there is a lot of excess wording in my code, though I copied the original reshaping code from the website attached and when trying to remove some of it, the code broke, so I am just sticking with it. What comes to mind is the variables countryname, countrycode, seriesname, seriescode which all feature in the reshape command and then are immediately dropped.
Thanks for your help!
I am trying to write some code to shorten an operation that I am doing to merge data.
I am repeating this bit of code four times.
Code:
use "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI.csv" drop if series!=2 reshape long yr, i( countryname countrycode seriesname seriescode country ) j(year) rename yr gdp_growth drop seriescode series seriesname ctry countrycode countryname drop if year<1979 merge m:m country year using "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI II.dta", nogenerate save "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI II.dta", replace
"rename yr gdp_growth", where "gdp_growth" is either itself, "gdp_exp","inflation", or "population"
and
"drop if series!=2" has 2 be one of 2, 3, 4, 5.
The bit of code I wrote to attempt to do this is the following
Code:
foreach x in "gdp_growth" "gdp_exp" "inflation" & forvalues y = 2(1)5 { use "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI.csv" drop if series!=`y' reshape long yr, i( countryname countrycode seriesname seriescode country ) j(year) rename yr `x' drop seriescode series seriesname ctry countrycode countryname drop if year<1979 merge m:m country year using "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI II.dta", nogenerate save "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI II.dta", replace }
The only other way I could think of doing that was writing
Code:
foreach x in "gdp_growth" "gdp_exp" "inflation" { use "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI.csv" forvalues y = 2(1)5 { drop if series!=`y' } reshape long yr, i( countryname countrycode seriesname seriescode country ) j(year) rename yr `x' drop seriescode series seriesname ctry countrycode countryname drop if year<1979 merge m:m country year using "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI II.dta", nogenerate save "C:\OneDrive\OneDrive - University\Year 3\Final stuff\WDI II.dta", replace }
N.B.: I am aware there is a lot of excess wording in my code, though I copied the original reshaping code from the website attached and when trying to remove some of it, the code broke, so I am just sticking with it. What comes to mind is the variables countryname, countrycode, seriesname, seriescode which all feature in the reshape command and then are immediately dropped.
Thanks for your help!
Comment