Using %macro to generate programs

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Using %macro to generate programs

Hi!

I would like to automate creating base to most of my programs by using macro that outputs code ready to use, then write it down to .txt file.
The problem is that (as i have found in docs) macro facility resolving tokens do not consider white marks (suchs as enter as brake line).
I've started from easiest version:

%macro generator;
     data _null_;
          a=5;
     run;
%mend generator;

data _null_;
     file ".../readyPGM.txt";
     put "%generator";
run;

The output obviously is like:
    data _null_;     a=5;     run;

Is there any workaround to include break lines? 
Thank you in advance for any tips and all useful opinions.

Lukas


Accepted Solutions
Solution
‎02-02-2018 05:19 AM
Super User
Super User
Posts: 9,397

Re: Using %macro to generate programs

Posted in reply to LukaszKul

Perhaps not incorrect, just maybe not described quite right.  If you have code that operates on various projects, and has some parameters, then just use and include file which contains your code and set a macro variable in your project specific code.  For instance, say I want to print sashelp.class for ABC1234, but for ABC2345 I want to print sashelp.cars:

Include file:

proc print data=sashelp.&study_dataset.;
run;

Study ABC1234 program:

%let study_data=class;

%include "include_file.sas";

data somethingelse...

Study ABC2345:

%let study_data=cars;

%include "include_file.sas";

data somethingelse...

So the generic part of the code is written once in one file then include by the other two programs with them supplying their parameter.  You could of course also macrotise the include file, and then call the macro from each of those programs also.

So from your given example, write the basic steps in one file or macro, then call from the others with a parameter.

 

This is not quite as I had read your post at first and makes far more sense Smiley Happy

View solution in original post


All Replies
Super User
Super User
Posts: 9,397

Re: Using %macro to generate programs

Posted in reply to LukaszKul

For what purpose?  SAS Base is designed to be extremely flexible, hence anything you do is going to limit that.  Macro is not a replacement for Base SAS programming!!  I can't stress this enough, the cause of almost all programming mistakes, problems, and lack of robustness is due to people jumping straight into macro programming, not really understanding what its purpose is, or that Base can do things far simpler and more robustly.  

The usual steps are:

Write a program using Base SAS

Identify sections which repeat either in this code or across code.

These sections can then be extracted and macrotised following a different set of documentation and validation process and then called in the main program.

I am 99% certain what you are attempting will not work, or will cause you far more than just writing the program in the first place.

New Contributor
Posts: 3

Re: Using %macro to generate programs

Thanks for you reply Smiley Happy

I totally understand your point, on some extent jumping straight into macros can be a really bad practice.

As i have mentioned, what i want to achieve is to automate some basic steps that im going to use across different projects. Applying some conditional processing (not very sophisticated, just few parameters to change names/places of outputs) would really improve my programming process and also save some time. Most important sections in code cant be generates as they differ across task, but basic set-up like generating header, some dates to keep track on project etc. could be. 
Is this approach incorrect?



Super User
Posts: 9,868

Re: Using %macro to generate programs

Posted in reply to LukaszKul

Then you don't need to write to files. Follow the standard macro development process:

  • create code that solves a single instance of your issue
  • identify parts of that code that need to be flexible
  • replace those with macro variables, and set the macro vars manually
  • once that works, wrap into a macro, and supply the macro vars as parameters in the %macro statement
  • add macro logic (if necessary) to create derived macro values from the parameters

After that, use the macro anywhere in your programs where needed. Consider storing such macros in the autocall library, or in one of the autoexec locations that SAS provides.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎02-02-2018 05:19 AM
Super User
Super User
Posts: 9,397

Re: Using %macro to generate programs

Posted in reply to LukaszKul

Perhaps not incorrect, just maybe not described quite right.  If you have code that operates on various projects, and has some parameters, then just use and include file which contains your code and set a macro variable in your project specific code.  For instance, say I want to print sashelp.class for ABC1234, but for ABC2345 I want to print sashelp.cars:

Include file:

proc print data=sashelp.&study_dataset.;
run;

Study ABC1234 program:

%let study_data=class;

%include "include_file.sas";

data somethingelse...

Study ABC2345:

%let study_data=cars;

%include "include_file.sas";

data somethingelse...

So the generic part of the code is written once in one file then include by the other two programs with them supplying their parameter.  You could of course also macrotise the include file, and then call the macro from each of those programs also.

So from your given example, write the basic steps in one file or macro, then call from the others with a parameter.

 

This is not quite as I had read your post at first and makes far more sense Smiley Happy

New Contributor
Posts: 3

Re: Using %macro to generate programs

That's a different view that i did not think about Smiley Happy

Changes perspective, i'm definitely gonna rethink whole process using that Smiley Happy

Case closed, thanks!

Super User
Posts: 9,868

Re: Using %macro to generate programs

Posted in reply to LukaszKul

The macro facilty is designed to create transient, dynamic code that is supposed to work but not supposed to be read.

 

Don't abuse it.

 

What are you trying to achieve that isn't already solved by keyboard macros in EG or the tasks in EG and SAS Studio (from where you can fetch the created code and save it as .sas files anyway)?

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 132 views
  • 1 like
  • 3 in conversation