But since I'm living in SAS these days -- not just the place (at SAS headquarters), but the software -- I decided to see if I could use my SAS tools to "find" some Pokémon in my work. Thanks to PROC HTTP and fantastic service called the Pokéapi, I've managed some success.
Calling the Pokéapi REST API with SAS
PROC HTTP is the the SAS procedure that you can use to call REST APIs. And the Pokéapi site is a REST API that yields on-demand information about our new favorite creatures. Here's a quick example:
/* utility macro to put file contents to SAS log */ %macro echoResp(fn=); data _null_; infile &fn; input; put _infile_; run; %mend; filename resp temp; /* Call the Pokeapi to list all available Pokemon */ proc http url="http://pokeapi.co/api/v2/pokemon/?limit=1000" out=resp method="GET"; run; %echoResp(fn=resp);
Here's a snippet of my "Pokémon log":
I need a DATA step to read and parse some of the API response, which is in JSON. I'm using a simple INFILE with SCANOVER to parse out just a few bits and create a data set of all the character names (811 of them). The API response is basically one huge line of text, so I'm using the @@ directive to keep the INPUT statement working on the same "record."
data pokemon; infile resp lrecl=65635 scanover truncover; length name $ 20; input @'"name":' name $quote20. @@; run;
If you're using the free SAS University Edition, this code should work there too! The Pokéapi site is accessed using HTTP and not HTTPS. (HTTPS doesn't work from SAS University Edition because the secure/encryption components are not included.)
I can also use PROC HTTP and the API to gather an incredible amount of detail about each character. I found Jigglypuff at record 39, so here's my code to retrieve and parse some more details. Note that there are hundreds of attributes available for each character, and I'm pulling just a couple of them.
proc http url="http://pokeapi.co/api/v2/pokemon/39" out=resp method="GET"; run; data jiggly; infile resp lrecl=500000 scanover truncover; length weight 8 base_experience 8; input @'"weight":' weight 2. @@; input @'"base_experience":' base_experience 2. @@; run;
And the results:
Going to "the source" for raw Pokémon data
Parsing JSON using SAS is fun and all, but sometimes you just want access to the raw data. And it turns out that the Pokéapi folks have a project on GitHub with everything we need. We can use PROC HTTP to get to that too! And then use SAS to join and analyze/visualize the results! These calls are to the GitHub site to access the "raw" view of data files in the repository. Note: GitHub does use HTTPS (sorry, SAS University Edition users...).
filename pk_csv "%sysfunc(getoption(WORK))/pokemon.csv"; proc http url="https://raw.githubusercontent.com/PokeAPI/pokeapi/master/data/v2/csv/pokemon.csv" method="GET" out=pk_csv; run; proc import file=pk_csv out=pokemon dbms=csv replace; run; filename pk_ab "%sysfunc(getoption(WORK))/pokemon_ab.csv"; proc http url="https://raw.githubusercontent.com/PokeAPI/pokeapi/master/data/v2/csv/pokemon_abilities.csv" method="GET" out=pk_ab; run; proc import file=pk_ab out=abilities dbms=csv replace; run; filename pk_abn "%sysfunc(getoption(WORK))/pokemon_abnames.csv"; proc http url="https://raw.githubusercontent.com/PokeAPI/pokeapi/master/data/v2/csv/abilities.csv" method="GET" out=pk_abn; run; proc import file=pk_abn out=abnames dbms=csv replace; run; /* Join the Pokemon with their abilities */ proc sql; create table work.withabilities as select t3.identifier as pokemon, t1.identifier as ability from work.abilities t2, work.pokemon t3, work.abnames t1 where (t2.pokemon_id = t3.id and t2.ability_id = t1.id); quit; ods graphics on / height=1000 width=600; proc freq data=work.withabilities order=freq ; tables ability / nocum scores=table plots(only orient=horizontal)=freq; run;
Here's what PROC FREQ shows about how common some of the abilities are among the Pokémon. "Levitate" appears to be common (good thing, because I'm not sure that they all have legs), and "slow start" is less common (but that's an ability that I think I can claim for myself...).
Full code: I placed all code presented here in a public Gist on GitHub. Enjoy!
The post Build your Pokémon library using SAS and the Pokéapi appeared first on The SAS Dummy.