DATA Step, Macro, Functions and more

Writing fixed-width text files

Accepted Solution Solved
Reply
SAS Employee
Posts: 73
Accepted Solution

Writing fixed-width text files

Good afternoon,

I need help writing fixed-width text files from SAS dataset.  I have SAS data set – Product with Product ID – 1, 2 and 3.

And I’m using following code to create text file.

filename outfile "/mnt/data01/test.txt"

data _null_;

file outfile ;

set WORK.Product;

put @1'Product ID'n 10. ;

run;

Result of the output is

123

I would like the output to be

Product ID

1

2

3

Thanks


Accepted Solutions
Solution
‎06-16-2014 04:31 PM
SAS Employee
Posts: 73

Re: Writing fixed-width text files

Thanks for all your help.  I was able to resolve the issue by using the "termstr=CRLF" option.

http://support.sas.com/kb/14/178.html

http://support.sas.com/kb/14/178.html

Thanks


View solution in original post


All Replies
Frequent Contributor
Posts: 101

Re: Writing fixed-width text files

The simplest fix is to add the following line code after your SET statement:

if _n_=1 then put 'Product ID';

Super User
Posts: 10,500

Re: Writing fixed-width text files

Is your product id a numeric or character variable? You may have issues geting a numeric to left justify in this context.

Super User
Posts: 17,840

Re: Writing fixed-width text files

Do a proc export to a text file and see the code generated in the log. That will let you know how to specify your code.

Something like the following may work as well (untested)

filename outfile "/mnt/data01/test.txt"

data _null_;

file outfile ;

set WORK.Product;

if _n_=1 then put 'Product ID';

put 'Product ID'n  10. ;

run;

SAS Employee
Posts: 73

Re: Writing fixed-width text files

i used the   if _n_ =1 then put .....and i'm still getting data in one line

Product ID

123

I woulud like the result to be:

Product ID

1

2

3

Thanks

Super User
Posts: 17,840

Re: Writing fixed-width text files

Post your code.

That isn't happening for me.

filename outfile "C:\_localdata\test.txt";

data _null_;

file outfile;

set sashelp.class;

if _n_=1 then put "Name";

put name $8.;

run;

SAS Employee
Posts: 73

Re: Writing fixed-width text files

Would this work on Linux ? Outfile file is on linux server.

Super User
Posts: 17,840

Re: Writing fixed-width text files

Yes it works on unix.

What does your data look like?

Another possibility is your text editor is garbling it. Try viewing it in something else or via the more command in unix.

You still haven't posted your code though.

SAS Employee
Posts: 73

Re: Writing fixed-width text files

Reeza,

I use the following code and when I transfer the file from Linux to windows files out put is  - Product_id 123.

However, when I edit the file out put is different -

Product ID

1

2

3

Reeza wrote:

Yes it works on unix.

What does your data look like?

Another possibility is your text editor is garbling it. Try viewing it in something else or via the more command in unix.

You still haven't posted your code though.

data product;
infile datalines;
input product_id $;
datalines;
1
2
3
;

filename outfile "/mnt/data01/test.txt";

data _null_;
file outfile Encoding=u;
set Product;
if _N_ = 1 then put  'Product_id';
do _N_ = 1 to length(Product_id);
id=substr(Product_id,_N_,1);
put @1'id'n 10.;
output;
end;
run;       

Trusted Advisor
Posts: 1,204

Re: Writing fixed-width text files

I think your dataset cotains one row 123. If that is the case then try this.

data product;
infile datalines;
input product_id $;
datalines;
123
;

filename outfile "C:\_localdata\test.txt";

data _null_;
file outfile;
set Product;
if _N_ = 1 then put  'Product_id';
do _N_ = 1 to length(Product_id);
id=substr(Product_id,_N_,1);
put @1'id'n 10.;
output;
end;
run;

Super User
Super User
Posts: 6,500

Re: Writing fixed-width text files

Sounds like your file is getting converted somehow in transit.

SAS is actually very good at looking at what is actually in your file.

Try this simple data step on your file.

data _null_;

   infile outfile ;

   input;

   list;

run;

Also try it using RECFM=F and LRECL=80 in the INFILE statement and then you can see what characters are placed between the lines.

Windows normally expects CR+LF ('0D0A'X) at the end of every line and Unix will normally just have LF ('0A'x).  In the old days Mac's used just CR ('0D'X) but I don't think they do that any more.

Solution
‎06-16-2014 04:31 PM
SAS Employee
Posts: 73

Re: Writing fixed-width text files

Thanks for all your help.  I was able to resolve the issue by using the "termstr=CRLF" option.

http://support.sas.com/kb/14/178.html

http://support.sas.com/kb/14/178.html

Thanks


☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 3949 views
  • 0 likes
  • 6 in conversation