BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jb3
Obsidian | Level 7 jb3
Obsidian | Level 7

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.);

Spaces inserted.png
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

3 REPLIES 3
data_null__
Jade | Level 19

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

 

lg = lengthn(line);
put line $varying1000. lg;
FreelanceReinh
Jade | Level 19

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.)
jb3
Obsidian | Level 7 jb3
Obsidian | Level 7
Thank you very much, you made my day!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 2309 views
  • 3 likes
  • 3 in conversation