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;
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.
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.
Thank you! Changing column order worked!
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.