A SAS function that extracts name of a SAS file

Reply
Contributor
Posts: 61

A SAS function that extracts name of a SAS file

hi,

My name is Chang. I am generating multiple tables in a single RTF file using PROC REPORT. For each table, I would like to add information about the program, programmer, and the date of generating the table in its footnote. As shown in the codes below, the first information (Program Name), comes from copied text. I am wondering if this can be automated. Perhaps there is a SAS function that can extract file name so I do not have to type up this information? I have a lot of tables to work on. It would be great if I can maximize automation. I have attached a screen shot with the footnote below the table. Thanks in advance

footnote j=l

            font='Times New Roman'

            h=10pt "Program Name:    Olsaa_dataList_03a_d01_table02.sas   

                         Programmer: Ken Chang   

                         Date:%sysfunc(date(),worddate18.)";

Attachment
Grand Advisor
Posts: 17,396

Re: A SAS function that extracts name of a SAS file

Contributor
Posts: 61

Re: A SAS function that extracts name of a SAS file

hi Reeza,

Thanks for you reply. The %sysget(SAS_execfilename) returns the name of the submitted file. My codes are different in that each table is customised in a separate SAS file and all the table files are executed in a master file that creates a single RTF file containing all the tables. I've put your function in each separate file and I ended up getting the name of the RTF program in each table. The syntax organisation of the RTF file is as below. It's basically some ODS RTF codes with some %include statements sandwiched between ODS RTF and ODT RTF close.

ODS RTF file=;

%include "&dir_project.\&dir_codes.\Olsaa_dataList_03a_d01_table01.sas";

%include "&dir_project.\&dir_codes.\Olsaa_dataList_03a_d01_table02.sas";

%include "&dir_project.\&dir_codes.\Olsaa_dataList_03a_d01_table03.sas";

ODS rtf close;

What has been put is

  • Olsaa_dataList_04_make_one_report.sas under table01
  • Olsaa_dataList_04_make_one_report.sas under table02
  • Olsaa_dataList_04_make_one_report.sas under table03

However, what I want to do is to put

  • Olsaa_dataList_03a_d01_table01.sas under table01
  • Olsaa_dataList_03a_d01_table02.sas under table02
  • Olsaa_dataList_03a_d01_table03.sas under table03

Any idea?

Esteemed Advisor
Posts: 6,698

Re: A SAS function that extracts name of a SAS file

Do

%let called_program_name=Olsaa_dataList_03a_d01_table01.sas;

before calling that program.

Then you can use &called_program_name in each of the includes.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,775

Re: A SAS function that extracts name of a SAS file

When you use a file path in a %INC as in your example SAS creates an internal FILEREF that you can query from DICTIONARY.TABLES.  The "only" problem is that this code needs to be in executed from within each %INCed file.  It can be a macro call but you will still need to modify each Olsaa_dataList_03a_d01_tableNN.sas file.

This is a self contained example that creates the INC file in the program that includes it.

filename FT15F001 '~/includetest.sas';
parmcards4;
proc print data=sashelp.vextfl;
   run;
proc sql;
  
reset noprint outobs=1 nowarn;
   create table _incname_ as select * from dictionary.extfiles where fileref eqt '#LN' order by fileref desc;
  
reset outobs=max warn;
   select xpath into :includefilepath from _incname_;
   drop table _incname_;
   quit;
  
run;
%put NOTE: &=includefilepath;
;;;;
  
run;
%include '~/includetest.sas';
Grand Advisor
Posts: 9,593

Re: A SAS function that extracts name of a SAS file

Here is a workaround . But you need get rid of the quote around &fname . or you can use compress() to remove them . Or you can make a main.sas which combine them all together , then %include main.sas ;  immediately .

c:\temp\Olsaa_dataList_03a_d01_table01.sas

c:\temp\Olsaa_dataList_03a_d01_table02.sas

c:\temp\Olsaa_dataList_03a_d01_table03.sas

contain :

footnote j=l

            font='Times New Roman'

            h=10pt "Program Name:    &fname.  

                         Programmer: Ken Chang  

                         Date:%sysfunc(date(),worddate18.) ";

proc print data=sashelp.class;run;

Here is the main code :

ODS RTF file='c:\temp\x.rtf';

data _null_;

infile cards truncover;

input fname $100. ;

infile dummy filevar=fname end=last;

