Code covered by the BSD License  

Highlights from
Import PWT Version 8.0

image thumbnail

Import PWT Version 8.0

by

 

12 Sep 2013 (Updated )

Downloads the 'Penn World Table' from the web and places the information into a Matlab variable.

ImportPWT(varargin)
% IMPORTPWT : downloads PWT version 8.0 data from internet and puts it into a struct
%
% Usage: PWT = ImportPWT()
%        PWT = ImportPWT([true/false])
%
% Input: The argument specifies if a fresh copy should be downloaded from
% the internet. Default is true. If set to false, the program will attempt
% to locate 'pwt80.xlsx' on the local drive and use this if available.
%
% Output: PWT is a struct containing years, country names, and all the
% Penn World Table variables as fields.
%
% Example:
%     PWT = ImportPWT();                            % download data
%     india = find(ismember(PWT.country,'India'));  % find index of India
%     % compute India's GDP/capita (using national account data)
%     income_per_capita = PWT.rgdpna(india,:)./PWT.pop(india,:);
%     % plot the growth rate
%     figure('Name','India''s growth rate');
%     plot(PWT.year(2:end),diff(log(income_per_capita)));
%     dateaxis('x'); grid on;                       % embellish the chart
%     % start new figure
%     figure('Name','India vs China');
%     china = find(ismember(PWT.country,'China'));  % find index of China
%     % plot India's vs China's imcome per capita
%     incPC = PWT.rgdpe([india,china],:)./PWT.pop([india,china],:);
%     semilogy(PWT.year,incPC,'LineSmoothing','on','LineWidth',1.5);
%     xlim([PWT.year(1),PWT.year(end)]);            % embellish the chart
%     dateaxis('x'); grid on;
%     legend('India','China');
%     legend('location','SouthEast');
%
% PWT stands for Penn World Table. It is a database of several
% macroeconomic data at yearly frequency, starting 1950 and covering
% 167 countries.
%
% Data provider:
%   Robert C. Feenstra, Robert Inklaar and Marcel P. Timmer (2013),
%   "The Next Generation of the Penn World Table" available for
%   download at www.ggdc.net/pwt
%
% Author: Yvan Lengwiler, University of Basel, Switzerland
% url   : http://www.wwz.unibas.ch/lengwiler
% email : yvan.lengwiler@unibas.ch (please write if you need help)
% Date  : 2013-09-10

function PWT = ImportPWT(varargin)

    % should file be downloaded?
    if nargin == 0
        doDownload = true;
    else
        doDownload = varargin{1};
    end
    
    % download or locate file
    fname = 'pwt80.xlsx';
    if doDownload
        
        % download data from PWT website
        url = 'http://www.rug.nl/research/GGDC/data/pwt/V80/pwt80.xlsx';
        [fname, success] = urlwrite(url,fname);
        if ~success
            error('PWT:download_failed', ...
                ['Tried to download from this URL, ''%s''.\n', ...
                'Download failed for some reason.\n'], url);
        else
            disp('File successfully downloaded from PWT site.');
        end
        
    else
        
        % use local copy of PWT file
        fprintf('Looking for local version of ''pwt80.xlsx'' ... ');
        d = dir(fname);
        if isempty(d)
            fprintf('failed.\n')
            error('PWT:search_failed', ...
                'Could not find local copy of ''%s''\n', fname);
        else
            fprintf('found it.\n');
        end
        
    end
    
    % read Excel file
    [num,txt] = xlsread(fname,'data');
    years     = [min(num(:,1)):max(num(:,1))];
    T         = numel(years);
    isocode   = txt(2:T:end,1);
    country   = txt(2:T:end,2);
    currency  = txt(2:T:end,3);
    N         = numel(isocode);
    
    % create output variable
    PWT = struct( ...
        'version',  {'PWT 8.0'}, ...
        'T',        T, ...
        'year',     datenum([years',repmat([12,31],T,1)])', ...
        'N',        N, ...
        'country',  {country}, ...
        'isocode',  {isocode}, ...
        'currency', {currency});
    fprintf(' %i countries, years from %i to %i.\n',N,years(1),years(end));
    
    % now add all the data
    vars = txt(1,[5:21,26:end]);            % leave out columns 22:25
    num(:,[22:25]-3) = [];
    nvars = numel(vars);
    for k = 1:nvars
        data = reshape(num(:,k+1),T,N)';    % first numerical col is year
        name = genvarname(vars{k});         % make sure variable name is legal
        fprintf(' variable #%i: %s (%ix%i)\n',k,name,size(data))
        PWT.(name) = data;                  % add variable to the struct
    end
    % add the string coded variables
    vars = txt(1,22:25);                    % take only columns 22:25
    txt  = txt(2:end,22:25);
    num  = NaN(size(txt));
    num(ismember(txt,'Benchmark'))    = 0;
    num(ismember(txt,'Extrapolated')) = 1;
    num(ismember(txt,'Intrapolated')) = -1;
    num(ismember(txt,'Estimated'))    = 1;
    num(ismember(txt,'Market'))       = 0;
    num(ismember(txt,'Regular'))      = 0;
    num(ismember(txt,'Outlier'))      = 1;
    for k = 1:numel(vars)
        data = reshape(num(:,k),T,N)';      % first numerical col is year
        name = genvarname(vars{k});         % make sure variable name is legal
        fprintf(' variable #%i: %s (%ix%i)\n',k+nvars,name,size(data))
        PWT.(name) = data;                  % add variable to the struct
    end

    disp('done.');
    
end

Contact us