ods markup and tagsets: a tutorial
DESCRIPTION
This presentation by Eric Gebhart is an excellent tutorial on SAS ODS markup and tagsets which can be used for arranging SAS reporting output in most any format. Great for creating dashboards too!TRANSCRIPT
Copyright © 2004, SAS Institute Inc. All rights reserved.SAS is a registered trademark or trademark of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other brand and product names are registered trademarks or Trademarks of their respective companies
ODS Markup and Tagsets,A tutorial.Eric Gebhart
Copyright © 2003, SAS Institute Inc. All rights reserved. 2
Copyright © 2003, SAS Institute Inc. All rights reserved. 3
ODS MARKUP!
Copyright © 2003, SAS Institute Inc. All rights reserved. 4
ODS Markup & Tagsets
Copyright © 2003, SAS Institute Inc. All rights reserved. 5
Copyright © 2003, SAS Institute Inc. All rights reserved. 6
Proc Template.
It is NOT what you think it is!
Copyright © 2003, SAS Institute Inc. All rights reserved. 7
Tagsets are ODS Destinations
ODS Markup tagset=HTML4 ....
ODS Tagsets.HTML4 ....
ODS HTML4 ....
ODS HTML ....
Copyright © 2003, SAS Institute Inc. All rights reserved. 8
ODS HTML is ODS Markup!
Copyright © 2003, SAS Institute Inc. All rights reserved. 9
ods tagsets.short_map file = "body.xml" frame = "frame.xml" contents = "contents.xml" pages = "pages.xml" code = "code.xml" data = "data.xml" stylesheet = "stylesheet.xml";ods _all_ close;
The ODS Markup Statement
Copyright © 2003, SAS Institute Inc. All rights reserved. 10
Special, Named Destinations.
● HTML, PHTML, HTMLCSS, HTML4, CHTML● CSV, CSVByline, CSVAll● LaTeX, Troff● XML, SASReport, DocBook● IMode
Copyright © 2003, SAS Institute Inc. All rights reserved. 11
What is Different ?
One more output file.
Stylesheet Behavior.
More Options.
Copyright © 2003, SAS Institute Inc. All rights reserved. 12
The same options as before.
Ods html file='test.html' (headtext='<meta text=”my headtext”>', metatext='text=”my meta text”, url='http://www.sas.com/test.html' ) newfile=table;
Copyright © 2003, SAS Institute Inc. All rights reserved. 13
Multiple Stylesheet URL's!
ODS html file = ”test.html” stylesheet = ”test.css” (url=”test.css corp.css corpnav.css”);
Copyright © 2003, SAS Institute Inc. All rights reserved. 14
Multiple Personality Disorder.
Ods tagsets.myhtml file=”test.html” Alias=”NoNotes”;
Copyright © 2003, SAS Institute Inc. All rights reserved. 15
Make up your own Options!
ODS tagsets.test file=”test.txt” options(embedded_titles = 'yes' Orientation = 'landscape' Doc = 'Help');
Copyright © 2003, SAS Institute Inc. All rights reserved. 16
Options inside the tagset.
define tagset tagsets.test;
define event initialize; putvars $options _name_ ': ' _value_ nl; end; end;
Copyright © 2003, SAS Institute Inc. All rights reserved. 17
The tagset Output.
EMBEDDED_TITLES: yesORIENTATION: landscapeDOC: Help
Copyright © 2003, SAS Institute Inc. All rights reserved. 18
One more method of control!
ODS tagsets.htmlpanel file = ”test.html”;
ODS tagsets.htmlpanel event = panel(start);......ODS tagsets.htmlpanel event = panel(finish);
ODS tagsets.htmlpanel close;
Copyright © 2003, SAS Institute Inc. All rights reserved. 19
ODS Markup is like ODS HTML,
PLUS!
Copyright © 2003, SAS Institute Inc. All rights reserved. 20
Getting a listProc template;list tagsets;
Listing of: SASHELP.TMPLMST Path Filter is: Tagsets Sort by: PATH/ASCENDING
Obs Path Type 1 Tagsets Dir 2 Tagsets.Chtml Tagset 3 Tagsets.Colorlatex Tagset 4 Tagsets.Csv Tagset
Copyright © 2003, SAS Institute Inc. All rights reserved. 21
The HTML Family
●Phtml●HtmlCss●Html4●Chtml●Imode (chtml)
Copyright © 2003, SAS Institute Inc. All rights reserved. 22
PHTML, Plain HTML.
Simple 12 element Stylesheet
No class attributes.
No Fancy features.
Copyright © 2003, SAS Institute Inc. All rights reserved. 23
HTMLCSS.
Full Stylesheet
Fancy Features
Copyright © 2003, SAS Institute Inc. All rights reserved. 24
HTML4 is ODS HTML
Same as HtmlCSS
More like Previous Releases
Copyright © 2003, SAS Institute Inc. All rights reserved. 25
CHTML - Compact HTML.
iSNo
Subset of HTML.
No Styles.
Super Set of Imode.
Copyright © 2003, SAS Institute Inc. All rights reserved. 26
Data Transport and Excel.CSV, CSVByline, CSVAll
PYX
MSOffice2K
ExcelXP
DDE
http://support.sas.com/news/feature/04jul/excelsupport.html
Copyright © 2003, SAS Institute Inc. All rights reserved. 27
CSV
"Obs","Name","Sex","Age","Height","Weight""1","Alfred","M",14,69.0,112.5"2","Alice","F",13,56.5,84.0"3","Barbara","F",13,65.3,98.0
Copyright © 2003, SAS Institute Inc. All rights reserved. 28
PYX
(data-AlfredAtype "string"Ajust "l"Acolcount "1"Acol "2"Asection "body"Aclass "Data")data
Copyright © 2003, SAS Institute Inc. All rights reserved. 29
The ExcelXP Tagset!
Copyright © 2003, SAS Institute Inc. All rights reserved. 30
Publications, Papers, PDF.
LaTeX
Troff
DocBook
Copyright © 2003, SAS Institute Inc. All rights reserved. 31
LaTeX Tagsets
LaTeX
SimpleLaTeX
TablesOnlyLaTeX
Copyright © 2003, SAS Institute Inc. All rights reserved. 32
Combined Slide Bars - PDF via LaTeX
Copyright © 2003, SAS Institute Inc. All rights reserved. 33
Other Miscellaneous Tagsets.
ODSStyle
Style_Popup
Style_Display
NamedHTML
MVSHTML
Copyright © 2003, SAS Institute Inc. All rights reserved. 34
Copyright © 2003, SAS Institute Inc. All rights reserved. 35
Tagsets are Collections of Events
Copyright © 2003, SAS Institute Inc. All rights reserved. 36
Events Define Actions & Output.
define event doc; start: put HTMLDOCTYPE nl; put “<html>” nl; finish: put "</html>" nl; end;
Copyright © 2003, SAS Institute Inc. All rights reserved. 37
A very simple tagset.proc template; define tagset tagsets.plain;
define event data; put “Data: “ value nl; end;
define event header; put “Header: “ value nl; end;
end;run;
Copyright © 2003, SAS Institute Inc. All rights reserved. 38
Header: ObsHeader: NameHeader: SexHeader: AgeHeader: HeightHeader: WeightHeader: 1Data: AlfredData: MData: 14Data: 69.0Data: 112.5
ods tagsets.plain file="plain.txt";proc print data=sashelp.class;run;ods tagsets.plain close;
Copyright © 2003, SAS Institute Inc. All rights reserved. 39
Label, Value Pairs
Becomes this:
<Table background="grey" foreground="blue" cellpadding="5">
Putq '<Table' ' background=' background;putq 'foreground=' foreground;putq 'cellpadding=' cellpadding;put '>' nl;
With no attribute values, it becomes:
<Table>
Copyright © 2003, SAS Institute Inc. All rights reserved. 40
If Statements.
Put 'Foreground is blue!' nl /if cmp('blue', foreground);
Put 'Foreground is not red!' nl /if ^cmp('red', foreground);
Copyright © 2003, SAS Institute Inc. All rights reserved. 41
AND and OR, sort of...
Put 'Some variables have a value.' nl /if any(background, foreground, cellpadding, value);
Put 'All variables have a value.'' nl /if exists(background, foreground, cellpadding, value);
Copyright © 2003, SAS Institute Inc. All rights reserved. 42
Contains a Substring.
Set $myvar “this is random text”;
put “myvar has ran” nl /if contains($myvar, “ran”);
Copyright © 2003, SAS Institute Inc. All rights reserved. 43
Simple if test, Where clauses.
Set $myvar “this is random text”;Set $mytest “True”;
put “myvar has length” nl /if $myvar;
put “longer than 10” nl /if length($myvar) > 10
break /if $mytest;
Copyright © 2003, SAS Institute Inc. All rights reserved. 44
Break, Breakif
Break /if ^$options; Trigger alternate_event /if $do_something_else;Break /if $do_something_else; Trigger alternate_event /breakif $do_something_else;
Copyright © 2003, SAS Institute Inc. All rights reserved. 45
A list of images.proc template; define tagset tagsets.gifs;
define event branch; put label " "; end;
define event image; put url nl; end; end; run;
Copyright © 2003, SAS Institute Inc. All rights reserved. 46
The Output
Name=Alfred temp/gplot.gifName=Alice temp/gplot1.gifName=Barbara temp/gplot2.gif
Copyright © 2003, SAS Institute Inc. All rights reserved. 47
A Prettier Report.proc template; define tagset tagsets.gifs2;
define event branch; set $name scan(label, 2, '='); end;
define event image; put url " " $name nl; end; end; run;
Copyright © 2003, SAS Institute Inc. All rights reserved. 48
Nicer looking Output
temp/gplot.gif Alfredtemp/gplot1.gif Alicetemp/gplot2.gif Barbara
Copyright © 2003, SAS Institute Inc. All rights reserved. 49
The Set Statement.
set $myvar “foreground is” forground;
Always creates a string.
Variables always start with $.
Copyright © 2003, SAS Institute Inc. All rights reserved. 50
The Eval Statement.
Eval $count 0;
Eval $count $count + 1;
set $myvar “this is a test”;
Eval $index index($myvar, 'test');
Copyright © 2003, SAS Institute Inc. All rights reserved. 51
Data Step Functions.
Can be used anywhere.
Cannot be nested except in where clauses.
Copyright © 2003, SAS Institute Inc. All rights reserved. 52
Simple Needs, Simple Tagsets.
Copyright © 2003, SAS Institute Inc. All rights reserved. 53
SQL ???
Create table CLASS (Name varchar(7), Sex varchar(1), Age float, Height float, Weight float);
Insert into CLASS(Name, Sex, Age, Height, Weight) Values ('Alfred', 'M', 14, 69.0, 112.5); ... ...
Copyright © 2003, SAS Institute Inc. All rights reserved. 54
Dictionaries
define event type_translations; set $types['string'] 'varchar'; set $types['double'] 'float'; set $types['int'] 'integer'; end;
/* column name translation */ define event name_translations; set $name_trans['desc'] 'description'; end;
Copyright © 2003, SAS Institute Inc. All rights reserved. 55
The Initialize Event
define event initialize; trigger type_translations; trigger name_translations;
/* types that need widths */ set $types_with_widths['string'] "True";
/* types that need quotes */ set $types_with_quotes['string'] "True"; end;
Copyright © 2003, SAS Institute Inc. All rights reserved. 56
Creating Listsdefine event colspec_entry;
set $lowname lowcase(name);
do /if $name_trans[$lowname]; set $names[] $name_trans[$lowname]; else; set $names[] $lowname; done;
Copyright © 2003, SAS Institute Inc. All rights reserved. 57
define event table_body; put "Create table " $table_name "("; eval $i 1; unset $not_first; do /while $i <= $names; /* comma's only after the first name */ put ', ' /if $not_first; put $names[$i] " "; put $columns[$i]; eval $i $i+1; set $not_first "True"; done; put ");" nl; end;
Copyright © 2003, SAS Institute Inc. All rights reserved. 58
iterate $types do /while _name_; put _name_ “: “ _value_ nl; next $types; done;
Looping through A Dictionary
Copyright © 2003, SAS Institute Inc. All rights reserved. 59
XBRL, Data Interchange
Copyright © 2003, SAS Institute Inc. All rights reserved. 60
Better Stripes.
Copyright © 2003, SAS Institute Inc. All rights reserved. 61
Counting Rows
Copyright © 2003, SAS Institute Inc. All rights reserved. 62
Streams.
Designated by $$
Can hold large amounts of data
Not limited by memory
Can be opened, closed and flushed.
Copyright © 2003, SAS Institute Inc. All rights reserved. 63
Streams by example
open junk;put “This goes to junk.” nl;put “ More stuff.” nl;close;
open trash;put $$junk 'This goes to trash.';putstream junk; put “ This goes to trash too.”;
open junk;put “ Even more stuff”;close;
set $$junk “A completely new junk.”;set $$junk $$junk “ Appending to the end”;
unset $$junk
Copyright © 2003, SAS Institute Inc. All rights reserved. 64
Byline Modification
Copyright © 2003, SAS Institute Inc. All rights reserved. 65
Repeating Row Headers
Copyright © 2003, SAS Institute Inc. All rights reserved. 66
Simple Needs, Simple Tagsets.
Copyright © 2003, SAS Institute Inc. All rights reserved. 67
Slide Bars.
Copyright © 2003, SAS Institute Inc. All rights reserved. 68
Craeting Slide Bars.
proc print data=sashelp.class; var name; var sex; var age; var height / style(data) = slider[just=center tagattr="slider-80"]; var weight / style(data) = slider[just=center tagattr="slider-150"];run;
Copyright © 2003, SAS Institute Inc. All rights reserved. 69
WebSite Integration
Copyright © 2003, SAS Institute Inc. All rights reserved. 70
Sashelp.class, My Favorite.
Copyright © 2003, SAS Institute Inc. All rights reserved. 71
Copyright © 2003, SAS Institute Inc. All rights reserved. 72
Copyright © 2003, SAS Institute Inc. All rights reserved. 73
Copyright © 2003, SAS Institute Inc. All rights reserved. 74
Tagset Inheritance. Define tagset tagsets.SSV;
parent = tagsets.csv; define event header; start: put ';' / if ^cmp(COLSTART, "1"); put '"'; put VALUE; finish: put '"'; end;.....
Copyright © 2003, SAS Institute Inc. All rights reserved. 75
Tagset Attributes
Define tagset tagsets.mytags; map = '<>&'; mapsub = '/</>/&/'; nobreakspace = ' ' ; embedded_stylesheet = yes; output_type = html; lognote = “you are using my special tagset”;
Copyright © 2003, SAS Institute Inc. All rights reserved. 76
Using Options
define event options_set; trigger setup_options;end;
define event setup_options; trigger nls_numbers; trigger options_setup; trigger documentation; end;
ODS tagsets.excelxp options(embedded_titles='yes');
Copyright © 2003, SAS Institute Inc. All rights reserved. 77
Mvar embedded_titles;define event options_setup; set $options['test'] "test" /if ^$options; do /if $options['EMBEDDED_TITLES']; do /if cmp($options['EMBEDDED_TITLES'], "yes"); set $embedded_titles "true" ; else; unset $embedded_titles; done; else; do /if cmp(embedded_titles, "yes"); set $embedded_titles "true" ; else; unset $embedded_titles; done; done;
Copyright © 2003, SAS Institute Inc. All rights reserved. 78
Creating Help and documentation.
define event documentation; break /if ^$options; trigger quick_reference /if cmp($options['DOC'], 'quick'); trigger help /if cmp($options['DOC'], 'help'); end;
define event help; putlog "============================== “; putlog “The EXCELXP Tagset Help Text."; ... trigger quick_reference; end;
Copyright © 2003, SAS Institute Inc. All rights reserved. 79
Extended Procedural Controls.
Eval $count 10;do /while $count < 10; eval $count $count+1; stop /if $count = 9; continue /if $count = 3; put $count nl;else; put “Count was already > 10” nl;done;
Copyright © 2003, SAS Institute Inc. All rights reserved. 80
Else If
do /if $count < 10; put “count is < 10”; nl;else /if $count = 11; put “Count is11” nl;else /if $count = 12; put “Count is 12” nl;else; put “Count is > 12” nl;done;
Copyright © 2003, SAS Institute Inc. All rights reserved. 81
A process for Tagset Creation
● Define the goal● Identify an Event● Locate the Event Code● Explore the available variables.
Copyright © 2003, SAS Institute Inc. All rights reserved. 82
Identify and Locate.
Search the Output to be modified.Search the Tagset for the corresponding Event
Search the Output of the Event_Map Tagset.
Look for convienent events with short_map.
Copyright © 2003, SAS Institute Inc. All rights reserved. 83
Finding & Exploring with Event Map.
ods tagsets.event_map file="map.xml";
<image event_name="image" trigger_name="attr_out" output_name="Gplot" output_label="Plot of Weight * Height" index="IDX" just="c" url="gplot.gif" />
Copyright © 2003, SAS Institute Inc. All rights reserved. 84
Exploring Variables with Putvars.
Put “Event variables” nl;Putvars Event _name_ ': ' _value_ nl;
Put “Style variables” nl;Putvars Style _name_ ': ' _value_ nl;
Put “Options” nl;Putvars $Options _name_ ': ' _value_ nl;
Copyright © 2003, SAS Institute Inc. All rights reserved. 85
Finding Other Useful Events.
Default_Event = putall;
Define event putall; put '<' Event_name nl; Putvars Event _name_ ': ' _value_ nl; Putvars Style _name_ ': ' _value_ nl; Putvars Mem _name_ ': ' _value_ nl; put '>' Event_name nl;End;
Copyright © 2003, SAS Institute Inc. All rights reserved. 86
Using a PutLog event.
Define event putlog; putlog “Event:” event_name nl; Putlog “Name is” “:” name; Putlog “Count is” “:” $count;end;
Copyright © 2003, SAS Institute Inc. All rights reserved. 87
Locating Alfred
<branch event_name="branch" trigger_name="attr_out" class="ByContentFolder" value="Name=Alfred" name="ByGroup1" label="Name=Alfred" index="IDX" just="c" url="junk.xml#IDX" hreftarget="body">
Copyright © 2003, SAS Institute Inc. All rights reserved. 88
A Solution.proc template; define tagset tagsets.gifs2;
define event branch; set $name scan(label, 2, '='); end;
define event image; put url " " $name nl; end; end;run;
Copyright © 2003, SAS Institute Inc. All rights reserved. 89
Copyright © 2003, SAS Institute Inc. All rights reserved. 90
Useful Links
ODS Markup, New Tagsets, Howto's.
http://support.sas.com/rnd/base/topics/odsmarkup/
Introduction to Tagsets.
http://support.sas.com/rnd/base/topics/odsmarkup/tagsets.html
The Template FAQ.
http://support.sas.com/rnd/base/topics/templateFAQ/Template.html