Desktop productivity for business analysts and programmers

How to create .txt file with a header and Trailer

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

How to create .txt file with a header and Trailer

[ Edited ]

Hi , 

I am working on a dataset to create an out put txt file with header and trailer records .The header record which is the first line in the file and trailer record as the last line in the file .

EX: If there were,3 records then the header field as 00000 .  and trailer as 0003 ( count of records).

Could anyone send me the code.

Thanks

Madhu

 

 

Script: 

%let filePath = /user/home/test;
libname test '/user/home/test/';
%include 'user/home/test/TDpwd.txt';

proc sql;
CONNECT TO TERADATA (user=&user password=&password server=oneview mode=teradata);
create table test.test_data as
select * from connection to teradata(
SELECT * from 5pt.test24
);
disconnect from teradata;
quit;

Data test.test_result;
set test.test_data;
file "&filePath/test.data_2016";
put
Name $ 1 - 17
City $ 18 - 25
Country $ 26 - 30
Id $ 31 - 46
;
run;

 

Output:

00000 ( header)

Thomas Newjersy USA 1000

David Newjersy USA 1212

Rahul Virginia USA 2002

 

00003 (trailer)

 

 

 


Accepted Solutions
Solution
‎11-26-2016 06:47 PM
Super User
Posts: 19,193

Re: How to create .txt file with a header and Trailer

Here's a fully worked example. You'll need to modify it to fit your data.

 

data test;
	set sashelp.class END=EOF; /*END creates a variable EOF that lets you know when you are on the last record*/
	file 'C:\_localdata\temp\test.txt' dlm=",";

	*Header record;
	If _n_=1 then
		do;
			put '0000';

			/*Output header record with column names - manual type in names*/
			put 'Name, Sex, Age, Height, Weight';
		end;

	put name sex age height weight;

	*Rest of your SAS code;
	/*Trailer record*/
	if eof then
		do;
			put _N_ : z4.; /*Put number of records into the file, with a format of 0000, leading zeroes*/
		end;
run;

View solution in original post


All Replies
Super User
Posts: 19,193

Re: How to create .txt file with a header and Trailer

1.

Identifies first record in a data step

If _n_=1 then do;

 <insert header record>

end;

 

 

2. Identify last record

 

Set have end=eof;

if eof then do;

<insert trailer record here>

end;

Occasional Contributor
Posts: 8

Re: How to create .txt file with a header and Trailer

[ Edited ]

Not worked Smiley Sad

 

Solution
‎11-26-2016 06:47 PM
Super User
Posts: 19,193

Re: How to create .txt file with a header and Trailer

Here's a fully worked example. You'll need to modify it to fit your data.

 

data test;
	set sashelp.class END=EOF; /*END creates a variable EOF that lets you know when you are on the last record*/
	file 'C:\_localdata\temp\test.txt' dlm=",";

	*Header record;
	If _n_=1 then
		do;
			put '0000';

			/*Output header record with column names - manual type in names*/
			put 'Name, Sex, Age, Height, Weight';
		end;

	put name sex age height weight;

	*Rest of your SAS code;
	/*Trailer record*/
	if eof then
		do;
			put _N_ : z4.; /*Put number of records into the file, with a format of 0000, leading zeroes*/
		end;
run;
Occasional Contributor
Posts: 8

Re: How to create .txt file with a header and Trailer

Thanks and Apreciate Reeza, It is working Smiley Happy

little more details to be added  , header and trailer starts with first column and ends with the last column of the record

 

ex: Output :

 

AL16                                0000
Thomas New Jersy USA 1000
David     New Jersy USA 1000
Rahul     Virginia      USA 2002
DL99                               0003


script:

If _n_=1 then
do;
put '0000';

end;

put name,city,country,id ;

 

if eof then
do;
put _N_ : z4.; 
end;

 

Thanks

Madhu

 

 

Super User
Posts: 9,878

Re: How to create .txt file with a header and Trailer

CODE NOT TESTED.

ods csvall file='c:\temp\x.csv';
title 'xxxxxxxxxx';
footnote 'yyyyyyyyyy';
proc print data=sashelp.class;run;
ods csvall colse;


☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 1428 views
  • 1 like
  • 3 in conversation