/********Stata code used to extract WID series 1820-2023 for population and net domestic product***/
/*****series using 57 core territories: 48 countries + 9 subregions**/

#delimit;
clear all;
set more off;
set maxvar 6000;
do Codes/doglobalvariables.txt;


/****importation of WID population series***/ 
/****population age-gender 1800-2023 57 core territories**/
wid, ind(npopul) ag(999) pop(i f) clear;
save Data/popndp.dta, replace;
wid, ind(npopul) ag(014) pop(i f) clear;
append using Data/popndp.dta;
save Data/popndp.dta, replace;
wid, ind(npopul) ag(156) pop(i f) clear;
append using Data/popndp.dta;
save Data/popndp.dta, replace;
wid, ind(npopul) ag(997) pop(i f) clear;
append using Data/popndp.dta;
save Data/popndp.dta, replace;
wid, ind(npopul) ag(991) pop(i f) clear;
append using Data/popndp.dta;
save Data/popndp.dta, replace;
use Data/popndp.dta, clear;
gen coreterritory=.;
foreach c of global coreterritories
{;
replace coreterritory=1 if country=="`c'";
};
keep if coreterritory==1 | country=="OK" | country=="OL" | country=="OO" | country=="OP" | country=="OQ";
drop if country=="OB" | country=="OC" | country=="OE" | country=="OI" | country=="OJ";
replace country="OB" if country=="OK"; 
replace country="OC" if country=="OL";
replace country="OE" if country=="OO";
replace country="OI" if country=="OP";
replace country="OJ" if country=="OQ";
drop coreterritory age pop percentile;
replace value=value/1000000;
reshape wide value, i(country year) j(variable) string;
rename valuenpopul* pop*;
rename pop999i poptot;
rename pop014i pop014;
rename pop014f pop014w;
rename pop156i pop1564;
rename pop156f pop1564w;
gen pop1564m=pop1564-pop1564w;
rename pop997i pop65;
rename pop997f pop65w;
keep poptot pop014 pop1564 pop65 pop014w pop1564w pop1564m pop65w year country;
reshape wide poptot pop014 pop1564 pop65 pop014w pop1564m pop1564w pop65w, i(year) j(country) string;
save Data/popndp.dta, replace;


/****importation of WID GDP and CFC series***/ 
/**(08-05-25) new GDP and CFC series still need to be uploaded on WID, so for now we use Gaston-Thomas file and Jonas et al files**/
/****aggregate GDP millions euros 2023 PPP 1800-2023 for 57 core territories***/
import excel "D:\Users\t.piketty\Dropbox\Piketty2025GlobalJusticeProjectTrade\Paper\NievasPiketty2025AppendixHistorical.xlsx", sheet("B1d") cellrange(K4:BO228) firstrow clear;
rename * gdp*;
gen year=1800+_n-1;
merge 1:1 year using Data/popndp.dta;
drop _merge;
save Data/popndp.dta, replace;
import excel "D:\Users\t.piketty\Dropbox\Piketty2025GlobalJusticeProjectSectors\Dietrichetal2025Sectors.xlsx", sheet("C1") cellrange(K4:BO228) firstrow clear;
rename * cfc*;
gen year=1800+_n-1;
merge 1:1 year using Data/popndp.dta;
drop _merge;
foreach i in DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
gen ndp`i'=gdp`i'*(1-cfc`i');
};
drop gdp* cfc*;
save Data/popndp.dta, replace;


/****adding years 2024-2025 (country level past-10-year growth rate)**/
/****converting all 2023 PPP amounts in 2025 PPP amounts (assuming 2.4% inflation in 2024 and 2.2% in 2025)**/
use Data\popndp.dta, clear;
insobs 2;
replace year=2024 if _n==225;
replace year=2025 if _n==226;
sort year;
foreach i in DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
foreach k in ndp poptot pop014 pop1564 pop65 pop014w pop1564w pop1564m pop65w
{;
replace `k'`i'=`k'`i'[_n-1]*((`k'`i'[_n-1]/`k'`i'[_n-11])^(1/10))  if year==2024 | year==2025;
};
replace ndp`i'=ndp`i'*1.024*1.022; 
};
save Data/popndp.dta, replace;


