You can query the Quandl data set to retrieve the observations or data values for a list of economic time series by specifying the Quandl code of the data set. The Quandl code consists of a source code and a table code for the data set that contains the time series that you want to read into SAS. You must also specify your unique Quandl API key (authentication token for unlimited access). To obtain your own unique API key, visit the Quandl website at the following URL: https://www.quandl.com/users/sign_up .
The Quandl API key is a 20-character mixed-case alphanumeric string, such as "abCDefghiJKLMn123456," and is represented by ’XXXXXXXXXXXXXXXXXXXX’ in the APIKEY= option in the following example. In addition, the example URLs in this section and in the section Details: SASEQUAN Interface Engine use the same Quandl API key as the argument your_quan_apikey.
After you have your assigned Quandl API key and have agreed to the Quandl terms of use, you are almost ready to download Quandl data. Before you download, make sure you have the necessary rights to work with the data.
Now that your are informed about the terms of use of the Quandl data, you can use your Quandl API key to access the Quandl data, as shown in the following example.
The statements that follow enable you to access the prices for oil from the National Stock Exchange of India’s time series
data from September 1, 2013, to November 5, 2013, on a daily basis. The observations are sorted by the time ID variable DATE
. The output is shown in Figure 50.1.
options validvarname=any; title 'Retrieve Data for Oil India Limited Prices'; libname _all_ clear; libname quan sasequan "%sysget(QUANDL)" OUTXML=oiltd XMLMAP="%sysget(QUANDL)oiltd.map" APIKEY='XXXXXXXXXXXXXXXXXXXX' IDLIST='NSE/OIL'; data oil_gsa; set quan.oiltd; run;
proc contents data=oil_gsa; run; proc print data=oil_gsa(firstobs=1328 obs=1342); run;
Figure 50.1: Oil India Limited Prices: Oil_Gsa (FIRSTOBS=1328 OBS=1342)
Retrieve Data for Oil India Limited Prices |
Obs | date | Open | High | Low | Last | Close | Total Trade Quantity |
Turnover (Lacs) |
---|---|---|---|---|---|---|---|---|
1328 | 2015-02-02 | 536.20 | 540.90 | 530.25 | 534.00 | 533.25 | 201704 | 1077.25 |
1329 | 2015-02-03 | 539.80 | 541.00 | 526.25 | 531.50 | 531.35 | 923694 | 4910.35 |
1330 | 2015-02-04 | 541.00 | 550.45 | 536.40 | 545.50 | 548.75 | 485793 | 2644.40 |
1331 | 2015-02-05 | 548.85 | 549.00 | 538.25 | 540.50 | 540.05 | 877473 | 4742.75 |
1332 | 2015-02-06 | 536.50 | 552.90 | 536.50 | 545.35 | 547.00 | 358329 | 1962.28 |
1333 | 2015-02-09 | 545.00 | 553.75 | 530.00 | 540.00 | 543.00 | 608323 | 3332.38 |
1334 | 2015-02-10 | 540.00 | 546.45 | 527.00 | 531.45 | 530.85 | 326785 | 1759.67 |
1335 | 2015-02-11 | 532.00 | 536.40 | 529.10 | 530.30 | 530.95 | 116276 | 618.56 |
1336 | 2015-02-12 | 534.65 | 536.00 | 528.00 | 531.95 | 531.65 | 189407 | 1006.99 |
1337 | 2015-02-13 | 521.00 | 525.90 | 495.10 | 504.00 | 500.20 | 895268 | 4542.81 |
1338 | 2015-02-16 | 505.00 | 513.90 | 495.00 | 495.00 | 499.00 | 379163 | 1909.42 |
1339 | 2015-02-18 | 501.80 | 506.50 | 494.40 | 500.95 | 501.10 | 261958 | 1314.47 |
1340 | 2015-02-19 | 503.30 | 506.00 | 494.15 | 497.00 | 497.30 | 161816 | 806.24 |
1341 | 2015-02-20 | 499.00 | 502.90 | 493.00 | 494.30 | 494.40 | 220134 | 1092.32 |
1342 | 2015-02-23 | 500.00 | 500.00 | 485.20 | 487.80 | 487.30 | 194121 | 952.37 |
The XML data that the Quandl website returns are placed in a file that is named by the OUTXML= option—in this case, OILTD1.xml. Note that the SASEQUAN engine appends a numeral to the XML filename, and the file extension (.xml) is excluded from the filename that appears in the OUTXML= option. This XML data file resides in the location that is given inside the string enclosed in double quotation marks in the SASEQUAN LIBNAME statement. So, in the preceding example, if the QUANDL environment variable is set to
C:\quandata\
then the downloaded XML file is located at
C:\quandata\OILTD1.xml
An equivalent LIBNAME statement that does not use any environment variables could be as follows:
libname quan sasequan "C:\quandata\" OUTXML=oiltd XMLMAP="C:\quandata\oiltd.map" APIKEY='XXXXXXXXXXXXXXXXXXXX' IDLIST='NSE/OIL';
You could also use either a SAS macro variable or a system environment variable to store the value of your Quandl 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 that the XMLMAP= option specifies. The SASEQUAN engine appends a numeral to the XML filename to indicate the position of the Quandl code in the IDLIST= option.
The IDLIST= option specifies the list of Quandl data sets (that contain 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 Quandl data sets that you select (keep) in the resulting SAS data set. The result, OILTD, is named in the DATA step and is shown in Figure 50.1. The preceding example uses only one Quandl code, which is in the first position of the IDLIST= option, so the numeral 1 is appended to the name of the XML file, resulting in OIL1.xml.
It is more efficient to use the DATA step to store your Quandl data in a SAS data set and then refer to the SAS data set directly in your PROC PRINT or PROC GPLOT statement. You can also refer to the SASEQUAN libref directly, as in the statement
proc print data=quan.oiltd; run;
This statement uses the member name, OILTD, in the PROC PRINT statement; this usage corresponds to specifying the OUTXML=OILTD option. Although using this statement might seem easier, it is not as efficient, because every time you use the SASEQUAN libref, the Quandl interface engine reads the entire XML file into SAS again. So it is better to refer to the SAS data set repeatedly than to invoke the interface engine repeatedly. For another example that uses more SASEQUAN LIBNAME statement options, see the section Reading All Financial Ratios Data.