DATA Step, Macro, Functions and more

Identifying a SAS program

Posts: 0

Identifying a SAS program

I am looking for a piece of code that could indentify the SAS program generating an output (eg an output generated by proc tabulate), and put the path and program name in a footnote to the output.

Posts: 0

Re: Identifying a SAS program

1 the SYSIN option for the code invoked in batch mode,
2 environment variables SAS_ExecFileName and SAS_ExecFilePath for the name of the program opened through the "file open" dialog of SAS Enhanced Editor[pre] %let this_is= %sysget(SAS_ExecFilePath) %sysfunc(getoption(sysin));
option symbolgen ;
footnote2 "&this_is ";[/pre]
Otherwise you could make your own arrangements (like filling a macro variable like &this_prog_is).

good luck

Super User
Posts: 10,511

Re: Identifying a SAS program

I wrote a macro to do this using titles. It may be overkill, but the issue I ran into was existing titles that were defined, avoiding duplication of the id text and the limitation on the number of title lines available. I am including my macro below. It should be relatively easy to change to use footnotes instead of titles chaning the references TYPE='T' to TYPE='F'.

/*Purpose: */
/*This macro puts the name of the currently executing program name */
/*to the current out put destination using a title statement */
/* Programmer: Ward Ballard Sep 2007 */
/* Use: */
/* Syntax %sourceid; Currently no options or parameters */
/* included in the SAS code file. The file MUST have a name */
/* (i.e. saved) at some time prior to the code execution. */
/* Needs to be executed from the Enhanced Program Editor. */
/* Best if placed in a directory included in AUTOCALL, probably the*/
/* initial data directory. */
/* Place the call AFTER any desired titles and BEFORE any proc */
/* step as this includes proc calls. */
/* Only works where TITLE is used. */
/* If the user creates a new title with a gap from the last time */
/* the macro is called, i.e. Title xxx; %SOURCEID; TITLE5 YYY. */
/* The previous call to sourceid is still a valid title and the */
/* titles for both instance will appear. */
/* I have not tested how this will work with %INCLUDE files, but */
/* I think if the macro is called within the included file that is */
/* the name reported. */
/*Challenges: Making sure that theren't aren't blank title lines */
/* and that there is a way of handling 10 existing title lines and */
/* that we don't get redundant source statements if executed many */
/* times. */
/* Initially attempted to use other methods of getting the number */
/* of assigned titles but the dataset functions do not work */
/* as needed with the SASHELP.VTITLE data view that has title info */
/* this could be modified to do FOOTNOTES instead by changing the */
/* places where TYPE='T' to TYPE='F'. Remember that Footnotes will */
/* be at bottom of listing in the output window and will tend to */
/* pad the output lines used to match pagesize. This can result in */
/* many blank lines depending upon your current pagesize setting. */
/* Each ODS output type seems to treat footnotes slightly different*/
/* You have been warned. */
/* If you use "Source file:" as part of your titles for some reason*/
/* replace that text with something else in the macro below (could */
/* be done as a parameter but be carefull of frequent changes) */
/* example use: */
/* */
/* Title1 "A summary of some variables by some others"; */
/* Title2 "Among some demographic groups"; */
/* %sourceid; (must come after the title statements & before proc) */
/* */
/* Proc Freq data=mydataset; */
/* table (demo1 demo2)*(var1*var2 var1*var3); */
/* run; */
/* */
/* Result: */
/* */
/* A summary of some variables by some others */
/* Among some demographic groups */
/* Source file: D:\Data\project\ */
/* */
/* (Freq tables) */
/* */

%macro SourceId;
%local nobs tnobs ttext pos;
proc sql NOPRINT;
select max (number) into :nobs
where type='T';
proc sql NOPRINT;
select TEXT into :TTEXT
WHERE NUMBER=&nobs and type='T';
%let nobs=%left(&nobs);
%*Check to see if the title already starts with Source file:;
%*If so, then reuse that title line. This improves utility ;
%*by not creating duplicate titles. Note that this does not ;
%*check to see if the source is actually the same as you ;
%*want to change the title when the file changes. ;

%put Initial nobs is &nobs ttext is &ttext;
%let pos = %index(&ttext,Source fileSmiley Happy;
%if &pos = 1 %then %do; %* previous title was only source file cite;
%let ttext = Source file: %sysget(SAS_EXECFILEPATH);
title&nobs "&ttext" ;
%Else %if &pos > 1 %then %do; %* previous title had other text with source file;
%* the following line has an extr ) at the end that is treated as text;
%* this is to maintain the appearance as title10 statment below, the start parenthese is in the substr ;
%let ttext = %substr(&ttext,1,%eval(&pos-1)) Source file: %sysget(SAS_EXECFILEPATH));
title&nobs "&ttext";
%else %do; %* The text Source file is not in the title;

%if &nobs lE 0 %then %let nobs=1;
%if &nobs < 10 %then %DO;
title&nobs "Source file: %sysget(SAS_EXECFILEPATH)" ;
%IF &NOBS = 10 %THEN %DO;
Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation