BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
newbi
SAS Employee

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

1 ACCEPTED SOLUTION

Accepted Solutions
newbi
SAS Employee

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

11 REPLIES 11
FloydNevseta
Pyrite | Level 9

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

if _n_=1 then put 'Product ID';

ballardw
Super User

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

Reeza
Super User

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;

newbi
SAS Employee

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

Reeza
Super User

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;

newbi
SAS Employee

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

Reeza
Super User

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.

newbi
SAS Employee

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;       

stat_sas
Ammonite | Level 13

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;

Tom
Super User Tom
Super User

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.

newbi
SAS Employee

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


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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