Data visualization with SAS programming

SAS/Graph: goptions for .pdf files

Reply
Contributor
Posts: 24

SAS/Graph: goptions for .pdf files

[Env: SAS 9.2, debian / linux]

Pasted below is a query I sent to SAS-L.  It was suggested that I repost here, since more SAS

developers read this list.  I've edited it slightly from the original.

I had several replies suggesting that I use ODS PDF instead of just relying on GOPTIONS settings.

So far, I'm resisting this because my goal is to get separate .PDF files of graphic images for use in papers and presentations

largely using pdfLaTeX, and I don't want all the printed output from procedures and macros cluttering this up.

As well, I'd like to avoid explicitly coding ODS PDF options into my source files, because I often need to

produce given graphs in multiple output formats. Hence, as I describe below, I've set up a system

that allows run-time selection of graphic format by essentially calling a macro at the start of a batch

job which sets up all the necessary GOPTIONS.

/* BEGIN EDITED RE-POST to SAS-L */

[Env: SAS 9.2, debian / linux]

For running SAS from a terminal window, I rely on a set of custom graphics utility macros to set

device goptions appropriately for different output formats (.ps, .eps, .gif, .png, ...)

so that I can easily run the same file unchanged to produce different output by changing an option

to the (perl) script I use to run SAS:

% mysas -v -d ps  myfile.sas   --> myfile.ps (one file, multiple pages)

% mysas -v -d eps myfile.sas --> myfile1.eps, myfile2.eps, ...

% mysas -v -d png myfile.sas --> myfile1.png, myfile2.png, ...

where the -v option opens these in an appropriate viewer.  I should also add that the mysas script

does some post-processing on image files as needed, e.g., fixing the bounding boxes for .eps

files.

I'm having trouble developing a similar macro for .pdf output which has the following features

(thinking of .pdf as similar to what I presently do for .ps output):

- honors HSIZE= and VSIZE= settings so that the page size can be specified

- can be made to honor GOPTIONS DISPLAY/NODISPLAY

- outputs multiple images to separately named .pdf files as for .eps, .png etc. 

- supports use of transparent colors (ie RGBA like color=aFF000080)

The macro %pdf below is my current version, which uses the PDFC driver. The problems are that

- only the last graph produced in a given run appears in the .pdf file, at least when there are

  multiple graphs produced in

- Using xpdf as the viewer, the .pdf file appears in a full 8.5 x 11 page, although the size of the graph

within that page is modified by HSIZE and VSIZE settings.

- Attempts to use transparent colors generates error messages from xpdf [Are RGBA colors supported

in SAS/Graph for 9.2?]

% mysas -v -d ps logist1b

logist1b.ps, 3 pages

% mysas -v -d pdf logist1b

1 images: logist1b.pdf

Error (52190): Illegal character '>'

Error: PDF file is damaged - attempting to reconstruct xref table...

Can some one help me sort out the appropriate goptions for this case?

/*--------------------------------------------------------------*

  *    Name: pdf.sas                                             *

  *   Title: Set graphics parameters for PDF file output         *

  *                                                              *

  *--------------------------------------------------------------*

  *  Author:  Michael Friendly <friendly@yorku.ca>    *

  * Created:  5 Dec 1996 14:30:47                                *

  * Revised:  5 Jan 1997 10:46:34                                *

  * Version:  1.1                                                *

  * Dependencies:                                                *

  *   sasgfile                                                   *

  *--------------------------------------------------------------*/

/*=

=Description:

The PDF macro initializes SAS/GRAPH to produce PDF output.

It sets the name of the output file based on the SASFILE

or GSASFILE environment parameters, or 'grfout.pdf' if neither of these

variables are defined.

The output file is written to the current SAS directory.  You can

override this by defining a global macro variable GSASDIR.

=*/

%global gsasfile gsasdir devtyp;

%macro pdf(

    fn,

    hsize=6in,

    vsize=6in

    );

    %let devtyp=PDF;

    %let dev=pdfc;

    %local gprolog gaccess;

    %*-- Get the basename of the graphic file(s);

   %sasgfile(pdf,&fn);

   %put PDF: gsasfile is: "&gsasdir.&gsasfile";

   filename gsasfile  "&gsasdir.&gsasfile";

    %if &sysver < 6.08 %then %do;

        %let gprolog='2521'x;

        %let gaccess=sasgaedt;

        %end;

    %else %do;

        %let gprolog=;

        %let gaccess=gsasfile;

        %end;

goptions device=&dev gaccess=&gaccess gsfname=gsasfile gsflen=80

   hpos=70   vpos=65                /* match pscolor device */

   gsfmode=append  gprolog=&gprolog;

goptions lfactor=3;

goptions ftext='helvetica';

goptions hsize=&hsize vsize=&vsize;

%mend;

%global gsasfile;

%macro sasgfile(ft,fn);

  %* -----------------------------------------------------------;

  %* Set the filename of the output graphics file               ;

  %* -----------------------------------------------------------;

%local gsasfn sasfn;

%if %length(&fn) %then %let gsasfile = &fn..&ft;

