BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nesslee
Fluorite | Level 6

Hello,

I would like to alternate 2 colors in proc report for each new value in a column (row).

row

20

20

20

22

23

23

24

 

so all rows with value=20 will have lightgray color

row with value=22 will have lightgreen color

all rows with value=23 will have lightgrey color

and so on.

The values are not fixed. So I guess i have to first create a variable as compute variable (flag).

Can you please help ?

ods listing close;

proc report data=dupl nowd;

compute flag;

if flag=0 then call define(_row_, "style", "style=[background=lightgray]");

if flag=1 then call define(_row_, "style", "style=[background=lightgreen]");

endcomp;

run;

ods excel close;

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi  @nesslee 

Here is a way to do this -> the idea is to compute a flag that changes each time there is a new value.
Depending on whether the flag is an odd or even number (cf. mod() function), the color changes.

Best,

data dupl;
	input value;
	datalines;
20
20
20
22
23
23
24
	;
run;

data dupl2;
	set dupl;
	by value notsorted;
	if first.flag then flag=0;
	_lag = lag(value);
	if _lag ne value then flag+1;
run;

proc report data=dupl2 nowd;
	
	column value flag;
	
	define value / display;
	define flag / display noprint ;
	
	compute flag;
		if mod(flag,2)=1 then call define(_row_, "style", "style=[background=lightgray]");
		else call define(_row_, "style", "style=[background=lightgreen]");
	endcomp;
run;

Capture d’écran 2020-05-27 à 17.04.46.png

View solution in original post

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

Hi  @nesslee 

Here is a way to do this -> the idea is to compute a flag that changes each time there is a new value.
Depending on whether the flag is an odd or even number (cf. mod() function), the color changes.

Best,

data dupl;
	input value;
	datalines;
20
20
20
22
23
23
24
	;
run;

data dupl2;
	set dupl;
	by value notsorted;
	if first.flag then flag=0;
	_lag = lag(value);
	if _lag ne value then flag+1;
run;

proc report data=dupl2 nowd;
	
	column value flag;
	
	define value / display;
	define flag / display noprint ;
	
	compute flag;
		if mod(flag,2)=1 then call define(_row_, "style", "style=[background=lightgray]");
		else call define(_row_, "style", "style=[background=lightgreen]");
	endcomp;
run;

Capture d’écran 2020-05-27 à 17.04.46.png

nesslee
Fluorite | Level 6

this is great ! thank you so much ! 😄

ed_sas_member
Meteorite | Level 14

You're welcome @nesslee 😊

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
  • 3 replies
  • 1450 views
  • 3 likes
  • 2 in conversation