BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MattJans
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

 

View solution in original post

4 REPLIES 4
Reeza
Super User

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. 

art297
Opal | Level 21

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

 

Quentin
Super User

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

 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
MattJans
Obsidian | Level 7

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 🙂 Cheers!

 

-Matt

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 976 views
  • 5 likes
  • 4 in conversation