BookmarkSubscribeRSS Feed

When Do I Use SG Procedures vs. Graph Template Language?  Q&A, Slides, and On-Demand Recording

Started ‎03-09-2022 by
Modified ‎04-15-2022 by
Views 1,834

Watch this Ask the Expert session to learn the pros and cons of SG Procedures and Graph Template Language along with a process for deciding which method to use. 

 Watch the webinar

This webinar includes:

  • A clear understanding of the differences between SG Procedures and Graph Template Language.
  • An understanding of when it is appropriate to use each method.
  • Tips for how to get started on writing Graph Template Language.

 

The questions from the Q&A segment held at the end of the webinar are listed below. The slides and code shown during the webinar are attached.

 

Q&A

Can you have graphs from different data with the multi cell GTL?

When you work with ODS Graphics (Graph Template Language or SG Procedures), all your data needs to be in the same data set. So, you must get creative with your data. You may have two data sources, but that is not to say you cannot combine those two data sources into one source. The first rows can be from one data source, and the bottom rows can be from another data source but different columns. You need to get all your data into one data set, it is doable. By putting the different data sources into one data set populating different columns, it is a way to choose what you want for each cell.

 

Just like SGPLOT, can SGMAP code can be translated into GTL template?

With SAS 9.4, the TMPLOUT= option is only available with the SGPLOT and SGSCATTER steps. The TMPLOUT= option writes the Graph Template Language code for your graph to a file.

 

Can we get GTL on SAS 9.4?

Yes, if you have SAS 9.2 or later, you have ODS Graphics. If you are still on 9.2, you do have to have a license for ODS Graphics. But starting in version 9.3, ODS Graphics is part of Base SAS. So, yes, you do have access to GTL with SAS 9.4. For my demos, I used SAS Studio as my interface which pointed to a SAS 9.4 session. You can code GTL in SAS Studio, SAS Enterprise Guide, or the SAS windowing environment.

 

What is the SAS Studio button that formats code?

It is on your Code toolbar. The second button from the right in SAS Studio in my version. If I hover over, it says Format Code. If you click that button, you will notice the difference. It takes code with no indentation and adds some. It is a good starting point. Have Studio format the code and then tweak the formatting as you desire. 

MicheleEnsor_0-1646844227504.png

 

Does TMPLOUT= work with PROC SGPANEL?

In SAS 9.2, TMPLOUT= was a valid option for PROC SGPANEL. However, the option was removed for SAS 9.3 and later versions. PROC SGPANEL does not support the TMPLOUT option because there is processing performed by the procedure that is not included in the Graph Template Language code.  

 

Is there a way to obtain a list of styles to determine what "parent" styles are available? (e.g., proc template; define style styles.boxplots)

The PROC TEMPLATE step is used to create graph templates as discussed in this webinar but the PROC TEMPLATE step can also be used to create style templates. The style template controls the visual presentation of your tabular and graphical output.

To see a listing of the style templates supplied by SAS submit the following:

proc template;

  list styles / store=sashelp.tmplmst;

run;

To see the source code for a style template, submit code like the following:

proc template;

  source styles.HTMLBlue / store=sashelp.tmplmst

                           file='C:\temp\htmlbluestyle.sas';

run;

The above code shows the source code for the HTMLBlue style template in a SAS program.

If you open the SAS program, you will see the style template code. The PROC TEMPLATE statement and the RUN statement will be missing from the file. Below are the first three lines of what shows up in this example.

define style Styles.HTMLBlue;

   parent = styles.statistical;

   class GraphColors /

As shown with the above code, the HTMLBlue style inherits from the parent template of Statistical. If you view the source code for Statistical style template, you will see that template inherits from the parent template of Default. Many of the style templates inherit back to the Default style template.

 

Here are some useful documentation links related to style templates:

 

How do statistical graphics overlap / be replaced with ODS graphics?

If you are using a statistical procedure which creates output that contains graphs (example: PROC REG), ODS Graphics is being used behind the scenes. This is where ODS Graphics got its start. The SG in procedure names stand for statistical graphics. If you are not able to do everything you want to the graph in the statistical procedure, you might consider switching to SG procedures or GTL code based on the output data of the statistical procedure.

 

I use Graph Template Language when I need help and start with %SGDESIGN. I don't often use Enterprise Guide or Studio. Does either of them help develop code for SG Procedures?

%SGDESIGN starts ODS Graphics Designer, a point and click interface to create graphs based on ODS Graphics. From this interface, you can see the underlying code which will be GTL code. The Designer does not show SG procedure syntax. In both SAS Enterprise Guide and SAS Studio, you have point and click tasks under the Graph category. Enterprise Guide tends to write SAS/Graph code (G procedures) and Studio tends to write ODS Graphics code (SG procedures).

 

Please discuss the priority order of commands between options on your SGPLOT commands and the options you might specify in ODS statements or an attribute dataset. 

Regarding style attributes, a graph gets its “style” based on the style template that is used by default with the ODS destination. If you do not like the default style, you can choose a different style template by using STYLE= on the ODS destination statement. However, any style attribute that you specify within the graphing procedure will overwrite the style template. If you are using an attribute map dataset, avoid putting attributes directly within procedure so that the attribute map is used.

 

Also, do you ever use GTL along with SG procedures?  I've inherited code that contains both PROC TEMPLATE and PROC SGPLOT.

Within a program, I will tend to use SG procedures such as SGPLOT. However, if there is a graph, I cannot create with a SG procedure then I will use GTL followed by SGRENDER. So yes, it is likely that you will see both GTL with SGRENDER along with other SG procedures such as SGPLOT within one program.

 

Can you show us what the code looks like when you use the tmplout= option in an SG Procedure? Do you just open the file and copy and paste the code?

Below is an example of using TMPLOUT= with the SGPLOT procedure.

proc sgplot data=sashelp.class;

            tmplout="c:\temp\MyHistoPgm.sas";

    histogram weight / showbins;

run;

After submitting the above code, you will open the program named MyHistoPgm.sas. The program will look like the following. You can now modify this code or copy/paste the code into another program.

proc template;

define statgraph sgplot;

begingraph / collation=binary;

layout overlay / yaxisopts=(labelFitPolicy=Split) y2axisopts=(labelFitPolicy=Split);

   Histogram 'Weight'n / primary=true binaxis=true LegendLabel="Weight";

endlayout;

endgraph;

end;

run;

 

In one of your examples, you used the "HTML Color codes" and also wrote out the "color name, like Red", what the purpose of using the "HTML Color Codes"?

Within SAS, you have numerous methods for specifying colors. Use whichever color method is familiar to you. Here is a link to the documentation that mentions the color naming conventions you can use.: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/odsug/p0jrvtifiwse7qn1kgce32eg83dl.htm

 

How do you prevent labels overlapping in a pie chart?

In general, overlapping labels is an issue that can occur with pie charts. Personally, I limit my use of pie charts and will tend to only use if I have a small number of pie slices. If you are using PROC SGPIE, I suggest looking at the documentation for the DATALABELDISPLAY= and DATALABELLOC= options. https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatproc/p02p7mhmfpwsixn16th3fjtr35uw.ht...

 

How do I add lines between slices in PROC SGPIE?

The OUTLINEATTRS= option can be used in the PIECHART statement within PROC TEMPLATE step to specify the properties of the pie and slice outlines.

proc template;

define statgraph simplepie;

  begingraph;

    entrytitle "Car Models by Origin";

    layout region;

      piechart category=origin / datalabellocation=outside

               outlineaattrs=(color=red pattern=2 thickness=5);

    endlayout;

  endgraph;

end;

run;

proc sgrender data=sashelp.cars

              template=simplepie;

run

 

One of the common things I do with my figures are change font size etc. Where I should do it?

Within the statements in the SG procedures, you can control font attributes using the appropriate ATTRS option to change the specific text. Similar options are used with GTL.

proc sgplot data=sashelp.class;

  vbar sex / datalabel datalabelattrs=(Color=red Family=Arial Size=12

                                       Style=Italic Weight=Bold);

  xaxis labelattrs=(Color=red Family=Arial Size=12

                    Style=Italic Weight=Bold)

        valueattrs=(Color=red Family=Arial Size=12

                    Style=Italic Weight=Bold);

run;

 

Could I use GTL within Enterprise Guide? What version is needed?

Yes, you can code GTL with SAS Enterprise Guide. Your Enterprise Guide is sitting on a version of SAS. As long as your version of SAS is 9.2 or later, you can code GTL.

 

Can we have the syntaxes demonstrated in this webinar?

Yes, the demo programs used in this webinar will be included in the SAS Communities page for the webinar. To run the demo programs, you will need to create the needed data sets using the cre8data.sas program. Prior to running the program, first open the cre8data.sas program and modify the two paths to point to your data location.

 

Our organization has used MS suite for graphics, but we used DDE, which we can't use for our new Viya environment - so, we're trying to replicate MS graphics, but using SG/GTL, do you think that's possible?

ODS Graphics can create graphs that you can open in Microsoft products such as Excel. However, the graph will not be a Microsoft graphic where you can modify. It will be a static image.

 

