Data comes from the SASHELP.AIR dataset, an example dataset integrated into SAS.
1 Code Block
DATA STEP
Explanation : This DATA _NULL_ block dynamically generates a PROC FORMAT procedure. It creates a format named `pct_` that maps numerical values (representing a percentage from 0 to 1) to hexadecimal color codes. Colors gradually vary from blue (cold, low percentage) to red (hot, high percentage). Values outside the defined range receive a default gray color.
Copied!
* Create a format to display colors ranging from Blue (cold) to Red (hot) ;
data _null_ ;
call execute('proc format fmtlib ; value pct_') ;
max=1;
maxloop=255 ;
do i=1 to maxloop ;
color='cx'||put(i/maxloop*255,hex2.)||'00'||put((maxloop-i)/maxloop*255,hex2.) ;
from=((i-1)/maxloop)*max ;
to=(i/maxloop)*max ;
call execute(put(from,best.)||'-'||put(to,best.)||'='||quote(color)) ;
end ;
call execute('.="light gray" other="cxd0d0d0" ; run ;') ;
run ;
1
* Create a format to display colors ranging from Blue (cold) to Red (hot) ;
call execute('.="light gray" other="cxd0d0d0" ; run ;') ;
13
RUN ;
2 Code Block
PROC SQL
Explanation : Uses PROC SQL to extract the maximum and minimum values of the 'air' variable from the `sashelp.air` dataset. These values are then stored in the macro variables `&max` and `&min`. A macro variable `&range` is calculated to obtain the total range between the minimum and maximum, which will be used to normalize the data later.
Copied!
* get the maximum value of air ;
proc sql ;
select max(air),min(air) into :max,:min from sashelp.air ;
%let range=%sysevalf(&max-&min) ;
1
* get the maximum value of air ;
2
PROC SQL ;
3
select max(air),min(air) into :max,:min from sashelp.air ;
4
%let range=%sysevalf(&max-&min) ;
3 Code Block
DATA STEP Data
Explanation : This DATA STEP creates a new temporary dataset named 'air'. It reads the `sashelp.air` dataset, extracts the year and month from the 'date' variable, and then calculates a new 'pct' variable. This 'pct' variable represents the normalized 'air' value, expressed as a percentage above the minimum and within the range defined by `&range`. It quantifies relative air quality.
Copied!
* express values of air as a percentage of the maximum ;
data air ;
set sashelp.air ;
year=year(date) ;
month=month(date) ;
* percentage is the level above the minimum ;
pct=(air-&min)/&range ;
run ;
1
* express values of air as a percentage of the maximum ;
2
DATA air ;
3
SET sashelp.air ;
4
year=year(date) ;
5
month=month(date) ;
6
* percentage is the level above the minimum ;
7
pct=(air-&min)/&range ;
8
RUN ;
4 Code Block
PROC TABULATE
Explanation : This block generates an interactive HTML report ('test.html') using ODS HTML and PROC TABULATE. It displays a pivot table of the 'pct' variable (air quality percentage), classified by year and month. The cell style is modified by applying the previously created `pct_` format, which colors the table cell background according to the 'pct' value, from blue (best) to red (worst), for an intuitive visualization of air quality.
Copied!
* tabulate the results indicating maximum as red, minimum as blue ;
ods html file='test.html' ;
title 'Air Quality over the years' ;
footnote 'Blue (cold) is best, Red (hot) is worst' ;
proc tabulate data=air style={background=pct_.} ;
class year month ;
var pct ;
label pct='Air Quality Percent of worst month' ;
table sum=''*pct*f=percent.,year='Year',month='Month of Year' ;
run ;
ods html close ;
1
* tabulate the results indicating maximum as red, minimum as blue ;
2
ods html file='test.html' ;
3
title 'Air Quality over the years' ;
4
footnote 'Blue (cold) is best, Red (hot) is worst' ;
5
PROC TABULATEDATA=air style={background=pct_.} ;
6
class year month ;
7
var pct ;
8
label pct='Air Quality Percent of worst month' ;
9
TABLE sum=''*pct*f=percent.,year='Year',month='Month of Year' ;
10
RUN ;
11
ods html close ;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.