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:
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
12 Comments
thank you very much for this
So glad you found this useful, Lian. May the SAS be with you!
Very useful, thanks for sharing
So glad you found it useful, Suryaprakash 🙂
Mark
Ok - as promised - here's what it would take to get the data2datastep macro:
Now, how would you modify this to permanently save the macro source code to a file?
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";
LIKE!!
Seriously - thanks for this blog,, and those macros; quite slick.
So glad you found it useful!
May the SAS be with you!
Mark
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?
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.
May the SAS be with you!
Mark
%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
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!