DATA Step, Macro, Functions and more

Who has a good basic macro program structure (specifically for data checking/cleaning)

Reply
Occasional Contributor
Posts: 18

Who has a good basic macro program structure (specifically for data checking/cleaning)

Hi folks! I've been googling and searching the site, but with no luck so far, so thought I'd ask. I'm looking for a good SUGI paper, blog post, site, whatever that has a really basic intro-level overview of writing a good SAS macro for data read-in and data cleaning (e.g. recoding). I've done this kind of thing before but it's been years, so I'm a little rusty, and I want to have a quick reference to share with colleagues. 

 

Problem is, every macro program reference I can find online goes into one detail or another at a level far more detailed that what I need. They either go into details on how the macro processor works, how to do really complicated macros, or how to do macros that aren't for my context.

 

I'm just making a macro or "master file" that calls other programs and runs them in sequence. For example. 

 


%macro check_and_clean ;
  %include _lib-assign.sas /* Sets libraries for this project */
  %include _contents.sas /* Checks number of cases data */
  %include _subset-national.sas /* Subsets from national data to state needed */
  %include _raw-formats /* Includes formats for the "raw" data */
  %include _new-formats /* Includes final formats for the to be used in analysis */
  %include _freq-check.sas /* Checks cat vars needed for analysis */
  %include _means-check.sas /* Checks cont vars needed for analysis */
  %include _weight-check.sas /* Checks weight vars needed for analysis */
  %include _freq-clean.sas /* Cleans cat vars */
  %include _means-clean.sas /* Cleans cont vars */
  %include _combine /* Combines multiple years of data and weights making them ready for analysis */
%mend check_and_clean ;

 

Thanks in advance! Maybe I just need to write the blog post myself. Smiley Happy 

Super User
Posts: 22,875

Re: Who has a good basic macro program structure (specifically for data checking/cleaning)

It's not clear what you're looking for. For a clinical trial for example there may be specific steps but I suspect the steps for other datasets are so unique that a macro won't make sense. 

 

Your example shows a control program which makes no sense to actually embed as a macro, rather it would make sense to have all the %includes with no macro. 

 

You can check LexJansen.com for lots of examples of macros, or how to structure your folders or programs which would fall under code conventions or programming best practices.  

My general programming practice is to avoid macros unless absolutely required, though I also do break up my programs and then use a control program with %INCLUDE to run them all at once if needed. 

PROC Star
Posts: 8,117

Re: Who has a good basic macro program structure (specifically for data checking/cleaning)

I think you might be looking for something like the following: analytics.ncsu.edu/sesug/2010/FF02.Diioriopdf.pdf

 

However, in that paper's first couple of paragraphs, it states (like @Reeza said), know when to and when not to turn a project into a macro. I totally agree that your example would be better off being something like:

 

  %include _lib-assign.sas /* Sets libraries for this project */
  %include _contents.sas /* Checks number of cases data */
  %include _subset-national.sas /* Subsets from national data to state needed */
  %include _raw-formats /* Includes formats for the "raw" data */
  %include _new-formats /* Includes final formats for the to be used in analysis */
  %include _freq-check.sas /* Checks cat vars needed for analysis */
  %include _means-check.sas /* Checks cont vars needed for analysis */
  %include _weight-check.sas /* Checks weight vars needed for analysis */
  %include _freq-clean.sas /* Cleans cat vars */
  %include _means-clean.sas /* Cleans cont vars */
  %include _combine /* Combines multiple years of data and weights making them ready for analysis */

saved as SAS code called something like: check_and_clean.sas

 

FWIW, the only thing I found inadequate in your code was that the %includes didn't specify the paths showing where those various programs reside. One purpose of documenting one's code is so that you, as well as others, can easily discover what is being done and how it is accomplished.

 

Art, CEO, AnalystFinder.com

 

PROC Star
Posts: 1,435

Re: Who has a good basic macro program structure (specifically for data checking/cleaning)

[ Edited ]

I like using macros for modular programming.

 

It's not clear to me what you're asking. It looks like you already have the outline of a structure, which should work fine with the driver macro calling all the other helper macros, e.g.:

%macro check_and_clean ;
  %_lib_assign /* Sets libraries for this project */
  %_contents /* Checks number of cases data */
  %_subset_national /* Subsets from national data to state needed */
  %_raw_formats /* Includes formats for the "raw" data */
  %_new_formats /* Includes final formats for the to be used in analysis */
  %_freq_check /* Checks cat vars needed for analysis */
  %_means_check /* Checks cont vars needed for analysis */
  %_weight_check /* Checks weight vars needed for analysis */
  %_freq_clean /* Cleans cat vars */
  %_means_clean /* Cleans cont vars */
  %_combine /* Combines multiple years of data and weights making them ready for analysis */
%mend check_and_clean ;

Perhaps this paper by Ed Heaton on top-down programming via macros would be of interest: http://analytics.ncsu.edu/sesug/2001/P-813.pdf

 

Highlighted
Occasional Contributor
Posts: 18

Re: Who has a good basic macro program structure (specifically for data checking/cleaning)

Thanks guys! This is really helpful. Guess I don't need a macro per se. Thanks for point that out. And thanks for the additional links and examples. Using the wrong terminology explains why I couldn't find what I was looking for Smiley Happy Cheers!

 

-Matt

Ask a Question
Discussion stats
  • 4 replies
  • 170 views
  • 2 likes
  • 4 in conversation