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

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 :

 

MarieT_0-1626723835198.png

 

 

Thank you very much for your help.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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_sas_0-1626739778478.png

Cynthia

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I recently asked a similar question, which ought to give you enough to solve your problem.

https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-how-to-test-group-variable-value-in-COMPU...

 

Thanks to @Ksharp for the correct answer.

--
Paige Miller
Cynthia_sas
SAS Super FREQ

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_sas_0-1626739778478.png

Cynthia

SASdevAnneMarie
Barite | Level 11
Thank you very much Cynthia!

SAS Innovate 2025: Register Now

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!

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