BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SAS_Question
Quartz | Level 8
/* HAVE */
proc format;
   value myfmt 12='12';
   value mycol 12='Yellow';
run;

data have(keep=Make Model Cylinders);
	set sashelp.cars; 
	if make in ('Audi','BMW','Volkswagen','Mercedes-Benz') then output;
run;

proc sort data=have;
	by Make Model;
run;

PROC TABULATE DATA=WORK.HAVE;
	CLASS Cylinders Make Model;
	classlev Cylinders / s={background=mycol.};
	keyword N / s=< parent >;
    format Cylinders myfmt.;
	keylabel all="Total";
	TABLE 
		(Make * (Model all = 'Total' )	all = 'Total' ),
		Cylinders all = 'Total' ;
RUN;

/* WANT */

WANT the column like this...WANT the column like this...

 

 

I Want the whole last column to be in yellow, not only the headers...

How do I do that?

Please see the picture. I want it to be like that... but i'm stuck! 

Thanks for the help or tips!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

OK. I think you need to change the type of variable "Cylinders" to avoid the conflict with keyword "N" .

And try this one:

 

data have(keep=Make Model Cylinders);
 set sashelp.cars;
  if make in ('Mercedes-Benz') then output;
run;
proc sql;
    insert into work.have
        values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
        values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
        values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
  values("Mercedes-Benz", "E501", 8);
quit;
proc sort data=have;
 by Make Model;
run;



proc format;
   value $ mycol '12'='Yellow';
run;
data have;
 set have;
 _Cylinders=put(Cylinders,best. -l);
 drop Cylinders;
run;

PROC TABULATE DATA=WORK.HAVE order=data;
 CLASS _Cylinders Make Model;
 classlev _Cylinders / s={background=$mycol.} ;
 keyword N / s=< parent >;
 keylabel all="Total";
 TABLE 
  (Make * (Model all = 'Total' ) all = 'Total' ),
  _Cylinders*n*{style=<parent>};
RUN;

Ksharp_0-1657540013551.png

 

View solution in original post

7 REPLIES 7
Ksharp
Super User
/* HAVE */
proc format;
   value myfmt 12='12';
   value mycol 12='Yellow';
run;

data have(keep=Make Model Cylinders);
	set sashelp.cars; 
	if make in ('Audi','BMW','Volkswagen','Mercedes-Benz') then output;
run;

proc sort data=have;
	by Make Model;
run;

PROC TABULATE DATA=WORK.HAVE;
	CLASS Cylinders Make Model;
	classlev Cylinders / s={background=mycol.};
	keyword N / s=< parent >;
    format Cylinders myfmt.;
	keylabel all="Total";
	TABLE 
		(Make * (Model all = 'Total' )	all = 'Total' ),
		Cylinders*n*{style=<parent>} all = 'Total' ;
RUN;

Ksharp_0-1657451243395.png

 

SAS_Question
Quartz | Level 8

Thanks @Ksharp!
We are almost there... 🙂
Now if I select only Mercedes, like in the code below, and it (the N?) has also a value with '12' in another column,

than that field in the other column will get the yellow color also ... how do I supress that? 

Look at mij output, to see what I mean:

 

proc format;
   value myfmt 12=12;
   value mycol 12='Yellow';
run;

data have(keep=Make Model Cylinders);
	set sashelp.cars; 
	if make in ('Mercedes-Benz') then output;
run;

proc sort data=have;
	by Make Model;
run;

PROC TABULATE DATA=WORK.HAVE;
	CLASS Cylinders Make Model;
	classlev Cylinders / s={background=mycol.};
	keyword N / s=< parent >;
    format Cylinders myfmt.;
	keylabel all="Total";
	TABLE 
		(Make * (Model all = 'Total' )	all = 'Total' ),
		Cylinders*n*{style=<parent>};
RUN;

Will produce: 

 WANT2.PNG

Ksharp
Super User

OK. You want this ?

 

proc format;
   value mycol 12='Yellow';
   value fmt low-high='white';
run;

data have(keep=Make Model Cylinders);
	set sashelp.cars; 
	if make in ('Mercedes-Benz') then output;
