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)
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;
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;
Not worked
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;
Thanks and Apreciate Reeza, It is working
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
CODE NOT TESTED. ods csvall file='c:\temp\x.csv'; title 'xxxxxxxxxx'; footnote 'yyyyyyyyyy'; proc print data=sashelp.class;run; ods csvall colse;
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: