The SAS Output Delivery System and reporting techniques

Replace text in RTF file adds unwanted spaces

Accepted Solution Solved
Reply
Occasional Contributor jb3
Occasional Contributor
Posts: 15
Accepted Solution

Replace text in RTF file adds unwanted spaces

Hi,

 

I create a report using tagsets.rtf_sample in SAS 9.4 M2 on Windows. For easier formatting of the headers in Word, I later would like to strip the section breaks from the RTF file. To do this, I found some macros here. The macros seem to work OK, but they add unwanted spaces to my RTF, which appear before the first letters of a paragraph. I suspect it has something to do with the way the rtf is read and/or written.

 

In the code below I create an RTF file, read it and write it out without replacing anything - and the spaces magically appear...! (see screenshot).

 

How can I get rid of the spaces? Any help is greatly appreciated.

 

Thanks

Jonas

 

goptions device=actximg;
options nodate nonumber papersize=A4;
options nocenter;
%let file="c:\test.rtf";
%let dest="c:\test_replaced.rtf";
ods tagsets.rtf_sample 
	file=&file.
	startpage=no
;

ods text='First Page';
ods text='Cars';
proc print data=sashelp.cars noobs;
	where Make='Infiniti';
	var make model;
run;
ods text='Class';
proc print data=sashelp.class noobs;
	where name='Alfred';
run;

ods tagsets.rtf_sample startpage=now;

ods text='Second page';
ods text='Cars';
proc print data=sashelp.cars noobs;
	where Make='Infiniti';
	var make model;
run;
ods text='Class';
proc print data=sashelp.class noobs;
	where name='Alfred';
run;

ods tagsets.rtf_sample close; 

%macro TestPut(source=, destination=);
	data temp;
		length line $1000;
		infile &source. length=lg lrecl=1000 end=eof;
		input @1 line $varying1000. lg;
	run;

	data _null_;
		set temp;
		file &destination. ls=1000;
		put line;
	run;
%mend TestPut;


%testput(source=&file., destination=&dest.);
Attachment

Accepted Solutions
Solution
‎01-16-2016 01:20 PM
Trusted Advisor
Posts: 1,114

Re: Replace text in RTF file adds unwanted spaces

[ Edited ]

EDIT: Oh dear, I realize that @data_null__ was faster.

EDIT 2: Lesson learned: Post a short answer quickly, add details later. Smiley Happy

----------

Original reply:

 

Hi Jonas,

 

I suggest the following solution: Replace the line

 

put line;

in macro TestPut by the two lines

 

 

lg=lengthn(line);
put line $varying1000. lg;

 

 

Rationale: "put line;" performs what is called simple list output. In contrast, "put line $varying1000. lg;" performs formatted output, in this case using the $VARYING1000. format, which corresponds to the $VARYING1000. informat, which was used to read the source file. There are at least two differences between these two output styles:

 

  1. Missing values of character variable LINE (these are represented by single blanks, as you certainly know) are written as they are, i.e. as single blanks, by simple list output. Using formatted output with the $VARYINGw. format with a length value of lg=0, however, they are written as null strings (i.e. actually not written at all). Thus you avoid the 19 blanks which were interspersed into your destination file test_replaced.rtf due to the simple list output. The LENGTHN function (unlike the LENGTH function) returns a value of 0 if the argument is a single blank.

  2. Leading blanks (i.e. indentations) are preserved by formatted output with the $VARYINGw. format, but not by list output, which would left align all written LINE values. (You may not have noticed this difference, because there happened to be no leading blanks in your source file.)

View solution in original post


All Replies
Respected Advisor
Posts: 3,775

Re: Replace text in RTF file adds unwanted spaces

Try using the $VARYING to write LINE.  Note the use of LENGTHN

 

lg = lengthn(line);
put line $varying1000. lg;
Solution
‎01-16-2016 01:20 PM
Trusted Advisor
Posts: 1,114

Re: Replace text in RTF file adds unwanted spaces

[ Edited ]

EDIT: Oh dear, I realize that @data_null__ was faster.

EDIT 2: Lesson learned: Post a short answer quickly, add details later. Smiley Happy

----------

Original reply:

 

Hi Jonas,

 

I suggest the following solution: Replace the line

 

put line;

in macro TestPut by the two lines

 

 

lg=lengthn(line);
put line $varying1000. lg;

 

 

Rationale: "put line;" performs what is called simple list output. In contrast, "put line $varying1000. lg;" performs formatted output, in this case using the $VARYING1000. format, which corresponds to the $VARYING1000. informat, which was used to read the source file. There are at least two differences between these two output styles:

 

  1. Missing values of character variable LINE (these are represented by single blanks, as you certainly know) are written as they are, i.e. as single blanks, by simple list output. Using formatted output with the $VARYINGw. format with a length value of lg=0, however, they are written as null strings (i.e. actually not written at all). Thus you avoid the 19 blanks which were interspersed into your destination file test_replaced.rtf due to the simple list output. The LENGTHN function (unlike the LENGTH function) returns a value of 0 if the argument is a single blank.

  2. Leading blanks (i.e. indentations) are preserved by formatted output with the $VARYINGw. format, but not by list output, which would left align all written LINE values. (You may not have noticed this difference, because there happened to be no leading blanks in your source file.)
Occasional Contributor jb3
Occasional Contributor
Posts: 15

Re: Replace text in RTF file adds unwanted spaces

Thank you very much, you made my day!
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 506 views
  • 3 likes
  • 3 in conversation