run;

proc sort data=have;
	by Make Model;
run;

PROC TABULATE DATA=WORK.HAVE style=header;
	CLASS Cylinders Make Model;
	classlev Cylinders / s={background=mycol.};
	keyword N / s=< parent >;
	keylabel all="Total";
	TABLE 
		(Make * (Model all ={Label='Total' style={BACKGROUND=lightred}} * {STYLE={BACKGROUND=lightred}} )	
ALL={Label='Total' style={BACKGROUND=lightred}} * {STYLE={BACKGROUND=lightred}} ),
		Cylinders*n*{style=<parent>}/style_precedence=row;
RUN;

Ksharp_0-1657457824742.png

 

SAS_Question
Quartz | Level 8

That cheating 😉

It works good as long as the data remains as is... right? 

Suppose the data becomes this: 

 


proc format;
   value mycol 12='Yellow';
run;

data have(keep=Make Model Cylinders);
	set sashelp.cars;
 	if make in ('Mercedes-Benz') then output;
run;

proc sql;
    insert into work.have
        values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
        values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
        values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
	    values("Mercedes-Benz", "E501", 8)
		values("Mercedes-Benz", "E501", 8);
quit;

proc sort data=have;
	by Make Model;
run;

Then your code won't work. Look: 

PROC TABULATE DATA=WORK.HAVE style=header;
	CLASS Cylinders Make Model;
	classlev Cylinders / s={background=mycol.};
	keyword N / s=< parent >;
	keylabel all="Total";
	TABLE 
		(Make * (Model all ={Label='Total' style={BACKGROUND=lightred}} * {STYLE={BACKGROUND=lightred}} )	
ALL={Label='Total' style={BACKGROUND=lightred}} * {STYLE={BACKGROUND=lightred}} ),
		Cylinders*n*{style=<parent>}/style_precedence=row;
RUN;

Will now give: 

SAS_Question_0-1657460272216.png

😞 

ballardw
Super User

@SAS_Question wrote:

That cheating 😉

It works good as long as the data remains as is... right? 

 


Actually I would say your question is "cheating". You have not provided a general RULE but single example values. So anything other than exactly your example values does not a good chance at a clean general solution.

 

One problem is assigning a format to general statistic, n. That can occur in multiple places so is likely not a good "solution" except to specific data.

 

If you want an extremely specific control on the appearance you may have to summarize the data and use a different approach to creating the output such as the Report Writing Interface in a data step that allows all sorts of conditional statements to set styles based on variable/value combinations.

 

SAS_Question
Quartz | Level 8

You are right @ballardw ! 

I thought there was something for the proc tabulate that I might have missed, because of my struggle with this issue. 

But even if it is a very wonderful tool (the proc tabulate) it lacks something (like coloring the way YOU want it, not the way the data wants it).

So maybe I should do a Proc Report research for this job..

If you have any tips where to start, it might help! 

Thanks anyways! 

 

Ksharp
Super User

OK. I think you need to change the type of variable "Cylinders" to avoid the conflict with keyword "N" .

And try this one:

 

data have(keep=Make Model Cylinders);
 set sashelp.cars;
  if make in ('Mercedes-Benz') then output;
run;
proc sql;
    insert into work.have
        values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
        values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
        values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
     values("Mercedes-Benz", "E501", 8)
  values("Mercedes-Benz", "E501", 8);
quit;
proc sort data=have;
 by Make Model;
run;



proc format;
   value $ mycol '12'='Yellow';
run;
data have;
 set have;
 _Cylinders=put(Cylinders,best. -l);
 drop Cylinders;
run;

PROC TABULATE DATA=WORK.HAVE order=data;
 CLASS _Cylinders Make Model;
 classlev _Cylinders / s={background=$mycol.} ;
 keyword N / s=< parent >;
 keylabel all="Total";
 TABLE 
  (Make * (Model all = 'Total' ) all = 'Total' ),
  _Cylinders*n*{style=<parent>};
RUN;

Ksharp_0-1657540013551.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 709 views
  • 5 likes
  • 3 in conversation