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!

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

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
  • 807 views
  • 4 likes
  • 1 in conversation