Allan Bowe
Allan Bowe

Reputation: 12691

Using SAS to copy a text file

Is there a SAS procedure / function which can be used to copy a text file from one location to another?

Of course this can be achieve using OS commands (%sysexec copy) but surely there must be an OS-agnostic way to do this?

From what I can tell by looking at the documentation, proc copy (or proc cport) only relate to SAS files..

Upvotes: 5

Views: 11324

Answers (3)

Richard
Richard

Reputation: 27508

For SAS 9.4 and forward use the FCOPY function. Use the recfm=n (n for none or binary) to perform a true copy.

filename src "path-to-src" recfm=n;
filename dst "path-to-dst" recfm=n;
%let rc = %sysfunc(FCOPY(src,dst));
%put %sysfunc(SYSMSG());

If the filerefs do not use the recfm=n option FCOPY will treat them as a text file and thus be affected by control characters and end of file markers.

Upvotes: 4

Tom Quarendon
Tom Quarendon

Reputation: 5708

The simplest method is something like:

 data _null_;
    infile 'c:\input.txt';
    file 'c:\output.txt';
    input;
    put _infile_;
 run;

The method presented by RawFocus will copy any file, in binary, one byte at a time from input to output. For a text file this isn't necessary and doing the above will copy the file one line at a time. You may have to be slightly careful with record lengths, I believe that the default record length is 256, so you may need to put an explicit

 lrecl=32767

option or similar on the infile statement, as in

infile 'c:\input.txt' lrecl=32767;

Upvotes: 10

Allan Bowe
Allan Bowe

Reputation: 12691

Seems Chris Hemedinger has the answer!

/* these IN and OUT filerefs can point to anything */
filename in "c:\dataIn\input.xlsx"; 
filename out "c:\dataOut\output.xlsx"; 

/* copy the file byte-for-byte  */
data _null_;
  length filein 8 fileid 8;
  filein = fopen('in','I',1,'B');
  fileid = fopen('out','O',1,'B');
  rec = '20'x;
  do while(fread(filein)=0);
     rc = fget(filein,rec,1);
     rc = fput(fileid, rec);
     rc =fwrite(fileid);
  end;
  rc = fclose(filein);
  rc = fclose(fileid);
run;

filename in clear;
filename out clear;

Thanks Chris

Upvotes: 6

Related Questions