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

Hi Everyone,

I have 2 set of column: s1 s2 and v1 v2. 

I have 2 column color_cell1 and color_cell2.

 

If color_cell1>0 then color S1 and V1

If color_cell2>0 then color S2 and V2

 

So I can do it with macro variable created separately as you can see in my main code.

 

I need a DO LOOP since my actual data has a lot of column.

 

My Macro works! 

In the original separated one, each location check create 1 set of color

In the Marco, it is the same set of color.

Thus the color pattern not match

(difficult to explain)

 

Thanks a lot.

HHCFX

 

*IT WORKS!*
%macro xxx(); proc report data=have nowd; column id v1 v2 s1 s2 color_cell_1 color_cell_2; define s1/display; define s2/display; define v1/display; define v2/display; define color_cell_1/display; define color_cell_2/display; %DO location=1 %to 2; compute color_cell_&location; if color_cell_&location>0 then do; do i=&location+1; call define (i,"style","style={background=RED}"); end; do j=&location+3; call define (j,"style","style={background=BIYG}"); end; end; endcomp; %END; run; %mend; %xxx();run;

 

 

****MAIN CODE**************;
data have;
input ID s1 s2 v1 v2 color_cell_1 color_cell_2;
datalines;
1 1 1 1 1 0 1
2 1 2 3 0 0 0
3 1 2 3 1 1 0
4 9 8 7 4 1 1
;run;



*MACRO VARIABLE to apply to the 2 sets;
%let location1=1;
%let location2=2;


proc report data=have nowd;
column id v1 v2 s1 s2 color_cell_1 color_cell_2;

define s1/display;
define s2/display;

define v1/display;
define v2/display;
define color_cell_1/display;
define color_cell_2/display;


*for set v1 s1 based on color_cell1;
compute  color_cell_&location1;
	if color_cell_&location1>0 then do;
		do i=&location1+1;
		call define (i,"style","style={background=RED}");
		end;

		do j=&location1+3;
		call define (j,"style","style={background=RED}");
		end;
	end;
endcomp;

*for set v2 s2 based on color_cell2;
compute  color_cell_&location2;
	if color_cell_&location2>0 then do;
		do i=&location2+1;
		call define (i,"style","style={background=BIYG}");
		end;

		do j=&location2+3;
		call define (j,"style","style={background=BIYG}");
		end;
	end;
endcomp;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
hhchenfx
Rhodochrosite | Level 12

I found the solution.

Posted in the question.

View solution in original post

4 REPLIES 4
hhchenfx
Rhodochrosite | Level 12

I found the solution.

Posted in the question.

ChrisNZ
Tourmaline | Level 20

I don't see a solution anywhere, but this works:

 

****MAIN CODE**************;
data HAVE;
input ID V1 V2 S1 S2 COLOR_CELL_1 COLOR_CELL_2;
datalines;
1 1 1 1 1 0 1
2 1 2 3 0 0 0
3 1 2 3 1 1 0
4 9 8 7 4 1 1
run;

proc report data=HAVE nowd;
  column ID V1 V2 S1 S2 COLOR_CELL_1 COLOR_CELL_2;

  define S1/display;
  define S2/display;

  define V1/display;
  define V2/display;
  define COLOR_CELL_1/display;                 
  define COLOR_CELL_2/display;             

%macro loop;
  %local i;
  %do i=1 %to 2; 
  compute COLOR_CELL_&i;              
    if COLOR_CELL_&i.>0 then call define ("V&i","style","style={background=RED}");
    if COLOR_CELL_&i.>0 then call define ("S&i","style","style={background=RED}");
  endcomp;
  %end;
%mend; 
%loop

run;

 

 

Capture.PNG

 

 

hhchenfx
Rhodochrosite | Level 12

So you put the macro inside Proc Report.

I put it outside.

Nice to know.

Thanks.

HHC

ChrisNZ
Tourmaline | Level 20

Oh I see what you are doing now. So convoluted.

 

You can write the 25 lines of code as:

	%do location=1 %to 2;
		compute COLOR_CELL_&location.;
			if COLOR_CELL_&location. > 0 then do;
				call define (&location.+1,"style","style={background=RED }");
				call define (&location.+3,"style","style={background=BIYG}");
			end;
		endcomp;
	%end

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1286 views
  • 0 likes
  • 2 in conversation