Can GTL code to be converted in SG code?

You can use a SGPLOT or SGSCATTER procedure to create GTL code by using the TMPLOUT= option. However, there is no option in PROC TEMPLATE code to create SG procedure code.

 

Can you tell me where to find template resources?

There are two sources of GTL documentation.

 

Can we export Excel data directly into the GTL code?

You can either import your Excel data into a SAS data set with the PROC IMPORT step or you can create a library to point to your Excel data with the LIBNAME statement. Then in the PROC SGRENDER, you refer to the appropriate data source. See examples below.

 

*Example 1;

proc import datafile='c:\temp\myExceldata.xlsx'

            out=work.mySASdata dbms=xlsx replace;

  sheet='Sheet1';

run;

proc template;

    define statgraph myscatter;

      begingraph;

        layout overlay;

          scatterplot x=Sales y=Profit;

        endlayout;

      endgraph;

    end;

run;

proc sgrender data=work.mySASdata

              template=myscatter;

run;

 

*Example 2;

libname myexcel xlsx 'c:\temp\myExceldata.xlsx';

 

proc template;

    define statgraph myscatter;

      begingraph;

        layout overlay;

          scatterplot x=Sales y=Profit;

        endlayout;

      endgraph;

    end;

run;

proc sgrender data=myexcel.Sheet1

              template=myscatter;

run;

 

PROC SGPLOT and PROC SGSCATTER work with TMPLOUT=, but SGPANEL had TMPLOUT= removed in 9.3?

Yes, you are correct. TMPLOUT= was removed from SGPANEL because there is processing performed by the procedure that is not included in the Graph Template Language code.

 

How can we add annotations to the figures (e.g., mean separation letters)? Can we do it with SG or we need to use GTL?

Annotation can be used with SG procedures and with Graph Template Language.

Below is a simple annotation example from the documentation.

data Line;

  infile datalines dlm='#';

  length label $ 27

         textcolor $ 9

         linecolor $ 9

         drawspace $ 9;

  retain drawspace "datavalue";

  input function $ x1 y1 label x2 y2 textcolor linecolor;

datalines;

text # 60 # 64 # Average Height 62 Inches # . # . # blue #  

line # 50 # 62 #   # 150 # 62 #   # blue

;

run;

 

proc sgplot data=sashelp.class sganno=Line tmplout='c:\temp\annexample.sas';

  scatter x=weight y=height;

run;

 

Below is an example of how the annotation looks in the PROC TEMPLATE step.

proc template;

define statgraph sgplot;

begingraph / collation=binary;

layout overlay / yaxisopts=(labelFitPolicy=Split) y2axisopts=(labelFitPolicy=Split);

   ScatterPlot X='Weight'n Y='Height'n / subpixel=off primary=true LegendLabel="Height" NAME="SCATTER";

   DrawText  TEXTATTRS=( COLOR=blue)  "Average Height 62 Inches" / X=60 Y=64 DRAWSPACE=datavalue;

   DrawLine X1=50 Y1=62 X2=150 Y2=62 / lineAttrs=( COLOR=blue) DRAWSPACE=datavalue;

endlayout;

endgraph;

end;

run;

 

How can we create pdf output from proc template?

To create a PDF document of your graph, use ODS PDF statements around the PROC SGRENDER step that follows the PROC TEMPLATE step. See example below.

 

proc template;

    define statgraph myscatter;

      begingraph;

        layout overlay;

          scatterplot x=Height y=Weight;

        endlayout;

      endgraph;

    end;

run;

 

ods pdf file="C:\temp\myreport.pdf";

proc sgrender data=sashelp.class

              template=myscatter;

run;

ods pdf close;

  

Recommended Resources

Graphically Speaking - SAS Blogs

Graphics Samples Output Gallery

Visualizing Data with SG Procedures 

Data Visualization Using SAS® ODS Graphics Course

 

Want more tips? Be sure to subscribe to the Ask the Expert board to receive follow up Q&A, slides and recordings from other SAS Ask the Expert webinars.  

Comments

I also submitted the following comment to the webinar Q&A:

  • There is another reason why GTL can be used instead of SG procedures: If similar graphs are going to be generated in multiple programs, the SG procedure code has to be included in every program (or at least in a macro called by each program). If GTL is used to generate a Graph Template, that template can be called by each program, and can be used to enforce graph standards to some extent too.

Another ODS Graphics resource:

Our ongoing hackathon project can make the use of MS Suite for Graphics also feasible with Viya environment. Stay tuned! 

Version history
Last update:
‎04-15-2022 08:04 AM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Article Labels
Article Tags