BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TD-A
Fluorite | Level 6

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

11 REPLIES 11
PGStats
Opal | Level 21

Why not take out the statement

 

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

 

 

PG
Ksharp
Super User

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;
TD-A
Fluorite | Level 6
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.
TD-A
Fluorite | Level 6
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. 😞 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..
Ksharp
Super User

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;
TD-A
Fluorite | Level 6
Thanks again, Xia Keshan! It worked this time, but there's a '|' at the end of every row. 😞
Ksharp
Super User

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;
TD-A
Fluorite | Level 6
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 ' '. 😞
Ksharp
Super User

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;
TD-A
Fluorite | Level 6
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.

ballardw
Super User

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

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
  • 4604 views
  • 5 likes
  • 4 in conversation