BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello,

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.

Michael
7 REPLIES 7
David_SAS
SAS Employee
Michael,

I'm not aware of a way to do what you want.

-- David Kelley, SAS
Cynthia_sas
SAS Super FREQ
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.

cynthia
[pre]
*** code;
data putspace;
length charvar1 charvar2 $30;
charvar1 = ' Big Space';
charvar2 = ' Little Space';
numvar1 = 12343.56;
numvar2 = 987.65;
output;
output;
output;
run;

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;
run;

proc report data=putspace nowd;
title 'use _character_';
column charvar1 charvar2 numvar1 numvar2;
define _character_ /
style(column)={nobreakspace=on};
define charvar1 / order;
define charvar2 / order;
define numvar1 /sum;
define numvar2 /sum;
run;

ods html close;
[/pre]
Cynthia_sas
SAS Super FREQ
Michael:
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   -- 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.
cynthia
[pre]
ods path work.custom(update)
sashelp.tmplmst(read);
**;
** create a char var with spaces in the value;
data newclass;
length newname $70 name $15;
set sashelp.class;
newname = trim(name)||' Wombat';
if sex = 'M' then name = trim(name)||' '||'Bob';
else name = trim(name)||' '||'Ann';
run;
**;
** now define a custom tagset to test whether;
** the variable is a character variable;
** type="string";
** 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;
proc template;
define tagset tagsets.char_nbsp;
parent=tagsets.html4;

define event table ;
start:
put '' nl;
finish:
put '
' nl;
end;

define event row;
start:
putq '' nl;
finish:
put '' nl;
end;

define event data;
start:
put ' trigger classalign;
put '>';
do /if cmp(type, 'string');
put tranwrd(value, ' ', ' ');
else;
put value;
done;
finish:
put '' nl;
end;

end;
run;

** 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;
run;
ods _all_ close;
[/pre]
Eric_SAS
SAS Employee
Note that the tranwrd() got mangled by the html rendering....

Lets see if I can get this to work... Maybe some pre tags. although
preview doesn't quite work...

That should be:


put tranwrd(value, ' ', ' ');
Eric_SAS
SAS Employee
Nope. still didn't go.

that second space in the tranwrd should be

 
Cynthia_sas
SAS Super FREQ
Thanks for catching that, Eric. That's a problem posting code with HTML entities. I wonder if I could have &'d the & before nbsp; . I'll have to test it here:


 

hmph...very hard to do.
deleted_user
Not applicable
Thank you all for the replies. It should make my code look a bit cleaner.

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

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.

Discussion stats
  • 7 replies
  • 1903 views
  • 0 likes
  • 4 in conversation