BookmarkSubscribeRSS Feed
Khadar_Shaik
Calcite | Level 5

I am trying to concatenate multiple RTF reports into a single RTF file by importing into SAS data and Exporting to Combine RTF file. When I am doing that everything is transferred exactly except the leading spaces in column labels for indentation purpose so, could anyone help me with this.

 

I am getting the same leading spaces from import of RTF in the final dataset that is ready to export but it is not showing in the concatenated RTF File. The below is the SAS code to Export the data into RTF file

 

data _null_;
  set result_s1 end=eof ;
  file "../../documentation/combined.rtf" ;
  if not delete then do;
    if outrtf eq "" then put inrtf;
    else do;
      if eof then put inrtf;
      else put outrtf;
    end;
   end;
run;


SAS Dataset with preserved leading spacesSAS Dataset with preserved leading spaces
 
4 REPLIES 4
quickbluefish
Barite | Level 11

Are the leading spaces you're trying to preserve the ONLY spaces in the document?  If so, then a hack-y kind of way to do this is to simply replace all the spaces with some character and then, in your output RTF file, find/replace those characters back to spaces.  

 


(in your data _null_ step... -- use some character that doesn't otherwise appear in your data.)

inrtf=tranwrd(inrtf, ' ', '#');

If there's no single character that's certain not to appear in your data, then just replace spaces with a string, e.g. "#SPACE#"

Khadar_Shaik
Calcite | Level 5

This will make more manual work if we have more files or more pages in the same report.

Tom
Super User Tom
Super User

The normal $ format does not preserve leading spaces.  Use either $CHAR or $VARYING.  For this application $VARYING is better since it will not add extra trailing spaces.

data _null_;
  set result_s1 end=eof ;
  file "../../documentation/combined.rtf" ;
  if not delete then do;
    len_in=lengthn(inrtf);
    len_out=lengthn(outrtf);
    if outrtf eq "" then put inrtf $varying3000. len_in;
    else do;
      if eof then put inrtf $varying3000. len_in;
      else put outrtf $varying3000. len_out;
    end;
   end;
run;

You could probably simplify the logic by using the COALESCEC() function. 

data _null_;
  set result_s1 end=eof ;
  file "../../documentation/combined.rtf" ;
  if not delete;
  outrtf=coalescec(outrtf,inrtf);
  len_out=lengthn(outrtf);
  put outrtf $varying3000. len_out;
run;

But I don't understand why you wanted the special case for the last line.

Tom
Super User Tom
Super User

Note that you will have a similar issue when using the $ INFORMAT to read in the original file.

 

Consider this text file:

This in example
  file with some
     lines with 
       leading
spaces.  And some lines
without.

You can read it in using the automatic _INFILE_ variable.

data have;
  infile rtf ;
  input;
  line=_infile_;
run;

Or use the $CHAR informat.

data have;
  infile rtf truncover;
  input line $char300.;
run;

Then you can write it out using the $VARYING format.

data _null_;
  set have;
  file out;
  len=lengthn(line);
  put line $varying300. len ;
run;

You can use the LIST statement to check the contents:

56   data _null_;
57     infile out;
58     input;
59     list;
60   run;

NOTE: The infile OUT is:

      Filename=C:\Users\...\#LN00012,
      RECFM=V,LRECL=32767,File Size (bytes)=103,
      Last Modified=25Apr2025:07:59:58,
      Create Time=25Apr2025:07:59:58

RULE:     ----+----1----+----2----+----3----+----4----+----5----+----6----+---
1         This in example 15
2           file with some 16
3              lines with 15
4                leading 14
5         spaces.  And some lines 23
6         without. 8
NOTE: 6 records were read from the infile OUT.
      The minimum record length was 8.
      The maximum record length was 23.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 432 views
  • 0 likes
  • 3 in conversation