Programming the Wordle game in SAS

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.

I created this project as a fun exercise to emulate the popular game Wordle in the SAS language. I was inspired by this story about a GitHub user who implemented Wordle in a Bash shell. (See the Bash script here. Read the comments — it’s an amazing stream of improved versions, takes in different programming languages, and “code golf” to reduce the lines-of-code.)

While developing my SAS solution, I created a Twitter poll to ask how other SAS programmers might approach it.

twitter poll
For me it was always going to be arrays, since that’s what I know best. I’d love to be able to dash out a hash object approach or even use SAS/IML, but it would take me too long to wrap my brain around these. The PRX* regex function choice is bit of a feint — regular expressions seem like a natural fit (pattern matching!), but Wordle play has nuances that makes regex less elegant than you might guess. Prove me wrong!

Two SAS users from Japan, apparently inspired by my poll, each implemented their own games! I’ve added links to their work at the end of this article.

How to code Wordle in SAS

You can see my complete SAS code for the game here: sascommunities/wordle-sas.

The interesting aspects of my version of the game include:

  • Uses the “official” word lists from New York Times as curated by cfreshman. I found these while examining the Bash script version. I used PROC HTTP to pull this list dynamically.
  • Also verifies guesses as “valid” using the list of allowed guesses, again curated by cfreshman. You know you can’t submit just any 5 characters as a guess, right? That’s an important part of the game.
  • Uses DATA step array to verify guesses against solution word.
  • Use DATA step object method to create a gridded output of the game board. Credit to my SAS friends in Japan for this idea!

I’ve added a screenshot of example game play below. This was captured in SAS Studio running in SAS Viya.

Example game with output

Example game play for my Wordle in SAS

How to play Wordle in SAS

To play:

  1. Submit the wordle-sas.sas program in your SAS session. This program should work in PC SAS, SAS OnDemand for Academics, SAS Enterprise Guide, and SAS Viya.

    The program will fetch word lists from GitHub and populate into data sets. It will also define two macros you will use to play the game.

  2. Start a game by running:
     %startGame;
    

    This will select a random word from the word list as the “puzzle” word and store it in a SAS macro variable (don’t peek!)

  3. Optionally seed a game with a known word by using an optional 5-character word parameter:
     %startGame(crane);
    

    This will seed the puzzle word (“crane” in this example). It’s useful for testing. See a battery of test “sessions” in wordle-sas-tests.sas

  4. Submit a first guess by running:
    %guess(adieu);
    

    This will check the guess against the puzzle word, and it will output a report with the familiar “status” – letters that appear in the word (yellow) and that are in the correct position (green). It will also report if the guess is not a valid guess word, and it won’t count that against you as one of your 6 permitted guesses.

Use the %guess() macro (one at a time) to submit additional guesses. The program keeps track of your guesses and when you solve it, it shares the familiar congratulatory message that marks the end of a Wordle session. Ready to play again? Use the %startGame macro to reset.

Start a fresh game using Git functions

If you don’t want to look at or copy/paste the game code, you can use Git functions in SAS to bring the program into your SAS session and play. (These Git functions require at least SAS 9.4 Maint 6 or SAS Viya.)

options dlcreatedir;
%let repopath=%sysfunc(getoption(WORK))/wordle-sas;
libname repo "&repopath.";
data _null_;
    rc = gitfn_clone( 
      "https://github.com/sascommunities/wordle-sas", 
      "&repoPath." 
    ); 
    put 'Git repo cloned ' rc=; 
run;
%include "&repopath./wordle-sas.sas";
 
/* start a game and submit first guess */
%startGame;
%guess(adieu);

Suggestions?

I know that my program could be more efficient…perhaps at the cost of readability. Also it’s possible that I have some lingering bugs, although I did quite a bit of testing and bug-fixing along the way. Pro tip: The DATA step debugger (available in SAS Enterprise Guide and in SAS Viya version of SAS Studio) was a very useful tool for me!

Your feedback/improvements are welcome. Feel free to comment here or on the GitHub project.

Wordle games in SAS by other people

SAS users in Japan quickly implemented their own versions of Wordle-like games. Check them out:

The post Programming the Wordle game in SAS appeared first on The SAS Dummy.

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.