As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Funny ^Stuff~ in My Code: Using ODS ESCAPECHAR

From sasCommunity
Jump to: navigation, search
Cynthia L. Zender

SAS Institute, Inc.

Cary, NC

Abstract

Have you ever wanted to insert a superscript or subscript into your SAS® output? Or, have you ever needed to make one part of a text string bold or a different color? Do you wonder how to get RTF or PDF output to have Page X of Y page numbering in a footnote?

This paper introduces you to the wonders of the ODS ESCAPECHAR statement (ODS ESCAPECHAR=). Discussed is how to define and then use a specific escape character to perform in-line formatting. Concrete examples show you how to change style within one table cell (or a SAS title or footnote), how to insert Page X of Y page numbers into your output, how to insert line feeds into long text strings, and how to insert destination-specific "raw text" into your HTML or RTF output. Learn how to use ODS ESCAPECHAR= and ESCAPECHAR control strings effectively and make the funny characters work for you.

A short glossary of the terms used in this paper appears at the end of the paper.

SAS Code

PROGRAM: INLINE1.SAS

data use_esc;
  length var1 $200;
  var1 = "The quick brown fox";
  output;
  var1 = "The ^S={font_weight=bold}quick ^S={} brown fox";
  output;
  var1 = "The quick ^S={foreground=brown}brown ^S={}fox";
  output;
  var1 = "The quick brown ^S={font_face='Courier New'}fox ^S={}";
  output;
  var1 = "The ^S={font_weight=bold}quick ^S={}" ||
         "^S={foreground=brown}brown ^S={}"||
         "^S={font_face='Courier New'}fox ^S={}";
  output;
run;

ods html file='inline1.html' style=sasweb;
ods rtf file='inline1.rtf' notoc_data;
ods pdf file='inline1.pdf';
ods escapechar='^';

  proc print data=use_esc;
  run;

ods _all_ close;

PROGRAM: INLINE2.SAS

data sup_sub;
  length myvar $200;
  myvar = "Pythagorean Theorem: a^{super 2} + b^{super 2} = c^{super 2}";
  output;
  myvar = "This is something that needs a footnote. ^{super 1}";
  output;
  myvar = "Macbeth: 'Is this a dagger I see before me?' ^{dagger}";
  output;
  myvar = "The Caffeine molecule is an alkaloid of the methylxanthine family: " ||
          "C^{sub 8}H^{sub 10}N^{sub 4}O^{sub 2}";
  output;
run;

ods html file='inline2.html' style=sasweb;
ods rtf file='inline2.rtf' notoc_data;
ods pdf file='inline2.pdf';
ods escapechar='^';

 proc print data=sup_sub;
 title j=r 'PDF & RTF: Page ^{thispage} of ^{lastpage}';
 title2 j=c 'RTF only: ^{pageof}';
 footnote '^{super 1}If this were a real footnote, there would be something very academic here.';
 footnote2 '^{dagger} Macbeth talked to himself a lot. This quote is from Macbeth: Act 2, Scene 1, Lines 33-39.';
 run;
ods _all_ close;

PROGRAM: INLINE3.SAS

data linebr;
  length myvar $200;
  myvar ="The Caffeine molecule is an alkaloid of the methylxanthine family: ~n" ||
         " C~{sub 8}H~{sub 10}N~{sub 4}O~{sub 2}";
  output;
  myvar ="The Caffeine molecule is an alkaloid of the methylxanthine family: ~2n" ||
         " C~{sub 8}H~{sub 10}N~{sub 4}O~{sub 2}";
  output;
  myvar ="The Caffeine molecule is an alkaloid of the methylxanthine family: ~3n" ||
         " C~{sub 8}H~{sub 10}N~{sub 4}O~{sub 2}";
  output;
  myvar ="The Caffeine molecule is an alkaloid of the methylxanthine family: ~4n" ||
         " C~{sub 8}H~{sub 10}N~{sub 4}O~{sub 2}";
  output;
run;

