Skip to content

Instantly share code, notes, and snippets.

@cjdinger
Last active July 19, 2016 20:39
Show Gist options
  • Save cjdinger/0bbd1b08725035e96bc135d9c759a192 to your computer and use it in GitHub Desktop.
Save cjdinger/0bbd1b08725035e96bc135d9c759a192 to your computer and use it in GitHub Desktop.
/* Example code to access Pokemon data */
/* Author: Chris Hemedinger */
/* See explanation at: */
/* http://blogs.sas.com/content/sasdummy/build-your-pokemon-library-using-sas */
/* First an example that uses the Pokeapi from SAS */
/* 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);
data pokemon;
infile resp lrecl=65635 scanover truncover;
length name $ 20;
input @'"name":' name $quote20. @@;
run;
/* Get just the data for Jigglypuff */
proc http
url="http://pokeapi.co/api/v2/pokemon/39"
out=resp
method="GET";
run;
%echoResp(fn=resp);
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;
/* Now an example that uses the raw data from GitHub */
/* Location of the PokeAPI project on GitHub */
%let githubRoot=https://raw.githubusercontent.com/PokeAPI/pokeapi/master/data/v2/csv;
/* temp location for CSV files, works on UNIX or Windows */
%let csvOut = %sysfunc(getoption(WORK));
filename pk_csv "&csvOut./pokemon.csv";
proc http
url="&githubRoot./pokemon.csv"
method="GET"
out=pk_csv;
run;
proc import file=pk_csv out=pokemon dbms=csv replace;
guessingrows=max;
run;
filename pk_ab "&csvOut./pokemon_ab.csv";
proc http
url="&githubRoot./pokemon_abilities.csv"
method="GET"
out=pk_ab;
run;
proc import file=pk_ab out=abilities dbms=csv replace;
guessingrows=max;
run;
filename pk_abn "&csvOut./pokemon_abnames.csv";
proc http
url="&githubRoot./abilities.csv"
method="GET"
out=pk_abn;
run;
proc import file=pk_abn out=abnames dbms=csv replace;
guessingrows=max;
run;
/* Join the 3 data sets */
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;
/* Frequency of Abilities among the characters */
proc freq data=work.withabilities noprint
order=freq
;
tables ability / nocum scores=table plots(only orient=horizontal)=freq out=ability_freq;
run;
/* Create a more readable plot based on the most common abilities */
ods graphics on / height=800 width=800;
title "Most common abilities among Pokemon";
proc sgplot data=ability_freq (obs=20);
hbar ability / response=count barwidth=.4;
yaxis discreteorder=data display=(nolabel);
xaxis label="# Pokemon who possess it" grid ;
run;
/* Join the FREQ output with pokemon names and abilities */
/* for a report on "rare" abilities */
proc sql;
create table rareabilities
as select t1.pokemon, t1.ability from
withabilities t1 inner join ability_freq t2 on
(t1.ability=t2.ability AND t2.count=1)
order by t1.ability;
quit;
title "Rare abilities among Pokemon (each possessed by only one character)";
proc print data=rareabilities noobs;
var ability pokemon;
run;
@cjdinger
Copy link
Author

Updated on 15-July-2016 to make a slightly nicer chart and a tabular report of "rare" abilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment