DATA Step, Macro, Functions and more

how to sftp files

Reply
Contributor
Posts: 40

how to sftp files

hi,

I need to get a set of datasets from a remote server. And I am connected to a unix server as well. How to accomplish this ?

So task is getting a set of files or sasdatasets from a remote unix server to another unix server  using SAS commands.

thanks!

sasbase9

Trusted Advisor
Posts: 2,115

how to sftp files

lots of possibilities. 

  1. You could use the SAS 'X" command to launch an ftp script to do the copies. 
  2. If the datasets are all SAS datasets and you have SAS Connect running, you could use PROC COPY to move the datasets from one library to another. 
  3. Depending on the flavor(s) of Unix and local permissions, you might be able to NFS mount the directories on the remote Unix box and treat them as local.

I prefer #1 as it is simple and robust.

Doc Muhlbaier

Duke

New User
Posts: 1

Re: how to sftp files

Hello,

 

Could you provide code example for using X command to launch fTP command? I cannot use command prompt and hoping to sftp files using user pwd using the X command.

Thanks,

Neha

Contributor
Posts: 36

how to sftp files

Some sample code you can use for ftp (All macro variables have to be set to your reality):

filename ftpcom "&ftpcommands";


* Commands for ftp session;


data _null_;
file ftpcom pad;

* ftp session user and password;
put "user &user &pwd";

* Disable prompting cmd;
put 'prom no';

* Enable Binary transfer mode cmd;
put 'bin';

* Go to local folder cmd;
put "lcd &excel";

* Go to remote folder cmd;
put "cd &sharefolder";

* Get needed files cmd;
put 'mget *.tgz';
stop;
run;

filename doftp pipe "ftp -n &host < &ftpcommands";

* Enable ftp session and pass it the commands file;

data _null_;
infile doftp;
input;
put _infile_;
run;

As said before, you can also use a Connect Session or create a NFS to the drive.

Contributor
Posts: 40

how to sftp files

One doubt--

What does "&ftpcommands" in first filename statement correspond to ?

thanks again for help!

Contributor
Posts: 36

Re: how to sftp files

sasbasls wrote:

One doubt--

What does "&ftpcommands" in first filename statement correspond to ?

thanks again for help!

It's the name of the file you pass to the ftp session with the commands that will be executed.

On a windows environment it would be something like: "c:\ftp_commands.txt".

On a unix environment it would be something like: "/home/sas/ftp_commands.txt".

In order to transfer files in an ftp session in batch you have to build your ftp commands file to pass to the ftp session, with the same commands you would use when typing directly in an ftp session.

Take this example and the sample i gave you with some modifications on macro variable names between two unix hosts:

You want to transfer dataset "wanted.sas7bdat", placed in "/sasdata/source" on a remote server with ip "10.203.22.35", to "/sasdata/target" in the local server (where your sas session is invoked). The remote server ftp user is "sasftp" and the password is "12345". Your "ftpcommands" file will be built and stored in "/home/trash" folder in the local host.

sample:

filename ftpcom "&ftpcommands";


* Commands for ftp session;


data _null_;
file ftpcom pad;

* ftp session user and password;
put "user &user &pwd";

* Disable prompting cmd;
put 'prom no';

* Enable Binary transfer mode cmd;
put 'bin';

* Go to local folder cmd;
put "lcd &target";

* Go to remote folder cmd;
put "cd &source";

* Get needed files cmd;
put 'mget &wanted_files';
stop;
run;

filename doftp pipe "ftp -n &host < &ftpcommands";

* Enable ftp session and pass it the commands file;

data _null_;
infile doftp;
input;
put _infile_;
run;

The macro variables for the sample code to work should look like this:

%let ftpcommands=/home/trash/ftp_commands;

%let user=sasftp;

%let pass=12345;

%let host=10.203.22.35;

%let source=/sasdata/source;

%let target=/sasdata/target;

%let wanted_files=A.sas7bdat

Since we're using mget ftp command to get the files we are allowed to use wildcards so, if the dataset was indexed you could use one wild card to bring both table and index with the same command.

%let wanted_files=wanted.sas7*;

Hope this helps.

PA.

N/A
Posts: 1

Re: how to sftp files

u can transfer by using some macro u need to craeate batch file for each file and need to give all credential for unix sever then u can transfer give me ur mail id i will send sample code for htis.

Super Contributor
Posts: 673

Re: how to sftp files

What is the order of the code in which it must be created or executed?

You started with filename ftpcom "&ftpcommands".

Where is the above command be give?

Super User
Posts: 10,023

Re: how to sftp files

Ask a Question
Discussion stats
  • 8 replies
  • 9445 views
  • 0 likes
  • 7 in conversation