BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sum_sand
Calcite | Level 5

I am using following code to color code the data for pdf report, but it's not working well. These are character variables.

 

compute ORG;
if ORG < LB_2 then do;
call define(col, 'style', 'style={background=cyan}'); /* Set background to blue /
end;
else if ORG > UB_2 then do;
call define(col, 'style', 'style={background=red}'); / Set background to red /
end;
else do;
call define(col, 'style', 'style={background=white}'); / Optional: set to default */
end;
endcomp;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The order of columns in Proc Report is critical when trying to use column items in Compute blocks. Proc Report builds the report from left to right. So a column such as ORG cannot "see" variables that are in columns to the right of Org, such as LB_2 or UB_2. So the comparisons basically fail.

 

Note that without a Columns statement the order of columns is variable order in the data set.

 

Typical solutions would involve one of:

1) change the order of the columns using a COLUMNS statement so LB_2 and UB_2 appear to the left of ORG

2) add additional variables to the data set with values of Lb_2 and Ub_2, have them appear in the column order before Org so they can be used. Have the NOPRINT option set in their defines so the column doesn't actually appear in the report body.

 

Example of approach 1 with a data set you should have available to test the code.

proc report data=sashelp.class;
   columns sex name ;
   compute name;
      if sex='F' then call define(_col_,'style', 'style={background=pink}');
      else if sex='M'  then call define(_col_, 'style', 'style={background=cyan}');
   endcomp;
run;

Do note that this does not color anything:

proc report data=sashelp.class;
   compute name;
      if sex='F' then call define(_col_,'style', 'style={background=pink}');
      else if sex='M'  then call define(_col_, 'style', 'style={background=cyan}');
   endcomp;
run;

 

Example of approach 2 adding a variable in a data step to use with Noprint;

data work.class;
   set sashelp.class;
   dummy_sex = sex;
run;

proc report data=work.class;
   columns dummy_sex name sex;
   define dummy_sex / noprint;
   compute name;
      if dummy_sex='F' then call define(_col_,'style', 'style={background=pink}');
      else if dummy_sex='M'  then call define(_col_, 'style', 'style={background=cyan}');
   endcomp;
run;

There might be other approaches but without data it is hard to say.

View solution in original post

4 REPLIES 4
Ksharp
Super User
Better post your FULL code and sample data.
And you need use keyword _col_ not col:

call define(_col_, 'style', 'style={background=cyan}');
sum_sand
Calcite | Level 5
ods path (prepend) work.template(update) sashelp.tmplmst;
 
 
proc template;
   define style custom_style;
      parent=styles.pearl; 
      style Document /
         margin=0.3in; 
      style Table /
         bordercolor=cxB0B7BB
         borderwidth=0.5pt
         borderstyle=solid; 
      class Header /
         backgroundcolor=lightblue height=40; /* Optional: Change header background color */
  class fonts /
    'TitleFont' = ("<MTsans-serif>, Albany",14pt,bold)
  'TitleFont2' = ("<MTsans-serif>, Albany",14pt,medium italic)
'docFont' = ("<MTsans-serif>, Albany",8pt);
    
   end;
run;
 
 
 
%let tdate = %sysfunc(date(),mmddyy10.);
%let ttime= %sysfunc(time(),timeampm11.);
 
 
title; /* Clear any existing titles */
options nodate nonumber orientation=landscape papersize=letter  topmargin=.25in bottommargin=.25in leftmargin=.25in rightmargin=.25in;
 
        /* Generate PDF report */
ods pdf file="&path.\&_USERORG..pdf" 
notoc
style=custom_style 
startpage=no; 
 
/* Create the report */
proc report data=_&Have. nowd;
    title H=2.0 J=C  'Birth Data Reporting';
    title2 H=2.0 J=C  color=blue "&_USERORG.";
   title3 H=3.0 ;
   title4 H=3.0 J=C F='Arial' "&tdate &ttime";
 
 
    /* Adjust column widths */
    DEFINE CA / STYLE(column)={width=1.5cm};
    DEFINE ORG / STYLE(column)={width=1.5cm};
    DEFINE LAST_60 / STYLE(column)={width=1.5cm};
    DEFINE LAST_60_120 / STYLE(column)={width=1.5cm};
DEFINE LB_2 / 'LB' STYLE(column)={width=1.5cm};
DEFINE UB_2 / 'UB' STYLE(column)={width=1.5cm};
 
compute ORG;
    if ORG < LB_2 then call define(_col_, 'style', 'style={background=cyan}');
    else if ORG > UB_2 then call define (_col_, 'style', 'style={background=red}');
endcomp;
 
 
run;
 
 ods pdf close; 
 
ballardw
Super User

The order of columns in Proc Report is critical when trying to use column items in Compute blocks. Proc Report builds the report from left to right. So a column such as ORG cannot "see" variables that are in columns to the right of Org, such as LB_2 or UB_2. So the comparisons basically fail.

 

Note that without a Columns statement the order of columns is variable order in the data set.

 

Typical solutions would involve one of:

1) change the order of the columns using a COLUMNS statement so LB_2 and UB_2 appear to the left of ORG

2) add additional variables to the data set with values of Lb_2 and Ub_2, have them appear in the column order before Org so they can be used. Have the NOPRINT option set in their defines so the column doesn't actually appear in the report body.

 

Example of approach 1 with a data set you should have available to test the code.

proc report data=sashelp.class;
   columns sex name ;
   compute name;
      if sex='F' then call define(_col_,'style', 'style={background=pink}');
      else if sex='M'  then call define(_col_, 'style', 'style={background=cyan}');
   endcomp;
run;

Do note that this does not color anything:

proc report data=sashelp.class;
   compute name;
      if sex='F' then call define(_col_,'style', 'style={background=pink}');
      else if sex='M'  then call define(_col_, 'style', 'style={background=cyan}');
   endcomp;
run;

 

Example of approach 2 adding a variable in a data step to use with Noprint;

data work.class;
   set sashelp.class;
   dummy_sex = sex;
run;

proc report data=work.class;
   columns dummy_sex name sex;
   define dummy_sex / noprint;
   compute name;
      if dummy_sex='F' then call define(_col_,'style', 'style={background=pink}');
      else if dummy_sex='M'  then call define(_col_, 'style', 'style={background=cyan}');
   endcomp;
run;

There might be other approaches but without data it is hard to say.

sum_sand
Calcite | Level 5

Thank you! Changing column order worked!

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
  • 4 replies
  • 3735 views
  • 0 likes
  • 3 in conversation