DATA Step, Macro, Functions and more

Carriage Return on output (Text file) - how to remove?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Carriage Return on output (Text file) - how to remove?

Hi,

 

I'm having trouble with my output (Text) file.  

 

Here's my code:

data null;
file &file3 linesize=3200 dlm='|' ;
set USDATM_&MTH. end=eof;

 

if _n_= 1 then do;

put
'ATM_ID'
'|ATM_ALT_ID'
'|ATM_USD_CSH_IND'
;
end;

put
atm_id
atm_alt_id
atm_usd_csh_ind
;

 

if eof and char in ('0D'x, '0A'x) then
put @;

run;

 

My output looks like this:

VAR1|VAR2|VAR3
ABC5|FD53|Y
DEF0|FE78|Y
FDS9|EW38|Y
FDR4|QW82|Y

 

I want to remove the extra line at the bottom.. 

 

Please help.

 

thanks

 


Accepted Solutions
Solution
‎06-07-2016 01:25 PM
Super User
Posts: 10,040

Re: Carriage Return on output (Text file) - how to remove?

OK. How about this one ?

 

data _null_;
 set sashelp.class end=last;
 file '/folders/myfolders/want.txt' recfm=n;
 if _n_=1 then put "Name|sex|age|weight" '0D0A'x;

 put name +(-1)'|'  sex +(-1)'|' age +(-1)'|' weight ;
 if not last then put +(-1) '0D0A'x;
run;

View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: Carriage Return on output (Text file) - how to remove?

Why not take out the statement

 

if eof and char in ('0D'x, '0A'x) then
put @;

 

 

PG
Super User
Posts: 10,040

Re: Carriage Return on output (Text file) - how to remove?

Here is an example. Is this file for windows or for unix ?

Windows use '0D0A'x 

Unix use '0A'x 

 

 

data _null_;
 set sashelp.class end=last;
 file '/folders/myfolders/want.txt' recfm=n ;
 put name +(-1)'|' sex  +(-1)'|'  age  +(-1)'|' weight @;
 if not last then put '0D0A'x;
run;
Occasional Contributor
Posts: 10

Re: Carriage Return on output (Text file) - how to remove?

Thanks so much, Xia Keshan. I'm remotely connecting to UNIX but my output is saved in a shared network (on Windows). I've tried your solution but there is a space after the last field/column and the first line went to the 'header line'. e.g. ATM_ID|ATM_ALT_ID|ATM_USD_CSH_INDNE4|NA5|Y RE8|RE7|Y JB9|JA3|Y HG4|HD8|Y Really appreciate your help.
Occasional Contributor
Posts: 10

Re: Carriage Return on output (Text file) - how to remove?

Hi again Xia Keshan, I've resolved the issue with the space (' ') after the column/field but not the first line that's joining the header or the first row. Smiley Sad So, I did: put atm_id +(-1)'|' atm_alt_id +(-1)'|' atm_usd_csh_ind +(-1)@ ; although there is still a ' ' at the very last row after the column/field..
Super User
Posts: 10,040

Re: Carriage Return on output (Text file) - how to remove?

OK. Try this one .

 

data _null_;
 set sashelp.class end=last;
 file '/folders/myfolders/want.txt' recfm=n dlm='|';
 if _n_=1 then put "Name|sex|age|weight" '0D0A'x;

 put name  sex  age weight ;
 if not last then put  '0D0A'x;
run;
Occasional Contributor
Posts: 10

Re: Carriage Return on output (Text file) - how to remove?

Thanks again, Xia Keshan! It worked this time, but there's a '|' at the end of every row. Smiley Sad
Solution
‎06-07-2016 01:25 PM
Super User
Posts: 10,040

Re: Carriage Return on output (Text file) - how to remove?

OK. How about this one ?

 

data _null_;
 set sashelp.class end=last;
 file '/folders/myfolders/want.txt' recfm=n;
 if _n_=1 then put "Name|sex|age|weight" '0D0A'x;

 put name +(-1)'|'  sex +(-1)'|' age +(-1)'|' weight ;
 if not last then put +(-1) '0D0A'x;
run;
Occasional Contributor
Posts: 10

Re: Carriage Return on output (Text file) - how to remove?

Yes, that solved it but, there's still a ' ' after the last field/column at the bottom. so, it's like: name|sex|age XXXX|F|25 and I have like 12 text files (output files), I have to open each one to remove that extra ' '. Smiley Sad
Super User
Posts: 10,040

Re: Carriage Return on output (Text file) - how to remove?

OK. This is really not easy. This code ought to be right.

 

data _null_;
 set sashelp.class end=last;
 file '/folders/myfolders/want.txt' recfm=n;
 if _n_=1 then put "Name|sex|age|weight" '0D0A'x;
 length x $ 4000;
 x=catx('|',name,sex,age,weight);
 len=length(x);
 put x $varying4000. len ;
 if not last then put '0D0A'x;
run;
Occasional Contributor
Posts: 10

Re: Carriage Return on output (Text file) - how to remove?

Thanks so much, Xia Keshan! This is the answer indeed! Thank you, thank you, thank you so much! Sorry for not getting back to you any sooner. I've been busy with other projects.

Super User
Posts: 11,343

Re: Carriage Return on output (Text file) - how to remove?

You may want to look at the TERMSTR option on your FILE statement. It sets whether the end of line is CRLF (windows default) LF  or NL

☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 507 views
  • 5 likes
  • 4 in conversation