Search code examples
emailsasoutputattachmentsas-macro

Output SAS macro to file or email attachment


I have some problems with getting basic file information send to me in an email.

From various scripts on the internet I have ended up with the following macro which works for outputting the information I want.

%macro list_files(dir);
    %local filrf rc did memcnt name i;
    %let rc=%sysfunc(filename(filrf,&dir));
    %let did=%sysfunc(dopen(&filrf));      
    %let ymd = %sysfunc(putn(%sysfunc(today()).,yymmdd6.));

    %if &did eq 0 %then %do; 
        %put Directory &dir cannot be open or does not exist;
        %return;
    %end;

    %do i = 1 %to %sysfunc(dnum(&did));   
        %let name=%qsysfunc(dread(&did,&i));

        %if %qscan(%qscan(&name,1,_,b),1,.) eq &ymd. %then %do;

            %let rc=%sysfunc(filename(fref,&dir\&name));
            %let fid=%sysfunc(fopen(&fref));

            %let CreatedDT=%qsysfunc(finfo(&fid,Create Time));
            %let ModifiedDT=%qsysfunc(finfo(&fid,Last Modified));

            %put &dir\&name,&CreatedDT,&ModifiedDT;

            %let fid=%sysfunc(fclose(&fid));
            %let rc=%sysfunc(filename(fref));
        %end;
    %end;
    %let rc=%sysfunc(dclose(&did));
    %let rc=%sysfunc(filename(filrf));     

%mend list_files;
%list_files(C:\logs\);

Notice: The macro does expect a certain file name format in order to work. If I for example have the following log files:

ProgramA_210215.log
ProgramA_210214.log
ProgramA_210213.log
ProgramB_210214.log
ProgramB_210213.log
ProgramC_210215.log
ProgramC_210214.log
ProgramC_210213.log

... and I run this on 2021-02-15, then I only get:

ProgramA_210215.log
ProgramC_210215.log

... which is perfect for my needs.

My problem is, that I can't figure out how to either send the output to me as an email or dump the output in a file, which I can then attach to an email.

Notice 2: I initially wrote the macro as "normal data calls"(?) and everything worked except for the CreateDT and ModifiedDT, which were static for all files. The macro version at least works and shows the correct (and different) CreateDT and ModifiedDT.

What I am trying to achieve in the end is, to send me an email with an attachment of todays log files and their creation datetime and last modified datetime.

I guess I'm just missing a simple step, but can't figure out which :(.


Solution

  • Calling all of those functions using %SYSFUNC() is just making it harder to to do what you want. Just call the functions in a data step instead so that you have an actual dataset with the files. Then you can use any of multiple methods to direct the list where ever you want, including sending an email.

    I am pretty sure that FINFO() always returns character strings so make sure that the target variables are long enough for the value returned. Note that the information available from FINFO() depends on the operating system where SAS is running. So make sure the names you use are appropriate for your SAS session.

    %let dir=c:\logs\;
    
    data file_list;
      length filrf fref $8 name $256 ymd $6 CreatedDT ModifiedDT $40;
      ymd = put(today(),yymmdd6.);
    
      rc=filename(filrf,"&dir");
      did=dopen(filrf);      
      if did=0 then do;
        put "Directory &dir cannot be open or does not exist.";
        stop;
      end;
      do i = 1 to dnum(did);
         name=dread(did,i);
         if scan(scan(name,1,'_','b'),1,'.') eq ymd then do;
            rc=filename(fref,"&dir\"||name);
            fid=fopen(fref);
            CreatedDT=finfo(fid,'Create Time');
            ModifiedDT=finfo(fid,'Last Modified');
            output;
            fid=fclose(fid);
            rc=filename(fref);
         end;
      end;
      rc=dclose(did);
      rc=filename(filrf);
      keep name CreatedDT ModifiedDT ;
    run;