Hello Experts,
My code is :
proc report data=sashelp.shoes;
column Region n Product Subsidiary n=n2;
define Region / group ;
define Product / group ;
define Subsidiary / group ;
define n / "Nombre de Régions" ;
define n2 / "Nombre de filiales" ;
compute Region;
if Region="Africa" then call define (_ROW_,'style','style=[background=lightblue foreground=red ]');
endcomp;
run;
I'm wondering how to color the group of observation for "Africa" please. When I run this code, I have only one row colored :
Thank you very much for your help.
Hi:
The challenge with your approach is using REGION as a GROUP item. I understand why you needed it. But the side effect of GROUP or ORDER usage is that only the first row of the group has a value and repetitive values of other rows in the group are set to missing, which in this case, is the blank. PROC REPORT does not have any visibility of the value for the variable REGION except on the first report row for the group.
But this is an instance where a helper variable or temporary variable can help you. PROC REPORT automatically retains the value of temporary variables that you use, so in this case, I changed the COMPUTE block to create 3 helper variables based on REGION not being blank (on the first of the GROUP). I also created a user-defined format for the background and foreground colors. I used a subset of just 4 REGION values and one PRODUCT value to make a subset where all the rows would fit in one screen shot so you could see how I was also able to eliminate the IF statement based on using the FORMAT.
The other reason this works is that the 3rd argument to the CALL DEFINE (the style values) can be specified with a variable value. I use all of my temporary values in the COMPUTE block for REGION and as you see, the colors for the 3 REGIONS are what I specified and the colors for Pacific were based on the OTHER category in the format.
Here's the annotated code and output:
Cynthia
I recently asked a similar question, which ought to give you enough to solve your problem.
Thanks to @Ksharp for the correct answer.
Hi:
The challenge with your approach is using REGION as a GROUP item. I understand why you needed it. But the side effect of GROUP or ORDER usage is that only the first row of the group has a value and repetitive values of other rows in the group are set to missing, which in this case, is the blank. PROC REPORT does not have any visibility of the value for the variable REGION except on the first report row for the group.
But this is an instance where a helper variable or temporary variable can help you. PROC REPORT automatically retains the value of temporary variables that you use, so in this case, I changed the COMPUTE block to create 3 helper variables based on REGION not being blank (on the first of the GROUP). I also created a user-defined format for the background and foreground colors. I used a subset of just 4 REGION values and one PRODUCT value to make a subset where all the rows would fit in one screen shot so you could see how I was also able to eliminate the IF statement based on using the FORMAT.
The other reason this works is that the 3rd argument to the CALL DEFINE (the style values) can be specified with a variable value. I use all of my temporary values in the COMPUTE block for REGION and as you see, the colors for the 3 REGIONS are what I specified and the colors for Pacific were based on the OTHER category in the format.
Here's the annotated code and output:
Cynthia
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.