BookmarkSubscribeRSS Feed
twildone
Pyrite | Level 9

Hi,

I would like to be able to create a table that alternates the row color to distinguish between cities. However, the variable cities is not unique as it is possible for the city to appear in the table more than once. I would like to keep and have each different city the same color and alternating colors between different cities......any suggestions would be greatly appreciated...thanks.

9 REPLIES 9
ballardw
Super User

What procedure are you using to generate your output?

In some procedures you can provide style overrides for background color using a format based on the value displayed.

twildone
Pyrite | Level 9

Hi Ballardw,

I am using the proc report procedure....and would like it to alternating between white and grey for each different city so as to identify the results for each city .

CityDepartmentEmployees
VancouverEngineering23
EdmontonAdministration14
EdmontonPublic Transportation34
CalgaryEngineering27
SaskatoonAdministration20
SaskatoonPublic Transportation13
SaskatoonEngineering21
ReginaAdministration9
ReginaPublic Transportation13
WinnipegEngineering18
Reeza
Super User

How many cities do you have? Do you want to control the color of the cities or have them automatically selected, which could be interesting if you have a lot of cities.

I'm a bit confused with the alternating colour yet each city the same color? Do you want 2 colors that alternate, but this is done for multiple rows at a time or should each city have its own color?

twildone
Pyrite | Level 9

Hi Reeza,

The number of cities can vary from time to time depending when the program is ran and the time of year that the data is being extracted. I am not too concern about the colors (white and grey is fine) as long as it is possible to distinguish the result on the table between cities.

CityDepartmentEmployees
VancouverEngineering23
EdmontonAdministration14
EdmontonPublic Transportation34
CalgaryEngineering27
SaskatoonAdministration20
SaskatoonPublic Transportation13
SaskatoonEngineering21
ReginaAdministration9
ReginaPublic Transportation13
WinnipegEngineering18
test999999
Calcite | Level 5

hi,

this can be done in proc report, by using a line number and checking if it is even or odd.

something along the lines of

proc report.....

      compute LINE_COLOR;

        if mod(LINE_COLOR, 2) gt 0 then

          call define(_ROW_,'STYLE','style=[background=LIGHTGRAY]');

      endcomp;

run;

snoopy369
Barite | Level 11

This is the basic concept, but then you'd want to change this from instead of incrementing every single row, increment only when you have a change in city.  Perhaps just store the last-seen city name and alter the color when it's not the same as the current one.

Scott_C_Moore
Calcite | Level 5
data colors;
set sashelp.shoes;
where Subsidiary in('Addis Ababa' 'Algiers' 'Cairo' 'Khartoum');
run;

ods html;
proc report data=colors nowd;
column subsidiary sales;
define subsidiary / display;
define sales / display;

compute subsidiary;
if subsidiary='Addis Ababa' then
    call define(_row_,
"style","style={background=green}");
else if subsidiary='Algiers' then
    call define(_row_,
"style","style={background=blue}");
else if subsidiary='Cairo' then
    call define(_row_,
"style","style={background=red}");
else if subsidiary='Khartoum' then
    call define(_row_,
"style","style={background=orange}");
endcomp;
where sales gt 70000;
run;
ods html close;
snoopy369
Barite | Level 11

Modified to simply alternate:

data colors;

set sashelp.shoes;

where Subsidiary in('Addis Ababa' 'Algiers' 'Cairo' 'Khartoum');

run;

ods html;

proc report data=colors nowd;

column  subsidiary sales;

define subsidiary / order;

define sales / display;

compute before subsidiary;

  colorcol+1;

endcomp;

compute subsidiary;

  if mod(colorcol,2)=1 then

  call define(_row_,"style","style={background=green}");

  else

  call define(_row_,"style","style={background=blue}");

endcomp;

where sales gt 70000;

run;

ods html close;

You can also do something like this in PROC TABULATE; see http://support.sas.com/resources/papers/stylesinprocs.pdf for examples of using CLASSLEV and formats to modify row colors.

twildone
Pyrite | Level 9

Hi Scott...thanks for your suggestion. The problem is that I do not know in advance how many cities will be in the table and for the most part, it would seem like it could be numerous cities....so specifying a color for a particular city would not work for me but I am fine with it alternating between white and grey....Thanks once again/

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

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
  • 9 replies
  • 4618 views
  • 4 likes
  • 6 in conversation