%else %if %defined(sasfile) %then %let gsasfile = &sasfile..&ft;

%else %do;

    %let sasfn =%scan(%SYSGET(SASFILE),1,%str(.));

    *-- Set the name of the output file;

    %let gsasfn =%SYSGET(GSASFILE);

    %if %length(&gsasfn)=0 %then %if %length(&sasfn) > 0

        %then %let gsasfn =&sasfn..&ft;

        %else %let gsasfn=grfout.&ft;

    %let gsasfile=&gsasfn;

%end;

%put SASGFILE: gsasfile is &gsasfile;

%mend sasgfile;

SAS Super FREQ
Posts: 8,820

SAS/Graph: goptions for .pdf files

Hi:

  I don't have a lot to contribute to the overall macro development discussion. But I do have something to contribute that might be useful in clarifying what a PDF file is, at least when created by ODS PDF....which may inform some of what you observe in the file that's been created when you use GSFNAME  and DEVICE= in GOPTIONS.

  When I first started writing programs that used ODS PDF and teaching students to create PDF files, I was told to imagine that my PDF file was being formatted on a virtual piece of paper, that would be sent to the PDF file instead of being sent to a physical printer. I stopped using GSFNAME when ODS came out -- so I have to admit that I never tried to do what you're doing. I know that using HSIZE and VSIZE with some devices will control the actual size of the output file being created and for image types like JPEG, GIF and PNG, this is what I observe.

  But a PDF file is a proprietary file format. So I wonder (but don't know for sure) whether what you observe is a side effect of that behavior. (You said: "Using xpdf as the viewer, the .pdf file appears in a full 8.5 x 11 page, although the size of the graph within that page is modified by HSIZE and VSIZE settings.")

  It was my understanding that PDF, when created by ODS at least, could only create files and output as controlled by regular SAS options such as TOPMARGIN, BOTTOMMARGIN and the various PAPERSIZE options. Because of that I always expect a PDF file to be created as a paper-sized output. And, I wouldn't expect HSIZE and VSIZE to change anything but the size of the image.

  I don't know what's behind the scenes of GSFNAME when you use the PDFC driver, that's probably a question for Tech Support. Can you create a proprietary PDF file that is 6"x6" using Adobe Acrobat -- if SAS is out of the picture? Anyway, I think that for the most complete answer to your question and help with your macro, if what you want to do is possible to create a PDF file,

  Also, I thought that RGBA was production in 9.3 (not 9.2), so I'm not sure that transparency will work in 9.2. Here's the 9.3 doc page:

http://support.sas.com/documentation/cdl/en/graphref/64854/HTML/default/viewer.htm#p0ekhb3mdqahk3n15...

  This may or may not be a helpful document:

http://support.sas.com/resources/papers/proceedings10/035-2010.pdf

  And, this earlier document that talks about PDF and GSFNAME may be useful:

http://support.sas.com/techsup/technote/ts659/ts659.html (where it says that: "With the PDF and PDFC device drivers, you cannot use the GSFMODE=APPEND option to write multiple graphs to the same file. In order for these files to contain multiple graphs, all the graphs must be written by a single procedure. This can be accomplished using BY-group processing, RUN-group processing, or the GREPLAY procedure.") Also in this document, it talks about each graph (when using PDF or PDFC drivers) being written to a separate "page" of output -- so this implies to me that .PDF files created with the GSFNAME method will create paper-sized/page-based PDF output.

  Sorry I don't have anything more helpful to add.

cynthia

Contributor
Posts: 24

SAS/Graph: goptions for .pdf files

Hi Cynthia

Thanks for clarifying things regarding your understanding of PDF graphic output in SAS.  As I mentioned, my interest is in

getting graphs in PDF format that I can include in other documents, typically LaTeX, processed via pdflatex.  I can already

do this as I want using PNG and JPG drivers in SAS, but those are bit-mapped rather than vector graphics formats, and

so resolution becomes another issue.

What I am wanting is something akin to what I can do in R, using

pdf(file="myfile.pdf", width=6, height=6)

plot(1:10)

dev.off()

or simply

plot(1:10)

dev.copy2pdf(file="myfile.pdf", width=6, height=6)

These give PDF files of the proper size with tight bounding boxes

ODS PDF is very good at creating complete PDF documents with lots of features, but it seems too hard to use for my purpose,

from what I've read.

I am concluding from this that there is a hole in the device drivers for PDF graphics in SAS, and so for now, I will have to stick

with PNG (and JPG) drivers for use in LaTeX.  My guess is that there has been no recent development of these drivers since the TS659 document because ODS PDF was considered to have superceded it.  If so, that is a shame.

Thanks also for your remark about transparency.  On Windows, I run SAS 9.3 and find RGBA works better there.

best,

-Michael

SAS Super FREQ
Posts: 925

SAS/Graph: goptions for .pdf files

Hey Michael,

Since you're running SAS 9.3, would the following code work for you?

ods listing;

ods graphics / outputfmt=pdf imagename="BarChart";

proc sgplot data=sashelp.class;

vbar age / response=height stat=mean;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 1096 views
  • 0 likes
  • 3 in conversation