DATA Step, Macro, Functions and more

How should a SAS program be structured?

Reply
Occasional Contributor
Posts: 6

How should a SAS program be structured?

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.

Super User
Posts: 5,071

Re: How should a SAS program be structured?

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".

 

 

Frequent Contributor
Posts: 82

Re: How should a SAS program be structured?

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.

Super User
Posts: 3,101

Re: How should a SAS program be structured?

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.

 

Ask a Question
Discussion stats
  • 3 replies
  • 119 views
  • 3 likes
  • 4 in conversation