I've been working lately with some students running cross-country
regressions, and it struck me it would be nice to have a convenient
way to map between country names and the two- or three-letter codes
for countries -- the former are often found in cross-country data
files but the latter are generally more usable in plots.
So we now (in git and snapshots) have a function, isocountry(), to
do that job using the ISO 3166 country information. We handle three
elements from ISO 3166:
1 Country name
2 2-letter code
3 3-letter code
The basic idea is that we take one of these as input and return one
of the others (or the same one, see below) as output.
The function (not yet formally documented) takes two arguments:
* A string, or array of strings (required)
* An integer (optional, may be omitted)
It returns either a string or an array of strings, matching the type
of the first argument.
The second argument (1, 2, or 3), if supplied, says which of the
three forms above you want on output. If the optional argument is
omitted the default is to convert from 1 to 2 (if form 1 is given on
input), or 2 to 1 (if 2 is given), or 3 to 1. If input is given that
is neither two nor three upper-case characters we try to interpret
it as part of a full country name. In that case we try to return the
country's 2-letter code, but a second argument of 1 can be given to
return the full name (if a partial match is found).
Here are some examples:
<gretl>
? eval isocountry("Bolivia")
BO
? eval isocountry("Bolivia", 3)
BOL
? eval isocountry("GB")
United Kingdom of Great Britain and Northern Ireland
? eval isocountry("GB", 3)
GBR
# try some abbreviations
? eval isocountry("Vanua")
VU
? eval isocountry("Zimb")
ZW
? eval isocountry("Zim", 1)
Zimbabwe
? eval isocountry("Wurzistan")
isocountry: 'Wurzistan' was not matched
? strings S = defarray("ES", "DE", "SD")
? strings C = isocountry(S)
? print C
Array of strings, length 3
[1] "Spain"
[2] "Germany"
[3] "Sudan"
? C = isocountry(S, 3)
? print C
Array of strings, length 3
[1] "ESP"
[2] "DEU"
[3] "SDN"
</gretl>
This is implemented as a "plugin", so the ISO 3166 table is not
bloating gretl's memory footprint unless you have reason to use it.
Comments welcome, if anyone has ideas for making this more useful.
Allin