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!
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
------------------------------------------------------------------------------*/
Making a macro is a good choice .
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.
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"
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
------------------------------------------------------------------------------*/
Very useful! Was not aware of Resolve. Thanks!
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.
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.
Ready to level-up your skills? Choose your own adventure.