DATA Step, Macro, Functions and more

How to create a text file with fixed length

Reply
Super Contributor
Posts: 401

How to create a text file with fixed length

Hi I have 3 SAS datasets, each having 75 variables.  I need to create them in text file with fixed lengths.  What is the best way to do that?

Thanks. 

Super User
Posts: 19,855

Re: How to create a text file with fixed length

A customized data step. 

 

You can use use the info from sashelp.vcolumns (length/type/format) to help generate the info required for the code...or even the code. 

Super Contributor
Posts: 401

Re: How to create a text file with fixed length

Thank you Reeza,  but how do I now integrate this into a code?

Super User
Posts: 19,855

Re: How to create a text file with fixed length

See the example here. 

 

http://www.ats.ucla.edu/stat/sas/faq/write_fixedformat.htm

 

Determining the locations (@#) is based on length which can be obtained from dictionary tables or proc contents. 

Super User
Posts: 10,044

Re: How to create a text file with fixed length

What do you mean by fixed length. Can you post an example ?

 

filename x 'c:\temp\x.csv' ;
data _null_;
set sashelp.class;
 file x ;
 length temp $ 40;
 temp=catx(',',of _all_);
 len=40;
 put temp $varying40. len;
run;

x.png

Super Contributor
Posts: 401

Re: How to create a text file with fixed length

I have no idea.. it's a request from someone that it must be fixed length.  So here's what I did:

 

filename out_pipe "P:\Data\new_bo1.txt";

data _null_; set pdrive.new_bor1;

file out_pipe;

put "|" ACEAmount

"|" ACEDate.....

run;

Valued Guide
Posts: 2,177

Re: How to create a text file with fixed length

[ Edited ]

PUT statement default behaviour for a list of variables would place single blanks (default delimiter) between vars.
Consider the brevity of:
FILE "YOUR FILE.CSV" DSD LRECL=1000 ;
PUT (_ALL_)( : ) ;
One way to achieve fixed layout would be to specify the formats with widths for each variable in the PUT.
It makes a fine example for use of the dictionary.columns metadata.
Just one question ... what default format/width would you wish for numeric values with no defined format?
The default BEST12. provides floating point notation for large numeric values.....
Once the required choices of width are clarified, a bit of SQL and a simple data step could be written by a student.
In a fixed layout, delimiters would not be needed, but might reduce any importer's need for proper definition of the column layout.
I believe the fixed positions layout would probably be faster to import than traditional csv ... (less cpu time, but more expensive in disk space and i/o) but any benefit will only become apparent for very large data.

Super User
Posts: 7,851

Re: How to create a text file with fixed length

Use formatted output

put
  x1 $10.
  x2 $20.
;

will result in lines of length 30.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 7 replies
  • 513 views
  • 1 like
  • 5 in conversation