DATA Step, Macro, Functions and more

How to put header in a fixed format file?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

How to put header in a fixed format file?

Hi I have below data and I need to provide header for this.

 

Input Data set:  100 018 XX
                         200 172 ZZ

 

Desired Output: A     B    C

                          100 018 XX
                          200 172 ZZ 

 

I only want this output in a data set, not in a report.


Accepted Solutions
Solution
‎03-12-2018 09:27 AM
Super User
Super User
Posts: 8,279

Re: How to put header in a fixed format file?

Assuming that you want to put it into a text file and are just calling it a data set because of confusion with old mainframe terminology for a disk file.

 

The problem with adding a "HEADER" to a fixed format file is just how to make those meaningful.  What if you are writing a single byte code for GENDER then where in the first line can you write the heading for that column?

But assuming you have figured that out then just use the automatic variable _N_ to tell when you are on the first observation and write the header line then.

 

data _null_;
  set have ;
  file 'output_file' ;
  if _n_=1 then the put '  A  B  C';
  put (A B C) (3.);
run;

 

View solution in original post


All Replies
Super User
Posts: 10,574

Re: How to put header in a fixed format file?

You can't put character data into numeric variables, so this won't be possible.

What for do you want to do that, anyway? Columns have names, and additional information can be put into column labels.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 18

Re: How to put header in a fixed format file?

Posted in reply to KurtBremser

Hi @KurtBremser  I am creating a flat file on mainframes, and i want to put labels or header (since this will be only on the first record) on the data on fixed positions.

Super User
Posts: 10,574

Re: How to put header in a fixed format file?

You were specifically talking about a dataset, quote:

 

I only want this output in a data set, not in a report.

 

Please make up your mind.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,574

Re: How to put header in a fixed format file?

If you want to put in a header in a typical fixed-length mainframe file, do this:

data have;
input a b c $;
cards;
100 018 XX
200 172 ZZ
;
run;

data _null_;
set have;
file '$HOME/sascommunity/flatfile' recfm=f lrecl=8;
if _n_ = 1 then put '  A  B C';
put
  a z3.
  b z3.
  c $2.
;
run;

You will have to use MF-style file syntax, of course, probably a fileref derived from a DD in the JCL that calls the SAS program. Just make sure that your SAS lrecl matches the LRECL in the JCL, and fits your lengths of the put statements

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Valued Guide
Posts: 629

Re: How to put header in a fixed format file?

There is a logical conflict between title and description: the title reads "How to put header in a fixed format file?" and later on you write "I only want this output in a data set, not in a report.". Why do you want that text in a dataset? Does not make sense, especially because all variables have to be converted to alphanumeric before a title-observation could be added.

 

Posting the code you are using to create the fixed format text file will make it easier to suggest something useful.

 
Occasional Contributor
Posts: 18

Re: How to put header in a fixed format file?

Posted in reply to andreas_lds

Why can't we add header record here?  Can we add labels on fixed positions?

 

 

Valued Guide
Posts: 629

Re: How to put header in a fixed format file?

Please read my post again, i explained what is necessary to add a header observation. And i repeat: this is not recommended! Writing additional lines of text while creating the text file, is possible. You don't have to add that row to the dataset.

Solution
‎03-12-2018 09:27 AM
Super User
Super User
Posts: 8,279

Re: How to put header in a fixed format file?

Assuming that you want to put it into a text file and are just calling it a data set because of confusion with old mainframe terminology for a disk file.

 

The problem with adding a "HEADER" to a fixed format file is just how to make those meaningful.  What if you are writing a single byte code for GENDER then where in the first line can you write the heading for that column?

But assuming you have figured that out then just use the automatic variable _N_ to tell when you are on the first observation and write the header line then.

 

data _null_;
  set have ;
  file 'output_file' ;
  if _n_=1 then the put '  A  B  C';
  put (A B C) (3.);
run;

 

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 256 views
  • 1 like
  • 4 in conversation