call execute('%let fname='||scan(fname,-1,'\')||';');

do until(last);

input;

call execute('%nrbquote('||_infile_||')');

end;

cards;

c:\temp\Olsaa_dataList_03a_d01_table01.sas

c:\temp\Olsaa_dataList_03a_d01_table02.sas

c:\temp\Olsaa_dataList_03a_d01_table03.sas

;

run;

ODS rtf close;

Xia Keshan

Message was edited by: xia keshan

Contributor
Posts: 61

Re: A SAS function that extracts name of a SAS file

hi Keshan,

I have tried to run your code but got an error "invalid physical name" in the SAS log. The sas file has been copied to the temp folder. Here is the code I run:

data _null_;

    infile cards truncover;

    input fname $100. ;

    infile dummy filevar=fname end=last;

    call execute("%let fname= '||scan(fname,-1,'\')||';");

        /*call execute allows character data to be sent to SAS macro facility*/

    do until(last);

        input;

        call execute("%nrbquote('||_infile_||')");

            /*%NRBQUOTE masks special characters, including & and %*/

    end;

cards;

C:\temp\Olsaa_dataList_03a_table03b.sas

;

run;

Here is the error message:

5945  data _null_;

5946      infile cards truncover;

5947      input fname $200. ;

5948      infile dummy filevar=fname end=last;

5949

5950      call execute("%let fname= '||scan(fname,-1,'\')||';");

5951          /*call execute allows character data to be sent to SAS macro facility*/

5952      do until(last);

5953          input;

5954          call execute("%nrbquote('||_infile_||')");

5955              /*%NRBQUOTE masks special characters, including & and %*/

5956      end;

5957  cards;

ERROR: Invalid physical name.

RULE:       ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0

5958

fname=  last=0 _ERROR_=1 _N_=1

NOTE: The SAS System stopped processing this step because of errors.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

5960  ;

5961  run;

Any idea?

Grand Advisor
Posts: 9,593

Re: A SAS function that extracts name of a SAS file

Hi,  As Jaap has pointed it out , my code is running under WINDOWS environment .If your SAS is under another OS like UNIX, LINUX  ... . You need to change the file PATH . Here is my SAS9.2 log ( no error ) .

1    ODS RTF file='c:\temp\x.rtf';

NOTE: Writing RTF Body file: c:\temp\x.rtf

2    data _null_;

3

4

5    infile cards truncover;

6    input fname $100. ;

7

8

9    infile dummy filevar=fname end=last;

10   call execute('%let fname='||scan(fname,-1,'\')||';');

11   do until(last);

12   input;

13   call execute('%nrbquote('||_infile_||')');

14   end;

15   cards;

NOTE: The infile DUMMY is:

      Filename=c:\temp\Olsaa_dataList_03a_d01_table01.sas,

      RECFM=V,LRECL=256,File Size (bytes)=250,

      Last Modified=14Aug2014:20:19:30,

      Create Time=12Aug2014:20:40:06

NOTE: The infile DUMMY is:

      Filename=c:\temp\Olsaa_dataList_03a_d01_table02.sas,

      RECFM=V,LRECL=256,File Size (bytes)=250,

      Last Modified=14Aug2014:20:19:44,

      Create Time=12Aug2014:20:39:56

NOTE: The infile DUMMY is:

      Filename=c:\temp\Olsaa_dataList_03a_d01_table03.sas,

      RECFM=V,LRECL=256,File Size (bytes)=250,

      Last Modified=14Aug2014:20:19:51,

      Create Time=12Aug2014:20:40:24

NOTE: DATA statement used (Total process time):

      real time           0.34 seconds

      cpu time            0.03 seconds

NOTE: 8 records were read from the infile DUMMY.

      The minimum record length was 0.

      The maximum record length was 60.

NOTE: 8 records were read from the infile DUMMY.

      The minimum record length was 0.

      The maximum record length was 60.

NOTE: 8 records were read from the infile DUMMY.

      The minimum record length was 0.

      The maximum record length was 60.

NOTE: CALL EXECUTE generated line.

19   ;

2   +  footnote j=l

3   +             font='Times New Roman'

4   +             h=10pt "Program Name:   Olsaa_dataList_03a_d01_table01.sas

5   +                          Programmer: Ken Chang

6   +                          Date:   August 21, 2014";

7   + proc print data=sashelp.class;run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

NOTE: PROCEDURE PRINT used (Total process time):

      real time           0.55 seconds

      cpu time            0.06 seconds

8   +  footnote j=l

9   +             font='Times New Roman'

10  +             h=10pt "Program Name:   Olsaa_dataList_03a_d01_table02.sas

11  +                          Programmer: Ken Chang

12  +                          Date:   August 21, 2014";

13  + proc print data=sashelp.class;run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

NOTE: PROCEDURE PRINT used (Total process time):

      real time           0.00 seconds

      cpu time            0.01 seconds

14  +  footnote j=l

15  +             font='Times New Roman'

16  +             h=10pt "Program Name:   Olsaa_dataList_03a_d01_table03.sas

17  +                          Programmer: Ken Chang

18  +                          Date:   August 21, 2014";

19  + proc print data=sashelp.class;run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

NOTE: PROCEDURE PRINT used (Total process time):

      real time           0.01 seconds

      cpu time            0.00 seconds

20   run;

21   ODS rtf close;

And here is what these three files contains ( they all have the same content ):

footnote j=l

            font='Times New Roman'

            h=10pt "Program Name:   &fname 

                         Programmer: Ken Chang  

                         Date:%sysfunc(date(),worddate18.)";

proc print data=sashelp.class;run;

Xia Keshan

Contributor
Posts: 61

Re: A SAS function that extracts name of a SAS file

hi Keshan,

Your codes work. What caused the error was my text documentation that spanned two or more lines. Your codes would work if I sandwich each line of documentation with /**/ like

/* comment A line 1*/

/* comment A line 2*/

/* comment A line 3*/

if my documentation is added in this way, then I start to get errors

/* comment A line 1

comment A line 2

comment A line 3*/

Many thanks!

Have a good weekend!

Chang

Valued Guide
Posts: 3,206

Re: A SAS function that extracts name of a SAS file

Woking out of Kurt...

When this is your footnote:

footnote j=l    font='Times New Roman'    h=10pt

         "Program Name:  &Program_name       Programmer: &Prcoder_name       Date:%sysfunc(date(),worddate18.)";

Just put in:

%let  Prcoder_name=Ken Chang;

ODS RTF file=;

%let  Program_name=Olsaa_dataList_03a_d01_table01;  %include "&dir_project.\&dir_codes.\&Program_name..sas";

%let  Program_name=Olsaa_dataList_03a_d01_table02;  %include "&dir_project.\&dir_codes.\&Program_name..sas";

%let  Program_name=Olsaa_dataList_03a_d01_table03;  %include "&dir_project.\&dir_codes.\&Program_name..sas";

ODS rtf close;

---->-- ja karman --<-----
Valued Guide
Posts: 3,206

Re: A SAS function that extracts name of a SAS file

Chang Your are focusing on your coding question.
What are your trying to achieve?

Not all environments behave equal.  The SAS_execfilename is only for Windows interactive 36613 - The availability of the SAS_EXECFILEPATH and SAS_EXECFILENAME environment variables

The use of C:\temp is only Windows and an approach often done before the Windows-XP era (2002). In fact it is the Dos 2.1 method.

So I teased you now...

You are having a goal, please describe that in way it looks like requirements with setting  some environment in which it should run.

---->-- ja karman --<-----
Grand Advisor
Posts: 9,593

Re: A SAS function that extracts name of a SAS file

Hi , Sorry. I think using %include could be better .

 
ODS RTF file='c:\temp\x.rtf';
data _null_;
infile cards truncover;
input fname $100. ;
 
call execute('data _null_; call symputx("fname","'||strip(scan(fname,-1,'\'))||'");run;');
call execute('%include "'||strip(fname)||'";'); 
cards;
c:\temp\Olsaa_dataList_03a_d01_table01.sas
c:\temp\Olsaa_dataList_03a_d01_table02.sas
c:\temp\Olsaa_dataList_03a_d01_table03.sas
;
run;
ODS rtf close;














173
174  ODS RTF file='c:\temp\x.rtf';
NOTE: Writing RTF Body file: c:\temp\x.rtf
175  data _null_;
176  infile cards truncover;
177  input fname $100. ;
178
179  call execute('data _null_; call symputx("fname","'||strip(scan(fname,-1,'\'))||'");run;');
180  call execute('%include "'||strip(fname)||'";');
181  cards;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


NOTE: CALL EXECUTE generated line.
185  ;
1   + data _null_; call symputx("fname","Olsaa_dataList_03a_d01_table01.sas");run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


2   +  %include "c:\temp\Olsaa_dataList_03a_d01_table01.sas";

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


3   + data _null_; call symputx("fname","Olsaa_dataList_03a_d01_table02.sas");run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


4   +  %include "c:\temp\Olsaa_dataList_03a_d01_table02.sas";

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


5   + data _null_; call symputx("fname","Olsaa_dataList_03a_d01_table03.sas");run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


6   +  %include "c:\temp\Olsaa_dataList_03a_d01_table03.sas";

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


204  run;
205  ODS rtf close;



Xia Keshan

Ask a Question
Discussion stats
  • 11 replies
  • 684 views
  • 9 likes
  • 6 in conversation