The SASEFRED Interface Engine

Reading Price Data by Using Indices

The following statements enable you to access the S&P 500 Stock Price Index (IDLIST=SP500) and the Wilshire 5000 Price Index (IDLIST=WILL5000PR) on a monthly basis:

options validvarname=any;
title 'FRED Data: SP500 Stock Index and Wilshire 5000 Price Index';
LIBNAME myLib sasefred "%sysget(FRED)"
   OUTXML=gstart
   AUTOMAP=replace
   MAPREF=MyMap
   XMLMAP="%sysget(FRED)gstart.map"
   APIKEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
   IDLIST='sp500,will5000pr'
   START='2011-01-01'
   END='2012-01-01'
   FREQ='m'
   FORMAT=xml
   ;

data stock_price;
   set myLib.gstart ;
run;
proc contents data=stock_price; run;
proc print data=stock_price; run;

Figure 39.2: FRED Data: stock_price

FRED Data: SP500 Stock Index and Wilshire 5000 Price Index

Obs date realtime_start realtime_end SP500 WILL5000PR
1 2011-01-01 2013-09-26 2013-09-26 1282.62 13368.14
2 2011-02-01 2013-09-26 2013-09-26 1321.12 13772.27
3 2011-03-01 2013-09-26 2013-09-26 1304.49 13610.85
4 2011-04-01 2013-09-26 2013-09-26 1331.51 13920.50
5 2011-05-01 2013-09-26 2013-09-26 1338.31 13967.83
6 2011-06-01 2013-09-26 2013-09-26 1287.29 13441.17
7 2011-07-01 2013-09-26 2013-09-26 1325.18 13848.15
8 2011-08-01 2013-09-26 2013-09-26 1185.31 12296.04
9 2011-09-01 2013-09-26 2013-09-26 1173.88 12144.13
10 2011-10-01 2013-09-26 2013-09-26 1207.22 12459.48
11 2011-11-01 2013-09-26 2013-09-26 1226.41 12684.75
12 2011-12-01 2013-09-26 2013-09-26 1243.32 12850.31
13 2012-01-01 2013-09-26 2013-09-26 1300.58 13465.23


The SASEFRED interface engine supports the XML format. The XML data that the FRED website returns are placed in a file named by the OUTXML= option. The XML map that is automatically created is assigned the full pathname specified by the XMLMAP= option, and the fileref that is used for the map assignment is specified by the MAPREF= option. In the preceding example, the SASEFRED engine uses the MAPREF= and XMLMAP= options in the FILENAME statement to assign a filename:

   FILENAME MyMap "U:\fred\test\gstart.map";

You can use the MAPREF= and XMLMAP= options to control where the map resides, what you name the map, and how you refer to it with a fileref. You can use the OUTXML= option to name your XML data file; it is described in the section SAS OUTXML File. The XML data file is placed in the folder designated by ‘physical-name’, which is described in the section The LIBNAME libref SASEFRED Statement. You can refer to your data by using the myLib libref in your SASEFRED LIBNAME statement. The myLib libref is shown inside the DATA step in the SET statement. The SET statement reads observations from the input data set myLib.gstart and stores them in a SAS data set named stock_price, as shown in Figure 39.2. You can also use the SAS DATA step to perform further processing and to store the resulting time series in a SAS data set; this process is described in the section SAS Output Data Set.

To specify the list of time series that you want to retrieve, use the IDLIST= option. This option accepts a string enclosed in single quotation marks that denotes a list of time series that you select for the resulting SAS data set. The series IDs are separated by commas, so valid time series IDs cannot contain embedded commas or quotes. The stock_price data set contains two time series variables, sp500 and will5000pr, as specified in the IDLIST= option, and the observation range is controlled by the START= and END= options. The stock_price data set contains observations that range from January 1, 2011, to January 1, 2012, as specified by the START= and END= options. The frequency of the data is monthly, as indicated by the ‘m’ in the FREQ= option.

Note: The '%20' is a special character for URL encoding of blanks. If the time series ID that you name in the IDLIST= option contains a blank, you must use the '%20' wherever the blank appears in the time series name. If the time series ID contains an underscore, then you must use an underscore in the time series name. The underscore and the blank are not equivalent in the FRED databases, so make sure that you use the '%20' (URL encoded space) to designate blank characters.