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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

LukaszKul
Calcite | Level 5

Thanks for you reply 🙂

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?



Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

LukaszKul
Calcite | Level 5

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

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

Case closed, thanks!

Kurt_Bremser
Super User

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)?

 

 

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
  • 6 replies
  • 501 views
  • 1 like
  • 3 in conversation