(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 53.2.1.
options validvarname=any;
title 'Retrieve Two Day Weather Forecast for London';
libname mylib "/sasusr/rain/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 53.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 | totalSnow_cm | sunHour | 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 | 2018-05-11 | London | United Kingdom | City of London, Greater London | 1 | 51.5170 | -0.106 | 18 | 64 | 12 | 54 | 0 | 0 | 6 | 24 | 18 | 64 | 9 | 14 | 161 | SSE | 299 | Moderate rain at times | 0.70000 | 57 | 18 | 1016 | 53 | 14 | 58 | 6 | 42 | 14 | 57 | 11 | 17 | 14 | 57 | 87 | 0 | 0 | 87 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | 2018-05-12 | London | United Kingdom | City of London, Greater London | 1 | 51.5170 | -0.106 | 14 | 57 | 7 | 44 | 0 | 0 | 2 | 24 | 14 | 57 | 4 | 6 | 178 | S | 296 | Light rain | 7.20000 | 74 | 17 | 1013 | 79 | 13 | 55 | 9 | 48 | 12 | 54 | 5 | 7 | 12 | 54 | 93 | 0 | 0 | 88 | 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 file name, and the file extension (.xml) is excluded from the file name 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.
You could 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 path name that the XMLMAP= option specifies. The SASERAIN engine appends a numeral to the XML file name 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 53.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.