BookmarkSubscribeRSS Feed

Graph Template Language Example Code - Save your time and effort

Started ‎03-12-2021 by
Modified ‎03-12-2021 by
Views 4,464

If you are asking yourself, at this point, “what is Graph Template Language, I have never heard of it ?” this might be a good place to start…..


A few years ago, on a bit of whim, I bought a copy of Getting Started with the Graph Template Language in SAS® by Sanjay Matange (ISBN: 9781612907123)


and promised myself that I would learn what this Graph Template Language (GTL) was, and what it might do for me within SAS.


Time went by, as it does, and I promised myself that I really would get down to it, just as soon as I had some spare time…………


Eventually, like a lot of people, 2020 offered me that spare time. I started working through the book and enjoyed putting together the PROC TEMPLATE steps displayed in the book and trying them out with the supplied datasets through PROC SGRENDER.


Now I will have to tell you, up front, and before you get too excited, this book is now out of print, but I notice there are copies lurking around on various book sites or on colleague's bookshelves.


The book is a truly excellent introduction to the Graph Template Language, I now know far more than I ever wanted to know !


Like all good SAS books there was plenty of SAS code and diagrams displaying the relevant output.


However, due to the feature rich nature of PROC TEMPLATE and the constraints of a small format book, I really needed to re-format the code in my own way so that I could see what was going on more easily in order to explain GTL to myself and others. Additionally, I wanted to play about with the sample code in the book to check my understanding. As I come from an era of de-bugging code where I would study a program listing printed on lineflow paper, with a pencil in one hand and a ruler in the other, my preferred code layout tends to be vertical rather than trying to fit loads of stuff all on one line. Not everyone may agree with this technique.


As a result, I ended up keying in all of the examples contained in the book into the attached single SAS program, using my own way of laying out code. I also tried to finally come to grips with some of the bits about the Output Delivery System (ODS) that I had managed to run away from over the years.


As this was a LOT of work, I thought it might be nice to share it with the SAS Community so that others could focus on the Graph Template Language rather than typing in the code.


Consequently I have attached the single SAS program for anyone to use, but, with the following caveat:
This version of the code is offered free-of-charge with absolutely no warranties, you use it at your own risk. Where the code deviates from that shown in the book, it is usually to highlight something that I thought was useful, to myself, as I was working through the original code.




This code was tested on SAS 9.4 Maintenance Release 6 on SAS Enterprise Guide 8.3 Update 1 on Microsoft Windows.


The SAS code to generate the sample datasets used by several of the examples can be downloaded from: 


A Quick Note about storing Templates in Item Stores

I will freely admit when I started down this path, I had trouble getting my head around Item Stores, especially as things would work but I did not know why. As a result, I almost always create practice code like this, with a statement to force Templates into a specific, temporary Item Store, using these statements somewhere at the top of my job to confirm where SAS will be storing my Templates:

ODS PATH(PREPEND) WORK.Daves_Custom_Templates1(UPDATE);

Then, during each PROC TEMPLATE, I have added an explicit instruction as to what the name of the Item Store will be that is used to store the template. The following example creates a Statgraph item, the template, called Fig_8_4_4, in the Daves_Custom_Templates1 item store which is stored in the WORK library:

Define StatGraph Fig_8_4_4 /   STORE=WORK.Daves_Custom_Templates1;


A Quick Note about ODS statements:
I constructed this piece of code so that individual PROC TEMPLATE/PROC SGRENDER examples could be run without having to run all of the code.


In the book, Sanjay uses the ODS LISTING destination throughout most of the book. I have added a pair of ODS statements immediately preceding PROC SGRENDER step, as follows:

ODS LISTING GPATH="&GraphicsPath";
  • The first statement specifies a directory for the default .PNG file to be written into, in this case via a user-defined macro variable.
  • The second statement specifies a name for the .PNG file, in this example the file will be called Fig_8_4_4.png
  • Although this recurring pair of statements looks tedious, it has helped me, feel free to do your own thing.

When I run this code in SAS Enterprise Guide, I turn OFF all ODS destinations like this:



….…and let ODS statements in the code do the heavy lifting. Once more, feel free to do your own thing.


I hope that you get to learn some new stuff about the Graph Template Language by using the attached code along with Sanjay's book and his descriptions about what is going on.


Cheers and Good Luck,


Downunder Dave

New Zealand


Thanks for the codes and welcome to the field of ods,  How long did it take you to compile the codes,  i have never seen such a long compilation of codes.



You are the first person to comment on my SAS Communities Library posting, thank you very much for that.


It took me a few months of on-again, off-again work, in the evenings, to get the SAS code copied from the book, formatted how I wanted it and, of course, tested 😓.


As I said in the original posting, the idea was originally just to help me work through Sanjay Matange's excellent book. It then sort of expanded into this SAS Communities Library posting. I am glad it has come to the attention of at least a few other SAS users, please spread the word !




Downunder Dave

New Zealand

Most welcomed DaveShea , sure will spread the gospel about GTL, I have a
passion in SAS GTL, whenever you get something interesting, kindly let me
know via my mail
Version history
Last update:
‎03-12-2021 04:20 PM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started