Published on :
Reporting SASHELP

Air Quality Analysis and Reporting

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by dynamically creating a custom SAS© format (`pct_`) that associates numerical value ranges with colors (from blue to red) to represent 'cold' to 'hot' levels. It then uses PROC SQL to determine the minimum and maximum values of the 'air' variable in the SASHELP.AIR dataset. A subsequent DATA STEP calculates an 'air quality percentage' indicator, normalized against these min/max values. Finally, PROC TABULATE is used with ODS HTML to generate a visual report, where table cells are colored according to the percentage index, providing a clear view of air quality variations over time.
Data Analysis

Type : SASHELP


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!
1* Create a format to display colors ranging from Blue (cold) to Red (hot) ;
2DATA _null_ ;
3 call execute('proc format fmtlib ; value pct_') ;
4 max=1;
5 maxloop=255 ;
6 DO i=1 to maxloop ;
7 color='cx'||put(i/maxloop*255,hex2.)||'00'||put((maxloop-i)/maxloop*255,hex2.) ;
8 from=((i-1)/maxloop)*max ;
9 to=(i/maxloop)*max ;
10 call execute(put(from,best.)||'-'||put(to,best.)||'='||quote(color)) ;
11 END ;
12 call execute('.="light gray" other="cxd0d0d0" ; run ;') ;
13RUN ;
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!
1* get the maximum value of air ;
2PROC 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!
1* express values of air as a percentage of the maximum ;
2DATA 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 ;
8RUN ;
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!
1* tabulate the results indicating maximum as red, minimum as blue ;
2ods html file='test.html' ;
3title 'Air Quality over the years' ;
4footnote 'Blue (cold) is best, Red (hot) is worst' ;
5PROC TABULATE DATA=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' ;
10RUN ;
11ods 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.