前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Stata | 批量获取 UN Comtrade 数据

Stata | 批量获取 UN Comtrade 数据

作者头像
PyStaData
发布2021-04-13 14:39:55
5K0
发布2021-04-13 14:39:55
举报
文章被收录于专栏:PyStaData

数据库介绍

简介

官网:UN Comtrade Database[1]

UN Comtrade 数据库(United Nations International Trade Statistics Database)是全球最大且最为广泛应用的国际贸易数据库。每年全球超过 200 多个国家或地区分别以记录国(Reporter)角色向该数据库汇交他们与其他贸易伙伴国家(Partner)的年度(或月度)国际商品(或服务)贸易分类统计数据。汇交过程中,这些数据记录被基于联合国统计司统一标准规范执行分类编码(例如,HS,SITC,BEC)与估值(单位:美元)。

如何获取?

在 data[2] 页面(如上图)可以填入需要下载的信息,即可获取或者下载 CSV 格式的数据。

也可以通过调用 API 批量获取数据API 说明文档[3],构造请求所需主要参数如下:

代码语言:javascript
复制
The UN Comtrade data extraction API:
https://comtrade.un.org/Data/doc/api/

url = 'url="http://comtrade.un.org/api/get?'
maxrec = 50000  # maximum records returned: [1, 100000]
type= C         # trade data type
freq = A        # data set frequency, Annual or Monthly
px= H1          # classification
ps= now         # time period
r = all         # reporting area, 156: China
p= all          # partner area
rg= 2           # trade regime/trade flow
cc= TOTAL       # Classification codes
fmt= csv        # output format, json or CSV
uitoken = ''    # authorization code

Stata 获取

命令介绍

代码语言:javascript
复制
* 安装命令
ssc install comtrade, replace
help comtrade
comtrade list // List and download possible parameters

使用 API 请求数据的命令语法如下:

代码语言:javascript
复制
 comtrade [api] , hs(string) class(string) reportercountry(string)
             partnercountry(string) maxdata(string) type(string) freq(string)
             years(string) traderegime(string) [ imts(string) general options ]

各选项含义如下(文档写的比较清晰,我就直接搬运帮助文档了):

  • hs(string) sets the px parameter, the product classification scheme. Can be HS,H0, H1, H2,H4,H5, ST, S1, S2, S3, S4, BEC or EB02. See the webpage or Stata for valid parameters.
  • class(string) sets the cc parameter, the detailed product classification code. string can be: TOTAL (Total trade between reporter and partner, no detail breakdown), AG1, AG2, AG3, AG4, AG5, AG6 and ALL (all codes).AG1 - AG6 are detailed codes at a specific digit level. For instance AG6 in HS gives all of the 6-digit codes, which are the most detailed codes that are internationally comparable. Not all classifications have all digit levels available. See the classification specific codes for more information.
  • reportercountry(string) sets the r parameter, the reporter country. See list of valid reporters in web or in Stata.
  • partnercountry(string) sets the p parameter, the partner country. See list of valid partners web or in Stata
  • type(string) sets the type parameter, the trade data type. Values can be C for commodities and S for services.
  • freq(string) sets the freq parameter, the frequency. Valid values are A for annual and M for monthly.
  • traderegime(string) sets the rg parameter, the trade regime. Valid values are all for imports and exports, 1 for imports and 2 for exports. See valid parameters in web or Stata.
  • years(string) sets the ps parameter, the time period. Format can be YYYY, YYYYMM, now or recent.
  • imts(string) data fields/columns based on IMTS Concepts & Definitions. Can be 2010 for data complying with IMTS 2010 or orig for earlier versions. Is optional.
  • maxdata(string) sets the max parameter, maximum number of records to be returned.

可以输入 comtrade list 获取相应参数可填写的内容:

使用示例

需求:比如要获取以中国为报告国, 2020 年,向全球所有国家 Live animals 的出口数据

解析上面的需求,可以提取出 API 关键的参数:

代码语言:javascript
复制
r = 156     # Reporters,China: 156
freq = A    # Annual
ps = 2019   # Periods (year)
p = all     # Partners
cc = "01"   # commodity codes
rg = 1      # Trade flows, 1:Import, 2: Export

因此,对应 Stata 的写法如下:

