BookmarkSubscribeRSS Feed
tomcmacdonald
Quartz | Level 8

Are there guides for "best practices" on how to structure a SAS program?  I'm coming from the python world where I use modules to abstract away parts of my program and import them when needed.  Coming to the SAS world I've been using %include statements as an import alternative.  I don't think putting all the code on a single program is good practice because it's hard to read.  Can any experienced SAS programmers shed some light here?  Thank you.

3 REPLIES 3
Astounding
PROC Star

There are a few ways to look at this, depending on how much code needs to be "modularized" and reused.  Here's a good starting point.

 

When you re-use a section of SAS code, you often want to do it a little differently each time ... change the data set  you are processing, or change some of the variables.  As a result, a common tool is to define SAS macros and call the macros as needed.  By adding parameters to the macro definitions, you can specify (for example) which data set to process when you call the macro.

 

Macros can be stored as separate programs within a folder, and a single line in the program can make all the macros in the folder available if the program requires them.  (No cost in terms of CPU time for any macros that are not needed.) 

 

It takes some comfort with SAS before you write macros.  After all, the macros are generating SAS code and you have to be able to picture what the SAS code should look like.  Macros are a separate, more advanced topics.  There are books and papers available.  That final topic I mentioned, putting macros into a folder, can be searched by looking for "autocall facility".

 

 

Sven111
Pyrite | Level 9

I'd second @Astounding on the Autocall library.  I have a decent number of modular functions I've developed over the years that automates frequent tasks like exporting a SAS dataset to a CSV files, moving files/datasets around or creating/adding to log files, etc. and I setup that Autocall library from my autoexec.sas file so all of my code can use those functions.  You can also create multiple Autocall libraries, so for larger projects that I want to modularize I create each module as a SAS macro file in a different Autocall library unique to that project, then I can call individual modules as macro functions and can still reference my base library of functions.  It's not as formal as what goes on in Python, but it works pretty well for me.

SASKiwi
PROC Star

Just like @Astounding and @Sven111 I also use macros a lot for automating common functions that are frequently repeated.

 

What I would NOT use macro for is for modularising SAS code. My rule of thumb is if a macro does not contain macro statements to generate SAS code then it is completely unnecessary. It complicates your SAS code for no benefit.

 

You can do a lot to make your code more readable by laying out nicely using indentation and splitting statements over several lines. If a SAS program is well laid-out, it doesn't matter if it is a few lines or a few hundred lines - it will still be easy to read and understand.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1011 views
  • 3 likes
  • 4 in conversation