Help using Base SAS procedures

Generate template SAS code with comments

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Generate template SAS code with comments

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!


Accepted Solutions
Solution
‎12-18-2012 07:11 PM
Super User
Super User
Posts: 6,502

Re: Generate template SAS code with comments

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


All Replies
Super User
Posts: 9,687

Re: Generate template SAS code with comments

Making a macro is a good choice .

Occasional Contributor
Posts: 10

Re: Generate template SAS code with comments

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.

Respected Advisor
Posts: 3,777

Re: Generate template SAS code with comments

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"

Solution
‎12-18-2012 07:11 PM
Super User
Super User
Posts: 6,502

Re: Generate template SAS code with comments

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

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


Occasional Contributor
Posts: 10

Re: Generate template SAS code with comments

Very useful! Was not aware of Resolve. Thanks!

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 373 views
  • 3 likes
  • 4 in conversation