代码语言:javascript
复制
comtrade api, maxdata(500) type(C) freq(A) hs(HS) ///
		years(2019) reporterc(156) partnerc(all) traderegime(1) ///
		cl(01)

运行上段命令之后,就可以获取需求的数据:

代码语言:javascript
复制
  obs:            65
 vars:            43                          8 Apr 2021 21:43
----------------------------------------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
----------------------------------------------------------------------------------------------------------------------
AltQuantity     byte    %10.0g                AltQuantity v_
CIFValue        byte    %10.0g                CIFValue v_
FOBValue        byte    %10.0g                FOBValue v_
GrossWeight     byte    %10.0g                GrossWeight v_
IsLeaf          byte    %10.0g                IsLeaf v_
NetWeight       byte    %10.0g                NetWeight v_
TradeQuantity   byte    %10.0g                TradeQuantity v_
TradeValue      long    %10.0g                TradeValue v_
aggrLevel       str64   %64s                  aggrLevel v_
cmdCode         byte    %10.0g                cmdCode v_
cmdDescE        str64   %64s                  cmdDescE v_
cstCode         byte    %10.0g                cstCode v_
cstDesc         str64   %64s                  cstDesc v_
estCode         byte    %10.0g                estCode v_
motCode         byte    %10.0g                motCode v_
motDesc         str64   %64s                  motDesc v_
period          int     %10.0g                period v_
periodDesc      str64   %64s                  periodDesc v_
pfCode          str64   %64s                  pfCode v_
pt3ISO          str64   %64s                  pt3ISO v_
pt3ISO2         str64   %64s                  pt3ISO2 v_
ptCode          int     %10.0g                ptCode v_
ptCode2         byte    %10.0g                ptCode2 v_
ptTitle         str64   %64s                  ptTitle v_
ptTitle2        str64   %64s                  ptTitle2 v_
qtAltCode       byte    %10.0g                qtAltCode v_
qtAltDesc       str64   %64s                  qtAltDesc v_
qtCode          byte    %10.0g                qtCode v_
qtDesc          str64   %64s                  qtDesc v_
rgCode          byte    %10.0g                rgCode v_
rgDesc          str64   %64s                  rgDesc v_
rt3ISO          str64   %64s                  rt3ISO v_
rtCode          int     %10.0g                rtCode v_
rtTitle         str64   %64s                  rtTitle v_
_TotalRecords   str21   %21s                  TotalRecords value
_freq           str21   %21s                  freq value
_isOriginal     str21   %21s                  isOriginal value
_isPartnerDet~l str21   %21s                  isPartnerDetail value
_publicationD~e str21   %21s                  publicationDate value
_px             str21   %21s                  px value
_rDesc          str21   %21s                  rDesc value
_type           str21   %21s                  type value
_DownloadDate   str20   %20s
----------------------------------------------------------------------------------------------------------------------
Sorted by:

批量获取

有了前面的基础,批量获取无非是通过循环请求了,不做过多的讲解,以一个示例说明,如果要下载其他年份的数据更改选项中的参数即可。

需求:下载以中国为报告国,2017 ~ 2019 年间 02, 03, 04 产品向全球的进口数据

代码语言:javascript
复制
/*******************************************
year: 2017~2019
reporter: China (156)
partner: all
commodities: 02 03 04
tradeflow: Import
*******************************************/

* comtrade list // List and download possible parameters

global rep "156" //China
global p "all"

*Import
local rg "1"
local cc "02 03 04"

forvalues y = 2017/2019{
	foreach c of local cc{
		comtrade api, maxdata(500) type(C) freq(A) hs(HS) ///
		years(`y') reporterc($rep) partnerc($p) traderegime(`rg') ///
		cl(`c') save("`c(pwd)'/comtrade_Import_`y'`rg'`c'$rep.dta")
	}
}

参考资料

[1]

UN Comtrade Database: https://comtrade.un.org/data/

[2]

data: https://comtrade.un.org/data/

[3]

API 说明文档: https://comtrade.un.org/Data/doc/api/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PyStaData 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数据库介绍
    • 简介
      • 如何获取?
      • Stata 获取
        • 命令介绍
          • 使用示例
            • 批量获取
              • 参考资料
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档