/***sous-totaux regionaux**/
use Data\popndp.dta, clear;
foreach k in ndp poptot pop014 pop1564 pop65 pop014w pop1564w pop65w pop1564m
{;
gen `k'EURO=`k'DE+`k'DK+`k'ES+`k'FR+`k'GB+`k'IT+`k'NL+`k'NO+`k'SE+`k'OC+`k'QM;
gen `k'NAOC=`k'US+`k'CA+`k'AU+`k'NZ+`k'OH;
gen `k'LATA=`k'AR+`k'BR+`k'CL+`k'CO+`k'MX+`k'OD;
gen `k'MENA=`k'AE+`k'DZ+`k'EG+`k'IR+`k'MA+`k'SA+`k'TR+`k'OE;
gen `k'SSAF=`k'CD+`k'CI+`k'ET+`k'KE+`k'ML+`k'NE+`k'NG+`k'RW+`k'SD+`k'ZA+`k'OJ;
gen `k'RUCA=`k'RU+`k'OA;
gen `k'EASA=`k'CN+`k'JP+`k'KR+`k'TW+`k'OB;
gen `k'SSEA=`k'BD+`k'IN+`k'ID+`k'MM+`k'PK+`k'PH+`k'TH+`k'VN+`k'OI;
gen `k'WO=`k'EURO+`k'NAOC+`k'LATA+`k'MENA+`k'SSAF+`k'RUCA+`k'EASA+`k'SSEA;
};
order year ndpWO ndpEURO ndpNAOC ndpLATA ndpMENA ndpSSAF ndpRUCA ndpEASA ndpSSEA ndpDE ndpDK ndpES ndpFR ndpGB ndpIT ndpNL ndpNO ndpSE ndpOC ndpQM ndpUS ndpCA ndpAU ndpNZ ndpOH ndpAR ndpBR ndpCL ndpCO ndpMX ndpOD ndpAE ndpDZ ndpEG ndpIR ndpMA ndpSA ndpTR ndpOE ndpCD ndpCI ndpET ndpKE ndpML ndpNE ndpNG ndpRW ndpSD ndpZA ndpOJ ndpRU ndpOA ndpCN ndpJP ndpKR ndpTW ndpOB ndpBD ndpIN ndpID ndpMM ndpPK ndpPH ndpTH ndpVN ndpOI 
poptotWO poptotEURO poptotNAOC poptotLATA poptotMENA poptotSSAF poptotRUCA poptotEASA poptotSSEA poptotDE poptotDK poptotES poptotFR poptotGB poptotIT poptotNL poptotNO poptotSE poptotOC poptotQM poptotUS poptotCA poptotAU poptotNZ poptotOH poptotAR poptotBR poptotCL poptotCO poptotMX poptotOD poptotAE poptotDZ poptotEG poptotIR poptotMA poptotSA poptotTR poptotOE poptotCD poptotCI poptotET poptotKE poptotML poptotNE poptotNG poptotRW poptotSD poptotZA poptotOJ poptotRU poptotOA poptotCN poptotJP poptotKR poptotTW poptotOB poptotBD poptotIN poptotID poptotMM poptotPK poptotPH poptotTH poptotVN poptotOI 
pop1564WO pop1564EURO pop1564NAOC pop1564LATA pop1564MENA pop1564SSAF pop1564RUCA pop1564EASA pop1564SSEA pop1564DE pop1564DK pop1564ES pop1564FR pop1564GB pop1564IT pop1564NL pop1564NO pop1564SE pop1564OC pop1564QM pop1564US pop1564CA pop1564AU pop1564NZ pop1564OH pop1564AR pop1564BR pop1564CL pop1564CO pop1564MX pop1564OD pop1564AE pop1564DZ pop1564EG pop1564IR pop1564MA pop1564SA pop1564TR pop1564OE pop1564CD pop1564CI pop1564ET pop1564KE pop1564ML pop1564NE pop1564NG pop1564RW pop1564SD pop1564ZA pop1564OJ pop1564RU pop1564OA pop1564CN pop1564JP pop1564KR pop1564TW pop1564OB pop1564BD pop1564IN pop1564ID pop1564MM pop1564PK pop1564PH pop1564TH pop1564VN pop1564OI 
pop014WO pop014EURO pop014NAOC pop014LATA pop014MENA pop014SSAF pop014RUCA pop014EASA pop014SSEA pop014DE pop014DK pop014ES pop014FR pop014GB pop014IT pop014NL pop014NO pop014SE pop014OC pop014QM pop014US pop014CA pop014AU pop014NZ pop014OH pop014AR pop014BR pop014CL pop014CO pop014MX pop014OD pop014AE pop014DZ pop014EG pop014IR pop014MA pop014SA pop014TR pop014OE pop014CD pop014CI pop014ET pop014KE pop014ML pop014NE pop014NG pop014RW pop014SD pop014ZA pop014OJ pop014RU pop014OA pop014CN pop014JP pop014KR pop014TW pop014OB pop014BD pop014IN pop014ID pop014MM pop014PK pop014PH pop014TH pop014VN pop014OI 
pop65WO pop65EURO pop65NAOC pop65LATA pop65MENA pop65SSAF pop65RUCA pop65EASA pop65SSEA pop65DE pop65DK pop65ES pop65FR pop65GB pop65IT pop65NL pop65NO pop65SE pop65OC pop65QM pop65US pop65CA pop65AU pop65NZ pop65OH pop65AR pop65BR pop65CL pop65CO pop65MX pop65OD pop65AE pop65DZ pop65EG pop65IR pop65MA pop65SA pop65TR pop65OE pop65CD pop65CI pop65ET pop65KE pop65ML pop65NE pop65NG pop65RW pop65SD pop65ZA pop65OJ pop65RU pop65OA pop65CN pop65JP pop65KR pop65TW pop65OB pop65BD pop65IN pop65ID pop65MM pop65PK pop65PH pop65TH pop65VN pop65OI;  
save Data\popndp.dta, replace;

