I was wondering if there is a way to code coditional formatting into a style template. I would like all numeric columns of PROC REPORT to have NOBREAKSPACE=OFF, but all character columns to have NOBREAKSPACE=ON. I'd like to avoid having to add a style element to each DEFINE statement for one or the other if I can.
Here are a few thoughts. By default, NOBREAKSPACE is set to OFF. So you only have to worry about turning it ON for the character variables.
Inside PROC REPORT, the use of _CHARACTER_ will work for you. In this program, the first PROC REPORT step shows that NOBREAKSPACE is OFF for charvar1 and charvar2 (and by default for the numeric variables, too).
It is an extra define statement in your code, but it could do what you want.
The only other thing I can think of depends on if you need HTML output only. I think you could make a custom style element in a custom style template and then make a custom tagset template to test the type of the variable and then apply the style attribute selectively. I thought there was an example of this on the ODS MARKUP site, but I can't find it right now. I will post again with either the URL or the example.
length charvar1 charvar2 $30;
charvar1 = ' Big Space';
charvar2 = ' Little Space';
numvar1 = 12343.56;
numvar2 = 987.65;
ods listing close;
ods html file='c:\temp\testit.html';
proc report data=putspace nowd;
title 'default is for nobreakspace=off';
column charvar1 charvar2 numvar1 numvar2;
define charvar1 / order;
define charvar2 / order;
define numvar1 /sum;
define numvar2 /sum;
I could not find a link to the example I was thinking of, but here's the code to do what you want. It is a custom tagset template, so it will only work to generate HTML tags. What is happening is that the tagset is testing the variable as ODS sends it to see if the type="string" and if that test is true, then the spaces are being converted to the HTML entity &NBSP; -- which has the same effect as turning nobreakspace=on for character variables. Since the default for nobreakspace is OFF, this means that your numeric variables would still use this default. Here's the code.
ods path work.custom(update)
** create a char var with spaces in the value;
length newname $70 name $15;
newname = trim(name)||' Wombat';
if sex = 'M' then name = trim(name)||' '||'Bob';
else name = trim(name)||' '||'Ann';
** now define a custom tagset to test whether;
** the variable is a character variable;
** and if so, use the tranwrd function to turn all;
** occurrences of space char into entity;
** which has the same -effect- as nobreakspace=on;
** the testing takes place in the tagset before;
** the value is written to the HTML file;
define tagset tagsets.char_nbsp;
define event data;
do /if cmp(type, 'string');
put tranwrd(value, ' ', ' ');
put '' nl;
** use the new tagset;
ods tagsets.char_nbsp file="forum_nbsp.html";
proc print data=newclass;
title 'use HTML custom tagset to turn spaces into ';
title2 'do a view --> source in browser to see conversion in data values';
var name newname age height weight;
ods _all_ close;