The SASERAIN Interface Engine
Example 52.2 Retrieving the Two-Day Local Weather Forecast for One Location
(View the complete code for this example.)
The statements that follow enable you to access the weather for London for two days (NUM_OF_DAYS=2), which starts with today. The observations are given at a frequency of every 24 hours and are sorted in chronological order. The output is shown in Output 52.2.1.
options validvarname=any; title 'Retrieve Two Day Weather Forecast for London'; libname _all_ clear; libname mylib "U:\rain940\doc\"; libname rain saserain "%sysget(RAIN_DATA)" QUERY='London,United Kingdom' OUTXML=foggy XMLMAP="%sysget(RAIN_DATA)foggy.map" APIKEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' NUM_OF_DAYS=2 TP=24 FORMAT=xml; data mylib.london_fog; set rain.foggy; run;
proc contents data=mylib.london_fog; run; proc print data=mylib.london_fog; run;
Output 52.2.1: London Weather for Today and Tomorrow: London_fog
| Retrieve Two Day Weather Forecast for London |
| Obs | date | AreaName | Country | Region | oc | latitude | longitude | maxtempC | maxtempF | mintempC | mintempF | uvIndex | time | tempC | tempF | windspeedMiles | windspeedKmph | winddirDegree | winddir16Point | weatherCode | weatherDesc | precipMM | humidity | visibility | pressure | cloudcover | HeatIndexC | HeatIndexF | DewPointC | DewPointF | WindChillC | WindChillF | WindGustMiles | WindGustKmph | FeelsLikeC | FeelsLikeF | chanceofrain | chanceofremdry | chanceofwindy | chanceofovercast | chanceofsunshine | chanceoffrost | chanceofhightemp | chanceoffog | chanceofsnow | chanceofthunder |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2016-10-18 | London | United Kingdom | City of London, Greater London | 1 | 51.5170 | -0.106 | 14 | 57 | 6 | 43 | 2 | 24 | 14 | 57 | 14 | 23 | 284 | WNW | 113 | Sunny | 1.30000 | 79 | 10 | 1020 | 5 | 13 | 55 | 5 | 40 | 11 | 51 | 18 | 30 | 11 | 51 | 95 | 0 | 0 | 2 | 94 | 0 | 0 | 0 | 0 | 0 |
| 2 | 2016-10-19 | London | United Kingdom | City of London, Greater London | 1 | 51.5170 | -0.106 | 14 | 57 | 7 | 44 | 2 | 24 | 14 | 57 | 11 | 18 | 311 | NW | 119 | Cloudy | 0.00000 | 79 | 10 | 1022 | 67 | 14 | 57 | 8 | 46 | 12 | 53 | 16 | 26 | 12 | 53 | 2 | 0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 |
The XML data that the World Weather Online website returns are placed in a file that is named by the OUTXML= option—in this case, FOGGY1.xml.
Note: The SASERAIN engine appends a numeral to the XML filename, and the file extension (.xml) is excluded from the filename that appears in the OUTXML= option. The SAS data set created when the XML data file is read into SAS is placed in the location that is specified inside the string enclosed in double quotation marks in the SASERAIN LIBNAME statement. So, in the preceding example, if the RAIN_DATA environment variable is set to
U:\rain940\test\
, then the downloaded XML file is located at
U:\rain940\test\FOGGY1.xml
.
An equivalent LIBNAME statement that does not use any environment variables could be as follows:
libname rain saserain "U:\rain940\test\" OUTXML=foggy XMLMAP="U:\rain940\test\foggy.map" APIKEY='XXXXXXXXXXXXXXXXXXXX' QUERY='London,United Kingdom';
You could also use either a SAS macro variable or a system environment variable to store the value of your World Weather Online 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 SASERAIN engine appends a numeral to the XML filename to indicate the position of the World Weather Online location code in the QUERY= option.
The QUERY= option specifies the list of World Weather Online locations that you want to retrieve weather data for. This option accepts a string, enclosed in single quotation marks, that consists of one or more World Weather Online locations that you select (keep) in the resulting SAS data set. The result, FOGGY, is named in the DATA step and is shown in Output 52.2.1. The preceding example uses only one World Weather Online code, which is in the first position of the QUERY= option, so the numeral 1 is appended to the name of the XML file, resulting in FOGGY1.xml.
It is more efficient to use the DATA step to store your World Weather Online 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 SASERAIN libref directly, as in the statement
proc print data=rain.foggy;
This statement uses the member name, FOGGY, in the PROC PRINT statement; this usage corresponds to specifying the OUTXML=FOGGY option. Although using this statement might seem easier, it is not as efficient, because every time you use the SASERAIN libref, the SASERAIN 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.
Copyright © SAS Institute Inc. All rights reserved.