/***export series for appendix tables and figures**/
use Data\popndp.dta, clear;
keep poptot*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A1", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace pop1564`i'=pop1564`i'/poptot`i';
};
keep pop1564*;
drop pop1564w*;
drop pop1564m*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A3", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace pop014`i'=pop014`i'/poptot`i';
};
keep pop014*;
drop pop014w*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A4", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace pop65`i'=pop65`i'/poptot`i';
};
keep pop65*;
drop pop65w*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A5", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace poptot`i'=(pop014w`i'+pop1564w`i'+pop65w`i')/poptot`i';
};
keep poptot*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A6", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace poptot`i'=pop1564w`i'/pop1564`i';
};
keep poptot*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A7", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace poptot`i'=pop014w`i'/pop014`i';
};
keep poptot*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A8", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace poptot`i'=pop65w`i'/pop65`i';
};
keep poptot*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("A9", modify) cell(B5) keepcellfmt;
use Data\popndp.dta, clear;
foreach i in WO EURO NAOC LATA MENA SSAF RUCA EASA SSEA DE DK ES FR GB IT NL NO SE OC QM US CA AU NZ OH AR BR CL CO MX OD AE EG DZ IR MA SA TR OE CD CI ET KE ML NE NG RW SD ZA OJ RU OA CN JP KR TW OB BD IN ID MM PK PH TH VN OI
{;
replace ndp`i'=ndp`i'/poptot`i';
};
keep ndp*;
export excel using Andreescuetal2025Appendix.xlsx, sheet("C1", modify) cell(B5) keepcellfmt;



