0
votes

The response to the question Is it possible to import a PNG file into SAS to include in RTF output? gave a way to use an external PNG file in SAS ODS RTF output. In order for the provided solution to work, it requires text to follow the insertion of the image file. One way that I used to make it work even when I didn't have text that followed the image was to insert underscores following the image which would serve has a bottom border prior to the footnotes; however, this does not conform to the other outputs that are created for this project, so I'm looking for a way to import a PNG that maintains the essential formatting.

Additionally, I would like to be able to adjust the display size of the image. Currently, I have to make sure that the image size is exactly right when it is created so that it doesn't overrun the borders, but this creates an image that is more pixelated than I would like. I would like to be able to force it to be the exactly the desired size from SAS, so that I can have some flexibility in the size when I create the image and produce an image that is high enough quality in the RTF file.

While this may seem like separate questions, it seems to me that the solution to one problem will likely address both, as you'll see below.

Here is some code to produce a minimal example:

/* Create test data */
data test;
  drop i;
  call streaminit(1579);
  do i = 1 to 200;
    u = rand("Normal");
    output;
  end;
run;

/* Create a PNG file to bring into the RTF -- the PNG will
   actually be created outside of SAS but is included here
   for convenience */
proc sgplot data=test;
  density u / type=kernel;
run;

/* Set options for RTF output */
option nodate nonumber;
ods rtf file = "test.rtf" nogtitle nogfoot
ods escapechar='~';

/* Titles and footnotes */
title 'Title';
/* Border line at the start of the footnote section */
footnote height=2pt '~R"\brdrb\brdrs\brdrw30';
footnote2 j=l 'Footnote';

/* Import the image and output into the RTF */
ods text='~S={preimage="SGPlot1.png"}';
ods rtf close;

The above code produces a document that looks like this:

Wrong Output

That thin vertical line in the body is the image. If I drag the handles to the right, the full image appears. I would like to be able to create the equivalent of the following code without creating the image in SAS:

/* Set options for RTF output */
option nodate nonumber;
ods rtf file = "test1.rtf" nogtitle nogfoot;
ods escapechar='~';

/* Titles and footnotes */
title 'Title';
/* Border line at the start of the footnote section */
footnote height=2pt '~R"\brdrb\brdrs\brdrw30';
footnote2 j=l 'Footnote';

ods graphics / height=9in width=7in;

/* Import the image and output into the RTF */
proc sgplot data=test;
  density u / type=kernel;
run;
ods rtf close;

This code produces the following:

Desired Output

Notice that I've enlarged the image (SAS will automatically adjust pixels etc. when creating the image, but this is not necessary since the PNG file will already be created). Notice also that the image is actually displayed when the RTF is produced and doesn't require any post processing. Is this possible?

1

1 Answers

0
votes

After some investigation I found there are two parts to this answer. Making the image show up at all instead of having no width can be done by adding width=100% to the style as follows:

ods text='~S={width=100% preimage="SGPlot1.png"}';

Adjusting the dimensions of the image is a little more hack-ish, but it can be done by reading the file in as text and replacing the rtf control words pichgoalN and picwgoalN with the desired height and width in twips (a twip is 1/1440 inch). Here's how I did it:

data edit;
  infile "test.rtf" dlm='09'x dsd lrecl=32767 missover;
  format var $200. varout $200.;
  input var $;
  varout = prxchange("s/pichgoal\d+/pichgoal12960/",-1,var);
  varout = prxchange("s/picwgoal\d+/picwgoal10080/",-1,varout);
run;

data _null_ ;
  set edit ; 
  FILE 'test1.rtf';
  PUT varout; 
run ;