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
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
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.
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?
Then you don't need to write to files. Follow the standard macro development process:
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.
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
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!
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)?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.