Help using Base SAS procedures

generating .txt file output

Reply
Frequent Contributor
Posts: 85

generating .txt file output

hi,

i am generating .txt file with following code

example

Data test;

input name $1-1 age $ 3-5 sex $ 6-7;

cards;

a   m

b 14 m

c 15 m

d 8  f

e    f

;

run;


data newtest;

file "path.txt";

if missing(age) then do;

want = catt(name,'|',age,'  |',sex);

put @1 want;

end;

else do;

want=catt('|',name,age,sex);

put want;

end;

run;

i need to stop cursor i last observation of last column but
in text file after last row cursor is moving to next line(one blank space)

wright now iam placing cursor at last obs and using delete key

this .txt file is entering in software

it is not accepting if blank space is comming

is there any way to stop cursor at last obs after completing of data

Trusted Advisor
Posts: 2,113

Re: generating .txt file output

Much easier to just use a macro like %sas2csv.

SAS-macros/sas2csv2.sas at master · friendly/SAS-macros · GitHub

Doc Muhlbaier

Duke

Super User
Posts: 9,676

Re: generating .txt file output

You need recfm= option to change the format of recod into fixed format.

filename x 'c:\x.txt';
Data test;
input name $1-1 age $ 3-5 sex $ 6-7;
cards;
a   m
b 14 m
c 15 m
d 8  f
e    f
;
run;


data newtest;
set test;
file x recfm=f;
if missing(age) then do;
want = catt(name,'|',age,'  |',sex);
put @1 want;
end;
else do;
want=catt('|',name,age,sex);
put want;
end;
run;

Ksharp

Frequent Contributor
Posts: 85

Re: generating .txt file output

Hi Kshap,

Thanks for your support

i am getting output but only missing values in text

a|   |m                                   b|14|m                                           c|15|m                                                      d|8|f

e|   |f

i want output as

 

a|  |m

b|14|m

c|15|m

d|t8|f

e|  |f

Attachment
Super User
Posts: 9,676

Re: generating .txt file output

Sorry. I can't help you. Maybe you can talk to Technique Support.

SAS Super FREQ
Posts: 683

Re: generating .txt file output

Hi

How about using the DLM= option of the FILE statement

Data test;
  input name $1-1 age $ 3-5 sex $ 6-7;
cards;
a    m
b 14 m
c 15 m
d 8  f
e    f
;

data _null_;
 
set test;
  file "c:\temp\someFile.txt" dlm="!";
 
put name age sex;
run;
New User
Posts: 1

Re: generating .txt file output

Use the CAT function in stead of CATT (TRIM) or CATX (TRIM(LEFT))

DATA test;
INPUT name $1-1 age $ 3-4 sex $ 6-6;
CARDS;
a    m
b 14 m
c 15 m
d  8 f
e    f
;
RUN;

/*
Function              Equivalent Code 
CAT(OF X1-X4)         X1||X2||X3||X4

CATS(OF X1-X4)        TRIM(LEFT(X1))||TRIM(LEFT(X2))||TRIM(LEFT(X3))||TRIM(LEFT(X4))

CATT(OF X1-X4)        TRIM(X1)||TRIM(X2)||TRIM(X3)||TRIM(X4)

CATX(SP, OF X1-X4)    TRIM(LEFT(X1))||SP||TRIM(LEFT(X2))||SP||TRIM(LEFT(X3))||SP||TRIM(LEFT(X4))
*/

DATA _NULL_;
SET test;
FILE LOG;
want = CAT (name, '|', age, '|', sex);
PUT @1 want;
RUN;

/*
a|  |m
b|14|m
c|15|m
d|8 |f
e|  |f
*/

Super User
Super User
Posts: 6,499

Re: generating .txt file output

Sounds like you are saying that you want to generate a text file where the end of line character at the end of the last line does not exist?  If so and your program does not except that input it seems strange to me as that is the reverse of what most programs want. I have had many programs complain when the last line does NOT have and end of line character.

It is really hard to get SAS to not write that final end of line.  You could try writing ASCII code 26 ( '1A'x) at the end of the last line.  This won't stop SAS from also writing the end of line after it, but perhaps the other program will truncate input when it sees it.

data _null_;

  file 'out.txt' ... ;

  set have end=eof;

  put .... @;

  if eof then put '1A'x @;

   else put;

run;


Super User
Posts: 9,676

Re: generating .txt file output

HaHa, I found a way.

data _null_;
file 'c:\x.txt' recfm=f ;
set sashelp.class;
length a $ 256;
a=cats(_n_,of _all_);
put a $800.;
run;

Ksharp

Ask a Question
Discussion stats
  • 8 replies
  • 452 views
  • 1 like
  • 6 in conversation