MATLAB: DATA ARRANGEMENT FROM PANEL FORMAT

panel data

Hello,
I have a data of 5 variables (x1, x2, x3, x4, x5) for 24 countries of 28 years. (N = 24, T = 28, d = 5) in panel format. I want to arrange X = (x_{11}, …, x_{1T}, …, x_{N1}, …, x_{NT})'.
How do I proceed.

Best Answer

Hi,
I assume you have 28 tables corresponding to 28 years with 5 variables for all 24 countries based on my understanding of panel format data. So, each table contains all 24 countries and 5 variables for it. You can create a matrix first for each variable X1, X2, X3, X4, X5. Then can read each of the 28 tables and assign the columns to corresponding variable column. Then you can flatten each variable matrix in row major form to get the output. You may find below code useful.
% Let say tablei is the table having 24 rows and 5 column
% corresponding to x1,x2,x3,x4,x5
% Lets say you have tables stored as .csv file and all the names of .csv
% files are stored in vector tableName.
N = 24;
T = 28;
d = 5;
X1mat = zeros(N,T);
X2mat = zeros(N,T);
X3mat = zeros(N,T);
X4mat = zeros(N,T);
X5mat = zeros(N,T);
for i=1:size(tableName,1) % For the number of years
tablei = readtable(tabelName(i));
X1mat(:,i) = tablei.x1;
X2mat(:,i) = tablei.x2;
X3mat(:,i) = tablei.x3;
X4mat(:,i) = tablei.x4;
X5mat(:,i) = tablei.x5;
end
% Creating vector by Reshaping in row major form
X1 = reshape(X1mat.',1,[]);
X2 = reshape(X2mat.',1,[]);
X3 = reshape(X3mat.',1,[]);
X4 = reshape(X4mat.',1,[]);
X5 = reshape(X5mat.',1,[]);