ods listing;
ods html file='inline3.html' style=sasweb;
ods rtf file='inline3.rtf' notoc_data;
ods pdf file='inline3.pdf';
ods escapechar='~';

 proc print data=linebr;
   title 'Using the ODS ESCAPECHAR for line break';
   title2 'Title 2';
   title3 'Title 3';
   title4 'Title 4';
   title5 'Title 5';
   title6 'Title 6';
   title7 'Title 7';
   title8 'Title 8';
   title9 'Title 9';
   title10 'Title 10 ~n Title 11 ~n Title 12 ~n Title 13 ~n Title 14';
 run;

ods _all_ close;

PROGRAM: INLINE4.SAS

data linebr_raw;
  length myvar $200;
  myvar ="The Caffeine molecule is an alkaloid of the methylxanthine family: " ||
         "#R/HTML'
' #R/RTF'\line'" || " C#{sub 8}H#{sub 10}N#{sub 4}O#{sub 2}"; output; myvar ="The Caffeine molecule is an alkaloid of the methylxanthine family: " || "#R/HTML'

' #R/RTF'\line \line'" || " C#{sub 8}H#{sub 10}N#{sub 4}O#{sub 2}"; output; myvar ="Start of Text" || "#R/HTML'    ' #R/RTF'\tab' " || "Rest of Text"; output; run; options nodate nonumber linesize=200; ods listing; ods html file='inline4.html' style=sasweb; ods rtf file='inline4.rtf' notoc_data; ods escapechar='#'; proc print data=linebr_raw; title 'Using the ODS ESCAPECHAR and Raw Text for line break'; run; ods _all_ close;

PROGRAM: INLINE5.SAS

data _null_;
  hexcode = 'AE'x;
  call symput ('hexreg',hexcode);
  hexcode1 = 'A9'x;
  call symput('hexcopy' , hexcode1);
  hexcode2 = 'A2'x;
  call symput('hexcent', hexcode2);
run;
** Display the hex codes in the SAS log;
%put hexreg= &hexreg;
%put hexcopy= &hexcopy;
%put hexcent= &hexcent;

ods html file='inline5.html' style=sasweb;
ods rtf file='inline5.rtf' notoc_data startpage=no;
ods pdf file='inline5.pdf' startpage=no;
ods escapechar='^';
ods noptitle;

  proc means data=sashelp.shoes sum min mean max;
    class Region;
    var Sales;
    label Sales='^S={font_style=italic}^nShoe Sales';
  run;

ods pdf text="^S={just=c font_size=18pt}PDF File^nOpens with Adobe Reader^n&hexreg &hexcopy My 2&hexcent";
ods rtf text="^S={just=c font_size=18pt}RTF File^nOpens with Microsoft Word^n&hexreg &hexcopy My 2&hexcent";
ods html text="^S={just=c font_size=18pt}HTML File^nOpens with Microsoft Internet Explorer^n &hexreg &hexcopy My 2&hexcent";

proc freq data=sashelp.shoes;
  tables Product /nocum;
  label Product='^S={font_style=italic}Shoe Types Sold';
run;

ods _all_ close;

PROGRAM: INLINE6.SAS

proc format;
  value $prdimg 'Boot' = 'c:\temp\boot.jpg'
                "Women's Dress" = 'c:\temp\dressheels.jpg'
                'Slipper' = 'c:\temp\slipper.jpg';
  run;

ods html file='inline6.html' style=sasweb;
ods rtf file='inline6.rtf' notoc_data;
ods pdf file='inline6.pdf';
ods escapechar='^';

  proc report data=sashelp.shoes nowd
    style(column) = {vjust=b};
    title '^S={preimage="c:\temp\shoe_logo.jpg"}';
    where product in ('Boot', 'Slipper', "Women's Dress");
    column Product Sales N;
    define Product / group
           style(header)={background=white foreground=black}
           style(column)={postimage=$prdimg.};
    define Sales / sum;
    define N / '^S={background=white foreground=black}Number of Sales';
  run;

ods _all_ close;

Online resources

Download the .pdf of this paper.

See also