BookmarkSubscribeRSS Feed
tony146
Calcite | Level 5

Trying to write multiple text files to a server by the FTP method.  I can get 1 file to write to the server I want to write to, but I would like to write different text files, using the name field in the dataset to set the name of the text file.

Example dataset call testing:

obs name lastname

1     bob   smith

2     sandy lane

3    luke   rath

Would like to have the following text files created:
bob.txt

sandy.txt

luke.txt

Code I have...I have tried a lot of different things, this is the latest.

data _null_;

set testing end=eof;

by name;

if first.name then do;

fname="'"||trim(left(put(name,$10.)))||".txt'";

filename create ftp "&fname"' cd='host_dir'

                host='hostname'

                user='test1'

                pass='xxxxxx'

                recfm=v;

file create linesize=155;

put @1 "<firstname="""name""" lastname="""lastname""">;

end;

If I replace &fname with 'testing.txt'  I get one file with all the data in it.

Is there a way to do this?

Thanks,

  Tony

4 REPLIES 4
Patrick
Opal | Level 21

I'm not 100% sure if below code will work. Give it a try and let us know.

Alternatively you could write all your files to a folder using the file statement in comment in below code and then use a FTP with mput to transfer all files in this folder.

If your data is not sorted by "name" and there are multiple rows with the same name then you might also need to use the "mod" option for the file statement.

data have;
  input name $ lastname $;
  datalines;
bob smith
sandy lane
luke rath
;
run;

data _null_;
  set have;
  length outfile $1000;

/*
  outfile=cats('c:\test\',name,'.txt');
  file dummy filevar=outfile;

*/

  outfile=cats('<host_dir>',name,'.txt');
  file dummy ftp  filevar=outfile

    host='hostname'
    user='test1'
    pass='xxxxxx'
    recfm=v
    linesize=155;
  put name +(-1) ',' lastname;
run;

Tom
Super User Tom
Super User

Actually the advice in that presentation is out of date. The FILEVAR option on the FILE statement does work with the FTP method.

data _null_;

  set testing ;

  fname = catx('.',name,'txt');

  file create ftp linesize=155 filevar=fname

    cd='host_dir'

    host='hostname'

    user='test1'

    pass='xxxxxx'

    recfm=v

  ;

  put @1 '<firstname=' name : $quote. 'lastname=' lastname : $quote. '>';

end;


Note that the put statement could be even simpler if the dataset variable names matched the field names in your output text file.

data _null_;

  length firstname lastname $20;

  input firstname lastname ;

  put '<' (firstname lastname) (= : $quote.) '>' ;

cards;

bob smith

sam jones

run;

Ksharp
Super User

I would like do it respectively . First make these txt file ,then using command to ftp them up to SAS Server.

data x;
input obs name $ lastname $;
cards;
1     bob   smith
2     sandy lane
3    luke   rath
;
run;
proc sql;
create table temp as
 select distinct name from x;
quit;
data _null_;
 set x;
 *output all of txt file under c:\temp\ ;
 call execute(catt('proc export outfile="c:\temp\',name,'.txt" data=x(where=(name="',name,'")) dbms=tab replace;run;'));
run;




Then 
cd c:\temp\
ftp 10.1.66.14
enter your username and password
>binary
>prompt
>mput *.txt 

Ksharp

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2497 views
  • 0 likes
  • 5 in conversation