Macro %archive

1. Description

The macro ARCHIVE will create an Archive folder and the dated folder inside it to store the old files.

2. Dependencies

2.1 Global Macro Variables - N/A

2.2 Global Macros - N/A

3. Macro Parameters

This macro does not have any parameters

4. Assumptions

  1. If the archive folder does not exist, macro will create a folder named ‘archive’ in the directory
  1. Macro will create a dated folder under archive if there does not exist any folder with the same date. Eg: 23Jun2020
  2. If the dated folder exist, macro will create a dated folder with timestamp. Eg: 23Jun2020- 6 PM
  3. Macro will produce an error if there exist a folder with same date and timestamp in the archive folder.
  1. After the creation of archive folder, the macro will copy all the files from original folder to archive folder.
  2. The macro is placed in the same folder and batch.

5. Sample Call

  %ARCHIVE;
                                     

6. Macro Code

    %macro archive;
      %*Identifying the last opned file;
      data arc_dir1;
        set sashelp.vextfl end=ls;
        where level=0 and xengine="DISK" and exists ne "no" and     find(xpath,"archive.sas","i");
        *Archive folder as sper template;
        xpath=substr(xpath,1,length(xpath)-length(scan(xpath,-1,"\")));
        archive=strip(xpath)||"archive";
        *Dated folder for placing files;
        date=put(today(),date9.);
        datefrmt=substr(date,1,2)||propcase(substr(date,3,3))||substr(date,6);
        time=strip(datefrmt)||"-"||put(time(),timeampm5.);
        dir=catx("\",of archive,datefrmt);
        dir1=catx("\",of archive,time);
        if ls then do;
          call symput('arcdir',archive);
          call symput('dir',dir);
          call symput('dir1',dir1);
          call symput('indir',xpath);
        end;
      run;
      
      %*Getting the file informations for copy;
      filename dir "&indir" ;
      data arc_flist (keep=filename i);
        dopen=dopen("dir") ;
        filecount=dnum(dopen) ;
        do i=1 to filecount ;
          filename=dread(dopen,i);
          if find(filename,'archive','i')=0 and find(filename,'.') then output ;
        end ;
        rc=dclose(dopen) ;
      run ;
    
      data arc_flist1;
        set arc_flist end=ls;
        id=strip(put(_n_,best.));
        pastea="pastea"||id;
        pasteb="pasteb"||id;
        pasteva="'copy  "||'"'||strip("%sysfunc(strip(&indir))"||strip(filename))||'"'||    ' "'||"%sysfunc(strip(&dir))\"||strip(filename)||'"'||"'";
        pastevb="'copy  "||'"'||strip("%sysfunc(strip(&indir))"||strip(filename))||'"'||    ' "'||"%sysfunc(strip(&dir1))\"||strip(filename)||'"'||"'";
        %*macros for copy pate files;
        call symput(pastea,pasteva);
        call symput(pasteb,pastevb);
        if ls then call symput('endlp',id);
      run;
    
      %*Checking the existence of the archive folder folder, if not exist create a     folder;
      %let rc = %sysfunc(filename(fileref,"%sysfunc(strip(&arcdir))")) ; 
      %if %sysfunc(fexist(&fileref))= 0 %then %do;
        options dlcreatedir; 
        libname folder "%sysfunc(strip(&arcdir))";
      %end;
      %*Sub folders;
      %let rc = %sysfunc(filename(subf,"%sysfunc(strip(&dir))")) ; 
      %if %sysfunc(fexist(&subf)) %then %do;
        %let rc = %sysfunc(filename(subft,"%sysfunc(strip(&dir1))")) ; 
        %if %sysfunc(fexist(&subft)) %then %do;
          %put ERROR: Folder already exist;
        %end;
          %else %do;
            options dlcreatedir; 
            libname folder "%sysfunc(strip(&dir1))";
            %do i=1 %to &endlp;
              options noxwait;
              data _null_;
                x &&pasteb&i;
              run;
            %end;
          %end;
      %end;
        %else %do;
          options dlcreatedir; 
          libname folder "%sysfunc(strip(&dir))";
          %do i=1 %to &endlp;
            options noxwait;
            data _null_;
              x &&pastea&i;
            run;
          %end;
        %end;
    
       *deleting the datasets generated through this macro;
      proc datasets nolist lib=work;
        delete arc_:;
      quit;
    %mend archive;

Back