You can use the FILENAME PIPE access method as an alternative way of executing SFTP


You might encounter various issues that prevent you from executing SSH File Transfer Protocol (SFTP) within SAS® code using the FILENAME SFTP access method. Some of these issues are time-outs due to the file being moved or the file being too large, or the default SFTP messages being altered. 

One alternative way to execute SFTP is to use the FILENAME PIPE access method. Here are some examples of using FILENAME PIPE to execute SFTP from SAS:

/*  Example 1            */
/* Write out SFTP batchfile commands to a temp file. */
filename cmds temp;
data _null_;
put "cd /outdir"
    / "ls -lr *.zip"
    / "quit";
run;
/* Execute SFTP */
filename sftp pipe "sftp -o '-b %sysfunc(pathname(cmds))'  ";
data _null_;
  infile sftp;
  file '<output file>';
  put _infile_;
run;
/* Example 2                 */
/* Build the sftp.txt file to hold SFTP batchfile commands. */

filename cmds "<any path>/sftp.txt" ;
data _null_;
file cmds ;
put 'cd /user/local/mydir' ;
put 'get remoteFile.txt  localFile.txt' ;
run ;
/* FILENAME PIPE for SFTP command */
filename foo pipe "sftp -b $HOME/sftp.txt user@remotehost"   ;
/* Execute the SFTP command by accessing the FILENAME PIPE. */
data _null_;
infile foo ;
input ;
run ;
/* Example 3                 */
/* Build the ​​​​​​​sftp.txt file to hold SFTP batchfile commands. */
%let sftpcmds=D:\work\sftpcmd.txt;
%let host=host.com;                          /* Machine to connect with ftp      */
%let userid=myuserid;              /* ID to use with ftp command       */
%let sftppwd=XXXX;                /* Password for the ftp userid      */
%let xfermode=binary;          /* ASCII or binary mode xfer        */
%let homedir=/testfolder;           /* Directory to start in    */
%let cmd=put;                  /* Using PUT to put files           */
%let thefile1=test.csv;       /* The file to PUT to (or GET from) */
%let localdir=D:\work; 
filename sftpcmds "&sftpcmds";  
data _null_;
   file sftpcmds pad lrecl=80;
/* Using Windows Putty
filename test pipe "'C:\Program Files (x86)\PuTTY\PSFTP.EXE' -v -i F:\Putty_private_key.ppk -b F:\work\sasuser\sftp.txt remote@abc.com";