Search code examples
macrossasfile-copying

SAS code to copy a file based upon a condition check?


Hello my requirement is for a pharma company.

Requirement goes as:

The SAS job is run on a daily basis, with some reject validations applied.

The rejected data goes to a specific reject folder. Now we have been asked to create a code where in the code "checks if a folder exists at a location with name as current date (today date) and if not , it should create it and put the rejected data files from the REJECT FOLDER to the date folder created (I have the code for folder creation.)

Now here is the catch, Other developer shall write the code by which today date shall be appended at the end of the filename as yyyymmdd format.

I need to check the file WITH CURRENT DATE AT LAST AND PUT IT IN THE CURRENT DATE FOLDER.

E.g file is c:\sas\xxxaaaa_20140716.dat , the file should be copied to c:\reject\20140716\ folder.

I have developed a code that checks the existence and create a folder with name as today date.

Please help me to write a code in between to move the files upon the condition. P .S. I am new to SAS coding so would appreciate an exact code.Thank You

Code:

%macro fcheck(dir=) ; 
options noxwait; %* no need to wait for command prompt action from user;

%local var fileref ; 
%let var = %sysfunc(filename(fileref,&dir)) ; 

%if %sysfunc(fexist(&fileref))  %then %* to check if the file xists;
   %put NOTE: The directory "&dir" exists ; %* here we can put a statement to move our reject file to the date folder;
%else 
   %do ; 
   %sysexec md   &dir ; %* create the directory as our current date;
   %put %sysfunc(sysmsg()) The directory has been created. ; %*this message may not be    required for us;
%end ; 

%let var=%sysfunc(filename(fileref)) ; 
%mend fcheck ; 
%let sascode=%sysfunc(date(), yymmddn8.);

%fcheck(dir=c:\dir) ;    %*  directory specification goes here ; 
%fcheck(dir=c:\dir\&sascode);%* checkin my directory path and name

Solution

  • Couple of methods to copy files in SAS:

    xcommand (this will use the dos system to copy files)

    X "copy c:\temp\*.* C:\temp\CopyFiles\*.* > C:\temp\CopyFiles\reportcp.txt"; 
    

    And systask:

    systask command "copy c:\temp\*.* C:\temp\CopyFiles\*.*" wait status=copyfl; 
    

    Be aware that systask runs asynchronously with SAS, therefore if you need to wait for the result of the SYSTASK command before you continue you'll need to use WAITFOR or WAIT options