DATA Step, Macro, Functions and more

Inserting white spaces at the end of string

Reply
Occasional Contributor
Posts: 6

Inserting white spaces at the end of string

Hi all,

 

I have one file details.txt with data:-

ajay    manager    account    45000
sunil   clerk      account    25000
varun   manager    sales      50000
amit    manager    account    47000
tarun   peon       sales      15000
deepak  clerk      sales      23000
sunil   peon       sales      13000
satvik  director   purchase   80000

 I want output file out.txt same as input file details.txt but want spaces after datas to make all rows of 480bytes long.

 To achieve this i have did :-

=============================================================================================

 

DATA readFile;

      INFILE '/home/inputFile/details.txt';

      INPUT line $ 1-35;

RUN;

 

PROC sql;

      CREATE TABLE final(data char(480));

      INSERT INTO final(data) select line FROM readFile;

QUIT;

 

PROC REPORT DATA=final NOHEADER;

RUN;
FILNAME zout '/home/output/out.txt';

 

DATA _null_;
  SET final end=eof;          * set the dataset final ;

  RETAIN fid 0;

  if _n_ eq 1 then
     fid = fopen('zout','O');

  if fid gt 0 then do;
    rc = fput(fid,data);          * data is a variable of dataset final ;
    rc = fwrite(fid,'P');
  end;

  if eof then
     rc = fclose(fid);
run;

FILNAME zout;

 

=============================================================================================

 

but in output file i got each row of length 256 bytes but i want it 480 bytes.

 

Thanks in advance...

Please provide me a solution to achieve my desired output.

 

 

Super User
Posts: 10,574

Re: Inserting white spaces at the end of string

Use the lrecl= option in the filename statement to increase the buffer size (default is 256).

But why don't you do

data _null_;
infile '/home/inputFile/details.txt' truncover;
file '/home/output/out.txt' lrecl=480;
input line $480.;
put line $480.;
run;

?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Valued Guide
Posts: 629

Re: Inserting white spaces at the end of string

Most times reading the documentation is enlightening. The file-statment has an option called "pad" controlling "whether records written to an external file are padded with blanks to the length that is specified in the LRECL= option."

 

So you need the options lrecl=480 and pad in your file-statement. Instead of pad you could use recfm=f.

Ask a Question
Discussion stats
  • 2 replies
  • 53 views
  • 1 like
  • 3 in conversation