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
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;
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;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.