How to Copy a file with SAS?

Published on:
written bySubhro
How To, SAS PROGRAMS

Here is an example of a code that you can use to copy a file with SAS. It copies the content byte-by-byte so it can be used to copy any file, even pictures! This will work on both Unix and Windows and it’s great for copying files from your SAS session to a place outside of SAS, or vice versa.

Functions used to copy files

  • FOPENExternal file function to read an external file.
  • FREAD – This function reads records bit by bit record from an external file into the File Data Buffer (FDB).
  • <a href="https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarget=p1a70mpbj2usaln1r9z4qldliexk.htm&locale=en">FGET</a> and FPUT – These two functions are used to copy data from the File Data Buffer (FDB) into a variable and to move data to FDB, respectively.
  • FWRITE – This function is used to write a record from the FDB to an external file.
filename in "/home/examples/data.txt";
filename out "/home/examples/copy.txt";

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;

 

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap