HASH Aggregator |
A HASH is similar to a VECTOR, except that the elements are indexed by strings (called "keys") rather than by numbers. It can't be used directly in calculations the way a VECTOR can, but is instead designed to make it simpler to organize information. You can create a HASH of any data type. The one function which can be applied to a HASH is %KEYS, which returns a VECTOR[STRINGS] with the keys. The keys aren't case-sensitive, so you can use "abc" or "ABC" or "AbC" to reference the same element.
If you DISPLAY a HASH, it will show the combination of key and value as in the following:
"def" => 13.50000
"ghi" => 11.30000
"jlk" => 15.30000
As with other aggregate types, the data type for a HASH is written as HASH[basetype], such as HASH[REAL] or HASH[VECT[VECT]]. It's important to note that a new element is added to the HASH any time you use a new string, even if it's by mistake. For instance,
dec hash[series] rseries
set rseries("defined") = %ran(1.0)
stats rseries("notdefined")
will add a second (empty) series named "notdefined" to the HASH and give a warning about having no data on the STATISTICS instruction.
An example is the following:
open data "g7rxrate.xls"
calendar(a) 1970:1
data(format=xls,org=columns) 1970:01 2003:01 canrxrate frarxrate gbrrxrate gerrxrate $
itarxrate jpnrxrate
*
dec hash[integer] lags
dofor s = canrxrate to jpnrxrate
set lx = log(s{0}/s{1})
@adfautoselect(maxlags=4,print,crit=aic) lx
compute lags(%l(s))=%%autop
end dofor s
This runs a set of @ADFAUTOSELECT procedures on data, and saves the number of chosen lags into a HASH[INTEGER]. Now LAGS("CANRXRATE") will be the number of lags chosen when S is CANRXRATE and LAGS("ITARXRATE") will be the number chosen when S is ITARXRATE. Note that you have to use "..." around literal strings in referencing an element.
As another example, this creates a HASH[STRING], again indexed by the series names, with long descriptors of the series. These are used in the headers on the graphs and to label regression output.
dec hash[string] ln
compute ln("rgnp")="Real GNP"
compute ln("gnp")="Nominal GNP"
compute ln("pcrgnp")="Real GNP(per capita)"
compute ln("ip")="Industrial production"
compute ln("emp")="Employment"
compute ln("un")="Unemployment"
compute ln("prgnp")="GNP Deflator"
compute ln("cpi")="CPI"
compute ln("wg")="Wages"
compute ln("rwg")="Real wages"
compute ln("m")="Money stock"
compute ln("vel")="Velocity"
compute ln("bnd")="Bond yield"
compute ln("sp500")="Stock price"
compute ln("dettrend")="Det. trend"
compute ln("sttrend")="Random walk"
*
spgraph(vfields=4,hfields=4,fillby=rows,footer="Figure 16.1 US annual macroeconomics variables")
dofor y = rgnp to sp500 dettrend sttrend
graph(header=ln(%l(y)))
# y
end dofor y
spgraph(done)
*
set trend = t*.01
*
dofor y = rgnp to sp500 dettrend sttrend
disp ln(%l(y))
linreg(noprint) y
# constant trend
disp %beta
linreg(noprint) y
# constant y{1}
disp %beta
linreg(noprint) y
# constant trend y{1}
disp %beta
end dofor y
Copyright © 2025 Thomas A. Doan