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.
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.
Thank you Reeza, but how do I now integrate this into a code?
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.
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;
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;
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.
Use formatted output
put
x1 $10.
x2 $20.
;
will result in lines of length 30.
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 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.