BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tvbMadhu
Calcite | Level 5

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)

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

7 REPLIES 7
Reeza
Super User

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;

tvbMadhu
Calcite | Level 5

Not worked Smiley Sad

 

Reeza
Super User

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;
tvbMadhu
Calcite | Level 5

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

 

 

Ksharp
Super User
CODE NOT TESTED.

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


hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 15071 views
  • 2 likes
  • 4 in conversation