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;


sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 7 replies
  • 13204 views
  • 2 likes
  • 4 in conversation