You can query the Federal Reserve Economic Data (FRED) databases to retrieve the observations or data values for a list of economic time series by specifying the series ID of each time series that you want to read into SAS and by specifying your unique FRED API key. To obtain your own unique API key, visit the FRED website at the following URL:
http://api.stlouisfed.org/api_key.html
The FRED API key is a 32-character alphanumeric lowercase string, such as ‘abcdefghijklmnopqrstuvwxyz123456’, and is represented by ’XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’ in the APIKEY= option in the following example. In addition, the example URLs in this section and in the section Details: SASEFRED Interface Engine use the same FRED API key as the argument your_fred_apikey.
After you have your assigned FRED API key and you have agreed to the terms of use, before downloading any copyright-protected data series, be aware that you are solely responsible for obtaining copyright permissions for any copyright-protected time series that you download (other than for personal use). To obtain a list of the copyright-protected data series, visit the web page at the following URL:
http://api.stlouisfed.org/fred/series/search?search_text=copyrightapi_key=your_fred_apikey
Now that your are informed about the terms of use of the FRED data, you can use your FRED API key to access the FRED data, as shown in the following example.
The following statements enable you to access the exports of goods and services time series data from January 1, 1960, to
January 1, 2012, on an annual basis. The observations are sorted by the time ID variable DATE
.
options validvarname=any; title 'Retrieve Data for the Exports of Goods and Services'; libname _all_ clear; libname fred sasefred "%sysget(FRED)" OUTXML=exportgs XMLMAP="%sysget(FRED)exportgs.map" APIKEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' IDLIST='bopxgsa'; data export_gsa; set fred.exportgs ; run;
proc contents data=export_gsa; run; proc print data=export_gsa(obs=15); run;
Figure 39.1: Getting Started with Exports of Goods and Services: export_gsa(obs=15)
Retrieve Data for the Exports of Goods and Services |
Obs | realtime_start | realtime_end | date | BOPXGSA |
---|---|---|---|---|
1 | 2013-09-26 | 2013-09-26 | 1960-01-01 | 25.940 |
2 | 2013-09-26 | 2013-09-26 | 1961-01-01 | 26.403 |
3 | 2013-09-26 | 2013-09-26 | 1962-01-01 | 27.722 |
4 | 2013-09-26 | 2013-09-26 | 1963-01-01 | 29.620 |
5 | 2013-09-26 | 2013-09-26 | 1964-01-01 | 33.341 |
6 | 2013-09-26 | 2013-09-26 | 1965-01-01 | 35.285 |
7 | 2013-09-26 | 2013-09-26 | 1966-01-01 | 38.926 |
8 | 2013-09-26 | 2013-09-26 | 1967-01-01 | 41.333 |
9 | 2013-09-26 | 2013-09-26 | 1968-01-01 | 45.543 |
10 | 2013-09-26 | 2013-09-26 | 1969-01-01 | 49.220 |
11 | 2013-09-26 | 2013-09-26 | 1970-01-01 | 56.640 |
12 | 2013-09-26 | 2013-09-26 | 1971-01-01 | 59.677 |
13 | 2013-09-26 | 2013-09-26 | 1972-01-01 | 67.222 |
14 | 2013-09-26 | 2013-09-26 | 1973-01-01 | 91.242 |
15 | 2013-09-26 | 2013-09-26 | 1974-01-01 | 120.897 |
The XML data that the FRED website returns are placed in a file named by the OUTXML= option, in this case, EXPORTGS.xml. Note that the XML file extension is excluded from the filename given in the OUTXML= option. This XML data file resides in the location given inside the string enclosed in double quotation marks in the SASEFRED LIBNAME statement. So, in the preceding example, if the FRED environment variable is set to:
C:\freddata\
then the downloaded XML file is located at
C:\freddata\EXPORTGS.xml
An equivalent LIBNAME statement that does not use any environment variables could be as follows:
Libname fred sasefred "C:\freddata\" OUTXML=exportgs XMLMAP="C:\freddata\exportgs.map" APIKEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' IDLIST='bopxgsa';
You could also use either a SAS macro variable or a system environment variable to store the value of your FRED API key so that the key does not appear explicitly in your SAS code. The XML map that is created is assigned the full pathname specified by the XMLMAP= option. The IDLIST= option specifies the list of time series that you want to retrieve. This option accepts a string, enclosed in single quotation marks, that denotes a list of one or more time series that you select (keep) in the resulting SAS data set. The result, Export_gsa, is named in the DATA step and is shown in Figure 39.1. It is more efficient to use the DATA step to store your FRED data in a SAS data set and then refer to the SAS data set directly in your PROC PRINT or PROC GPLOT statement, but you can also refer to the SASEFRED libref directly, as in the following statement:
proc print data=fred.exportgs; run;
This statement uses the member name, exportgs, in the PROC PRINT statement; this usage corresponds to specifying the OUTXML=EXPORTGS option. Although using this statement might seem easier, it is not as efficient, because every time you use the SASEFRED libref, the FRED interface reads the entire XML file again into SAS. It is best to refer to the SAS data set repeatedly rather than invoking the interface engine repeatedly. For another example that uses more SASEFRED LIBNAME statement options, see the section Reading Price Data by Using Indices.