function [DATA, HEADER, DATES, ID] = process_data_M

% Sample
start_date = [datetime(2002,1,1)]; % Start date
end_date = datetime(2023,10,1); % End date

data = readtable('EAdataM.xlsx','Sheet','EA','Range','A1:AV295','VariableNamingRule','preserve');
data_AT = readtable('ATdataM.xlsx','Sheet','AT','VariableNamingRule','preserve');
data_BE = readtable('BEdataM.xlsx','Sheet','BE','VariableNamingRule','preserve');
data_DE = readtable('DEdataM.xlsx','Sheet','DE','VariableNamingRule','preserve');
data_EL = readtable('ELdataM.xlsx','Sheet','EL','VariableNamingRule','preserve');
data_ES = readtable('ESdataM.xlsx','Sheet','ES','VariableNamingRule','preserve');
data_FR = readtable('FRdataM.xlsx','Sheet','FR','VariableNamingRule','preserve');
data_IE = readtable('IEdataM.xlsx','Sheet','IE','VariableNamingRule','preserve');
data_IT = readtable('ITdataM.xlsx','Sheet','IT','VariableNamingRule','preserve');
data_NL = readtable('NLdataM.xlsx','Sheet','NL','VariableNamingRule','preserve');
data_PT = readtable('PTdataM.xlsx','Sheet','PT','VariableNamingRule','preserve');

if data_AT.Time(1) ~= data.Time(1) || data_BE.Time(1) ~= data.Time(1) ||...
   data_DE.Time(1) ~= data.Time(1) || data_EL.Time(1) ~= data.Time(1) ||...
   data_ES.Time(1) ~= data.Time(1) || data_FR.Time(1) ~= data.Time(1) ||...
   data_IE.Time(1) ~= data.Time(1) || data_IT.Time(1) ~= data.Time(1) ||...
   data_NL.Time(1) ~= data.Time(1) || data_PT.Time(1) ~= data.Time(1) 

   error('The starting date must be the same in all .xlsx files')
end

DATA.data = table2array(data(:,2:end));
DATA.data_AT = table2array(data_AT(:,2:end));
DATA.data_BE = table2array(data_BE(:,2:end));
DATA.data_DE = table2array(data_DE(:,2:end));
DATA.data_EL = table2array(data_EL(:,2:end));
DATA.data_ES = table2array(data_ES(:,2:end));
DATA.data_FR = table2array(data_FR(:,2:end));
DATA.data_IE = table2array(data_IE(:,2:end));
DATA.data_IT = table2array(data_IT(:,2:end));
DATA.data_NL = table2array(data_NL(:,2:end));
DATA.data_PT = table2array(data_PT(:,2:end));

% Import variables IDs
HEADER.header = data.Properties.VariableNames; HEADER.header(:,1) = [];
HEADER.header_AT = data_AT.Properties.VariableNames; HEADER.header_AT(:,1) = [];
HEADER.header_BE = data_BE.Properties.VariableNames; HEADER.header_BE(:,1) = [];
HEADER.header_DE = data_DE.Properties.VariableNames; HEADER.header_DE(:,1) = [];
HEADER.header_EL = data_EL.Properties.VariableNames; HEADER.header_EL(:,1) = [];
HEADER.header_ES = data_ES.Properties.VariableNames; HEADER.header_ES(:,1) = [];
HEADER.header_FR = data_FR.Properties.VariableNames; HEADER.header_FR(:,1) = [];
HEADER.header_IE = data_IE.Properties.VariableNames; HEADER.header_IE(:,1) = [];
HEADER.header_IT = data_IT.Properties.VariableNames; HEADER.header_IT(:,1) = [];
HEADER.header_NL = data_NL.Properties.VariableNames; HEADER.header_NL(:,1) = [];
HEADER.header_PT = data_PT.Properties.VariableNames; HEADER.header_PT(:,1) = [];

% Import time vector
tstart = find(data{:,1} == start_date, 1);
tend = find(data{:,1} == end_date, 1);

date=table2array(data(tstart:tend,1));
date = datetime(date, 'InputFormat', 'dd/MM/yyyy');
startT=datetime(date(1));
endT=datetime(date(end));
DATES.date = datetime(startT:calmonths(1):endT)'; 

% Sample selection
[DATA.data, DATA.data_AT, DATA.data_BE, DATA.data_DE, DATA.data_EL, DATA.data_ES, DATA.data_FR, DATA.data_IE, DATA.data_IT, DATA.data_NL, DATA.data_PT] = sample_selection(DATA.data, DATA.data_AT, DATA.data_BE, DATA.data_DE, DATA.data_EL, DATA.data_ES, DATA.data_FR, DATA.data_IE, DATA.data_IT, DATA.data_NL, DATA.data_PT, tstart, tend);
DATES.date_EU = DATES.date;

% load monetary policy instrument datasets
mpi = readtable('Instruments_monthly.xlsx','Sheet','Foglio1','Range','A1:B295','VariableNamingRule','preserve');
HEADER.header_mpi = mpi.Properties.VariableNames; HEADER.header_mpi(:,1) = [];
DATA.MPI = table2array(mpi(:,2:end));

if mpi.Time(1) ~= data.Time(1)
   error('The starting date must be the same in all .xlsx files')
end

% Sample selection IV variables
[DATA.MPI] = sample_selection_iv_policy(tstart,tend, DATA.MPI);
DATES.date_iv = DATES.date;

% load policy indicators dataset
policy = readtable('EAdataM.xlsx','Sheet','EA','Range','AW1:AW295','VariableNamingRule','preserve');
HEADER.header_policy = policy.Properties.VariableNames; 
policy_arr = table2array(policy);
DATA.policy = policy_arr;

% Sample selection Policy Indicators variables
[DATA.policy] = sample_selection_iv_policy(tstart,tend, DATA.policy);

% Save variables names
[ID.id] = save_id(HEADER.header); % EA
[ID.id_AT] = save_id(HEADER.header_AT); %
[ID.id_BE] = save_id(HEADER.header_BE); %
[ID.id_DE] = save_id(HEADER.header_DE); %
[ID.id_EL] = save_id(HEADER.header_EL); %
[ID.id_ES] = save_id(HEADER.header_ES); %
[ID.id_FR] = save_id(HEADER.header_FR); %
[ID.id_IE] = save_id(HEADER.header_IE); %
[ID.id_IT] = save_id(HEADER.header_IT); %
[ID.id_NL] = save_id(HEADER.header_NL); %
[ID.id_PT] = save_id(HEADER.header_PT); %

% Save instruments names
[ID.id_mpi] = save_id(HEADER.header_mpi);

% Save policy variables names
[ID.id_policy] = save_id(HEADER.header_policy);        

end