BookmarkSubscribeRSS Feed
ChrisHemedinger
Community Manager

On this Talk Like A Pirate Day, I decided to share a fun example of how to use SAS and a fun free API to generate some pirate-y insults and translations. For these I'm leveraging the APIs I found here: https://pirate.monkeyness.com/api.html.

 

The format is simple: use PROC HTTP to call the API, capture the output in a text file, and then read that into a data set:

 

    filename rrr temp;

    proc http
      url="https://pirate.monkeyness.com/api/insult"
      out=rrr;
    run;

    data insult;
      infile rrr;
      length insult $ 255;
      input;
      insult=_infile_;
    run;

    filename rrr clear;

 

 

Run that once to get one good-natured pirate insult. Want a bunch of these? Here's a macro routine to get more:

 

/* Insults! */
%macro pirateInsults(howMany);
  %do i=1 %to &howMany.;
    filename rrr temp;

    proc http
      url="https://pirate.monkeyness.com/api/insult"
      out=rrr;
    run;

    data r_&i;
      infile rrr;
      length insult $ 255;
      input;
      insult=_infile_;
    run;

    filename rrr clear;
  %end;

  /* combine insults into compendium */
  data insults;
    set r_:;
  run;

  /* delete temp data */
  proc datasets lib=work nodetails nolist;
    delete r_:;
  quit;

%mend;

%pirateInsults(5);

 

 

pirateinsults.jpg

 

This team offers a similar API for translations. This code translates English to "Pirate":

 

 

%let phrase=Hey! You! Get off of my boat.;
filename rrr temp;
proc http 
 url="https://pirate.monkeyness.com/api/translate?english=%sysfunc(urlencode(&phrase))"
 out=rrr;
run;

data r;
  infile rrr;
  length insult $ 255;
  input;
  insult=_infile_;
run;

filename rrr clear;

 

 

Output: "Ahoy! Ye! Get off o' me ship."

 

We can drive more phrases through using SAS macro and CALL EXECUTE.

 

 

data inputs;
 infile datalines dsd;
 length english $ 200;
 input english;
datalines;
I know what you did
Get your hands off of my treasure
Hey! You! Get off of my boat.
;
run;

%macro pirateTranslate(phrase,n);
  filename rrr temp;
  proc http 
   url="https://pirate.monkeyness.com/api/translate?english=%sysfunc(urlencode(&phrase))"
   out=rrr;
  run;

  data r_&n;
    infile rrr;
    length pirate $ 255;
    input;
    pirate=_infile_;
  run;

  filename rrr clear;
%mend;

data _null_;
  set inputs;
  call execute('%pirateTranslate('||english||','||_n_||');');
run;

data allTranslations;
  set r_:;
run;

/* delete temp data */
proc datasets lib=work nodetails nolist;
  delete r_:;
quit;

 

 

piratetranslate.jpg

 

Got your own examples? Ahoy! Share it w' the crew!

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 0 replies
  • 1006 views
  • 4 likes
  • 1 in conversation