Jedi SAS Tricks: GIT my macros

12

I use macros extensively in my SAS programs, and over the years have accumulated a few that I find quite useful. The integration of GIT and SAS Studio has made it easy to build a re-usable macro library, so I've put some of the more polished macros I've written in a GIT repository to share. You can generate a list of the macro files available with this bit of SAS code (which leverages one of those macros)

%let thisMacro=listrepofiles;
filename mcode temp;
proc http url="https://raw.githubusercontent.com/SASJedi/sas-macros/master/%qlowcase(&thisMacro).sas"
          out=mcode;
run;
%include mcode /source2;
filename mcode;
%&thismacro(?)

Running that program makes the listRepoFIles macro available in your SAS session and provides syntax help in the LOG:

NOTE:  *LISTREPOFILES Documentation *******************************

       SYNTAX: %LISTREPOFILES(user,repo,branch,extension)
          user = Username for repo owner (Case sensitive)
          repo = Repo name (Case sensitive)
          branch = branch (Optional, default is master)
          extension = file extension (Optional, default gets all)

       Examples:
       Retrieve a list of all files in the master branch of
       SASJedi repo named sas-macros:
       %LISTREPOFILES(SASJedi,sas-macros)

       Retrieve a list of SAS files in the master branch of
       SASJedi repo named sas-macros:
       %LISTREPOFILES(SASJedi,sas-macros,,sas)

       *************************************************************

NOTE:  Use %LISTREPOFILES(?) for help.

Submitting the code copied straight from the last example in macro documentation:

%LISTREPOFILES(SASJedi,sas-macros,,sas)

will get you a list of the macro source files currently stored in the repository. I'll share a few here:

List of sas files in the sas-macros repository master branch
Obs path size url
1 benchmark.sas 6255 https://api.github.com/repos/SASJedi/sas-macros/git/blobs/3c6fe9cc8db1264f5e1b6978298ac25e970488ea
2 benchmarkparse.sas 30872 https://api.github.com/repos/SASJedi/sas-macros/git/blobs/0fe16b5de7ce187715bf91ae48e6b0f1ab7b3e0e
3 benchmarkreport.sas 7047 https://api.github.com/repos/SASJedi/sas-macros/git/blobs/e7e75e0ab6c8eacf8ad918680823dc7cd64c2210
4 charcollength.sas 1068 https://api.github.com/repos/SASJedi/sas-macros/git/blobs/fd48d0a1aeefd3b10e705977c61cb7cd1efb7a2e
5 charcollengths.sas 1130 https://api.github.com/repos/SASJedi/sas-macros/git/blobs/4738b57753d4b2af8f34f579a8322598c3d0b6f3
6 data2datastep.sas 4756 https://api.github.com/repos/SASJedi/sas-macros/git/blobs/a1708f9231305306c7a2c517cb665ca546999728

Can you download and get syntax help for the DATA2DATASTEP macro by modifying the very first program in this post? I'll post my solution for that in the comment section in a few days - but don't peek right away! Why not try it yourself first? 

May the SAS be with you!
Mark

Tags
Share

About Author

SAS Jedi

Principal Technical Training Consultant

Mark Jordan (a.k.a. SAS Jedi) grew up in northeast Brazil as the son of Baptist missionaries. After 20 years as a US Navy submariner pursuing his passion for programming as a hobby, in 1994 he retired, turned his hobby into a dream job, and has been a SAS programmer ever since. Mark writes and teaches a broad spectrum of SAS programming classes, and his book, "Mastering the SAS® DS2 Procedure: Advanced Data Wrangling Techniques" is in its second edition. When he isn’t writing, teaching, or posting “Jedi SAS Tricks”, Mark enjoys playing with his grand and great-grandchildren, hanging out at the beach, and reading science fiction novels. His secret obsession is flying toys – kites, rockets, drones – and though he usually tries to convince Lori that they are for the grandkids, she isn't buying it. Mark lives in historic Williamsburg, VA with his wife, Lori, and Stella, their cat. To connect with Mark, check out his SAS Press Author page, follow him on Twitter @SASJedi or connect on Facebook or LinkedIn.

12 Comments

  1. Ok - as promised - here's what it would take to get the data2datastep macro:

    %let thisMacro=data2datastep;
    filename mcode temp;
    proc http url="https://raw.githubusercontent.com/SASJedi/sas-macros/master/%qlowcase(&thisMacro).sas"
              out=mcode;
    run;
    %include mcode /source2;
    filename mcode;
    %&thismacro(?)
    

    Now, how would you modify this to permanently save the macro source code to a file?

    • SAS Jedi

      To permanently save the macro source code to a local file, use a real file name in the FILENAME statement instead of TEMP. For example, to save the file to your c:\my macros folder, something like this would do:
      filename mcode "c:\my macros\%qlowcase(&thisMacro).sas";

  2. Well that should say that the next part doesn't work, and I suspect that the PROC HTTP in the command
    %LISTREPOFILES(SASJedi,sas-macros)

    also needs those options.

    Is there a way to include those options in %LISTREPOFILES?

    • SAS Jedi

      As you wish, Paige Miller! I've updated the macro in the Github repo to accept four keyword parameters: proxyhost=, proxyport=,proxyusername=, and proxypassword=.
      Download and compile the macro code again just as you did before. Calling %listrepofiles(?) will now include syntax help for the new parameters. Let me know if it works for you or not.

      NOTE:  *LISTREPOFILES Documentation *******************************
      
             SYNTAX:
      %LISTREPOFILES(user,repo)
      
                Required parameters: (Case sensitive!)
                  user = Username for repo owner
                  repo = Repo name
      
                Optional parameters:
                  branch = branch                             (default: master)
                  extension = file extension                  (default: all files)
                  proxyhost = Proxy server host name          (default: option not used)
                  proxyport = Proxy server port number        (default: option not used)
                  proxyusername = Your proxy server user name (default: option not used)
                  proxypassword = Your proxy server password  (default: option not used)
      
             Examples:
             Retrieve a list of all files in the master branch of the
             SASJedi repo named sas-macros:
             %LISTREPOFILES(SASJedi,sas-macros)
      
             Retrieve a list of SAS files in the master branch of the
             SASJedi repo named sas-macros:
             %LISTREPOFILES(SASJedi,sas-macros,,sas)
      
             Proxy-related parameters are keyword parameters. For example,
             to retrieve a list of all files in the master branch of the
             SASJedi repo named sas-macros using a proxy server:
             %LISTREPOFILES(SASJedi,sas-macros,proxyhost=myhost.example.com,proxyport=898)
      
             *************************************************************

      May the SAS be with you!
      Mark

  3. %let thisMacro=listrepofiles;
    filename mcode temp;
    proc http url="https://raw.githubusercontent.com/SASJedi/sas-macros/master/%qlowcase(&thisMacro).sas"
    out=mcode;
    run;
    %include mcode /source2;
    filename mcode;
    %&thismacro(?)

    This part of your code works for me if I modify the PROC HTTP statement to have the PROXYHOST= and PROXYPORT= and PROXYUSERNAME= and PROXYPASSWORD= options. Great.

    The next part

    • SAS Jedi

      If you are running this code from a work environment, it's not unusual to need to authenticate to your proxy server; A proxy server acts as a gateway between your computer and the internet, usually acting as a firewall and web filter to protect you from malicious internet content. The PROC HTTP code downloads a file from the internet, hence the need to authenticate. Looks like you found the PROC HTTP options and the company security information required to provide the proper values. Great!

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top