Sander Ehmsen
Sander Ehmsen

Reputation: 83

SAS: Connect to OpenWeatherMap using API

I need to connect to OpenWeatherMap using their API and the SAS-system. The API can be called using the following adress api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=XXXXXXXXXXXXX

I have ealier connected to another API using:

filename dst url 'http://api.statbank.dk/v1/data/AUS08/CSV?OMR%C3%85DE=*&SAESONFAK=*&Tid=*';

data AUS08_0;
        infile dst dlm=';' encoding="ANY";
        format område $50. SAESONFAK $50. Tid $50. SAESONFAK $50.  indhold $50.;
        input Område SAESONFAK tid  indhold;
run;

I thus thought I could use a similar code, but without any luck. The output from the API-call is given by:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":701,"main":"Mist","description":"mist","icon":"50n"}],"base":"stations","main":{"temp":271.31,"pressure":1033,"humidity":64,"temp_min":268.15,"temp_max":274.15},"visibility":10000,"wind":{"speed":1.5,"deg":110},"clouds":{"all":0},"dt":1480447200,"sys":{"type":1,"id":5088,"message":0.0031,"country":"GB","sunrise":1480405363,"sunset":1480434929},"id":2643743,"name":"London","cod":200}

Can anybody help me here?

Upvotes: 1

Views: 192

Answers (1)

Joe
Joe

Reputation: 63424

Looks like it returns a JSON file, as opposed to a delimited text file. You'll need to parse the JSON file.

Depending on your version of SAS, you have several options.

If you have 9.4 TS1M4 (the current version as of time of writing), there is a newly created JSON libname (which I have no experience with, but it's mentioned in the documentation here).

If you have 9.4 TS1M3, you can use the DS2 built-in JSON package to read the JSON file in PROC DS2.

If you have 9.3+, you can use PROC GROOVY to parse it using the GROOVY language, which has built-in JSON parsing options.

And with any version you can always text parse the file, as it's a fairly straightforward file to parse, though there is some complexity with nested data structures.

See this question and answers for examples of the last two.

You also may want to consider using PROC HTTP to retrieve your results to a file rather than the data step, if you do one of the first three options.

Upvotes: 3

Related Questions