• "Everything should be made as simple as possible, but not simpler." - Anonymous (although often attributed to Albert Einstein)
  • Enter your email address to subscribe.

  • Categories

  • RSS Statalist: the Stata forum

  • Google Analytics Stats

    Period:Last 30 Days
    Total Visits:2548

Getting to know “factor variables”

This is an update to the earlier post i. without the prefix -xi-. So the i.‘s (or “i options” as Joe Glass called it) have a name. Stata calls them “factor variables” and there is more to them than i. .See -help fvvarlist- for the documentation and some very helpful examples.

World Bank’s open data policy and -wbopendata-

Last year, a friend from the World Bank (Manila) sent an email about World Bank’s open access policy that allows free download access to thousands of indicators from the World Bank data catalog. As I always had access to World Bank data sets via our institution’s subscription, I took this information for granted. This is not to say that I ignore the implications of this initiative. The World Bank model puts pressure on governments and other development agencies to follow. It is odd that there are still countries in the world today where economic data, such as GDP or inflation data, are not made public.

It is only a matter of time that applications, not only to automate data download, but also to present these wealth of information in ingenious ways will be made available. ESRI, for example, published a free web application that maps any one of more than a thousand economic and financial indicators for any region of the world. In the screen shot below, the size of the bubbles represents workers’ remittances inflows to countries in Asia.

To bring out the best of ideas, the World Bank initiated the “Apps for Development” competition, a challenge to software developers and development practitioners to create innovative apps using World Bank data (vote for your favorite apps here).

For Stata users, -wbopendata- (J.P. Azevedo 2011) is the module to access data from World Bank data catalog. -wbopendata- is easy to use but note that it requires an internet connection. First, install -wbopendata- via SSC:

ssc install wbopendata

-wbopendata- allows you to download (i) all indicators for a specific country for all years or (ii) a specific indicator for all countries and for all years or (iii) a set of indicators within a specified topic for all countries and for all years. -wbopendata- loads the data into the Stata memory. For example, to download all data available for the Philippines for all years, type:

wbopendata, country(phl) clear

This returns data for 972 indicators from 1960 to the latest year available. The default data display is in wide format. To display the data in a long format, use the ‘long’ option:

wbopendata, country(phl) long clear

To download GDP per capita (in constant PPP $) for all countries, type:

wbopendata, indicator(ny.gdp.pcap.pp.kd) clear

Lastly, to download all indicators under the topic “Poverty” for all countries, type:

wbopendata, topics(11) clear

The list of countries, topics, and indicators and their corresponding codes are documented in the help file (see -help wbopendata-). -wbopendata- also has other options not mentioned here.

ComtradeTools and Stata: Automating UN Comtrade data downloads

ComtradeTools (developed by UN) is a command line program that allows you to obtain data via UN Web Services and to convert the data into CSV format (or to an SQL server). You can use Stata’s -shell- command to run ComtradeTools.

First, read the instructions here. Download the latest version (March 2010) of ComtradeTools here and the required Microsoft .NET Framework (version 1.1.4322.573) here.

The command line parameters are listed in /help which you can copy into a text file. To copy ComtradeTools /help contents to ComtradeTools_Help.txt:

cd “C:\Program Files\UNSD\ComtradeTools\”
shell ComtradeTools /help >>U:\Data\Comtrade\ComtradeTools_Help.txt

/help does not enumerate all possible entries for each parameter, but you can find them in UN Comtrade’s website. Some are listed below:

/r: Reporter Code. To get the list of countries and their corresponding codes:

copy http://comtrade.un.org/ws/refs/getCountryList.aspx U:\Data\Comtrade\Countrylist.xml //You can open this in Excel

/y: Year. 4-digit year.

/px: Classification.
SITC Rev.3=S3
SITC Rev.2=S2
SITC Rev.1=S1

/cc: Commodity Code. For example, to get commodity codes for HS1996 and SITC Rev. 2:

local classification H1 S2
foreach c of local classification{
copyhttp://comtrade.un.org/ws/refs/getCommodityList.aspx?px=c'" <em>U:\Data\Comtrade\Commoditylist_</em>c’.xml

/rg: Trade Flow.

Try to download trade data (SITC Rev. 2) for the Philippines for the year 2007 and load it to Stata.

shell ComtradeTools /r:608 /y:2007 /px:S2 /action:DownloadAndConvertToCSV /outputDirectory:U:\Data\Comtrade\
insheet using U:\Data\Comtrade\S22007608_CSV.txt, comma clear

Note that if you do not specify /outputDirectory, the files will be saved in the current working directory, that is, where the program ComtradeTools is saved.

Now, it is easy to download data for multiple countries and years by using loops. For example, to download trade data (SITC Rev. 2) for the Philippines every 5 years from 1980 to 2005 and save them as dta files:

forvalues y=1980(5)2005{
shell ComtradeTools /r:608 /y:y' <strong>/px:</strong><em>S2</em> <strong>/action:</strong>DownloadAndConvertToCSV <strong>/outputDirectory:</strong><em>U:\Data\Comtrade\</em>
<strong>insheet </strong>using <em>U:\Data\Comtrade\S2</em>
y’608_CSV.txt, comma clear
save U:\Data\Comtrade\S2y'<em>608.dta</em>, replace
<strong>erase </strong><em>U:\Data\Comtrade\S2</em>

%d bloggers like this: