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

Hi all,

I've been writing several SAS programs recently using similar formatting (e.g my name/project name in comment at the beginning, filename variables etc). I was wondering if there is a neat way to write a "SAS template generator" program/macro that could take a username\project name\date\whatever else, and generate a new SAS program file that includes a standard header in comments with this information.

It would be convenient to only have to enter it once, because this information appears several times in each program.

Thanks in advance for any light on this!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

One useful trick is to use the RESOLVE function.

Build a text file with the template and put macro variable references (or even macro calls as long as the macro just generate text).

Then use a data step to read the template, resolve the macro variable references and write the new program.

template.sas


/%str(*)-----------------------------------------------------------------------------

Program: &program

Project: &project

Title: &title

Author: &author

Initial Date: &sysdate

------------------------------------------------------------------------------%str(*)/


%let project=XYZ;

%let program=main.sas ;

%let title = Main SAS program for Project &project;

%let author=A. Programmer ;

data _null_;

  infile 'template.sas' lrecl=300 truncover;

  file "&program" lrecl=300;

  input line $char300.;

  line = resolve(trim(line));

  len = length(line);

  put line $varying300. len ;

run;

main.sas


/*-----------------------------------------------------------------------------

Program: main.sas

Project: XYZ

Title: Main SAS program for Project XYZ

Author: A. Programmer

Initial Date: 17DEC12

------------------------------------------------------------------------------*/


View solution in original post

5 REPLIES 5
Ksharp
Super User

Making a macro is a good choice .

hypermonkey2
Calcite | Level 5

I forgot to mention that I am familiar with macros, I was looking for the best way using that. I think that using the FILE command along with a series of PUT statements could work.

data_null__
Jade | Level 19

I think it depends on how good your meta data is.  If you have useful info that can drive the report then a data step can easily write report.

REPORT="SAS program template"

Tom
Super User Tom
Super User

One useful trick is to use the RESOLVE function.

Build a text file with the template and put macro variable references (or even macro calls as long as the macro just generate text).

Then use a data step to read the template, resolve the macro variable references and write the new program.

template.sas


/%str(*)-----------------------------------------------------------------------------

Program: &program

Project: &project

Title: &title

Author: &author

Initial Date: &sysdate

------------------------------------------------------------------------------%str(*)/


%let project=XYZ;

%let program=main.sas ;

%let title = Main SAS program for Project &project;

%let author=A. Programmer ;

data _null_;

  infile 'template.sas' lrecl=300 truncover;

  file "&program" lrecl=300;

  input line $char300.;

  line = resolve(trim(line));

  len = length(line);

  put line $varying300. len ;

run;

main.sas


/*-----------------------------------------------------------------------------

Program: main.sas

Project: XYZ

Title: Main SAS program for Project XYZ

Author: A. Programmer

Initial Date: 17DEC12

------------------------------------------------------------------------------*/


hypermonkey2
Calcite | Level 5

Very useful! Was not aware of Resolve. Thanks!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3552 views
  • 4 likes
  • 4 in conversation