Published on :
Reporting SASHELP

Density Plot with PROC SGPLOT

This code is also available in: Deutsch Español Français
The script uses the `PROC SGPLOT` procedure to create two distinct plots. The first plot shows the distribution of the 'horsepower' variable from the `sashelp.cars` dataset by superimposing a normal density estimate and a kernel density estimate. The second plot compares the distributions of the 'MSRP' and 'Invoice' variables from the same dataset. Each plot is generated in a separate PNG file using ODS (Output Delivery System) instructions that define the output path, dimensions, and image name.
Data Analysis

Type : SASHELP


The data comes from the `sashelp.cars` table, which is a standard data library provided with SAS.

1 Code Block
PROC SGPLOT
Explanation :
This block first configures the ODS environment to save the plots in a specified directory. Then, it uses `PROC SGPLOT` to create a density plot for the 'horsepower' variable from the `sashelp.cars` table. The plot superimposes a normal density curve and a kernel density curve to compare the distributions. The output is a PNG image named 'density1.png'.
Copied!
1ods listing gpath="/home/nicolasdupont0/resources_github/Graph/Distribution/img" image_dpi=200;
2 
3*---------------------------------------------------;
4ods graphics /
5 reset = all attrpriority=color border = no width = 600px height = 400px
6 imagename = "density1" imagefmt = png outputfmt = png antialiasmax = 10000;
7 
8title '1# Distribution of the numerical variable horsepower in the cars dataset';
9PROC SGPLOT DATA=sashelp.cars;
10 title "1# horsepower Density";
11 density horsepower / type=normal scale=percent legendlabel="Normal";
12 density horsepower / type=kernel scale=percent legendlabel="kernel";
13 xaxis min=0;
14 yaxis min=0;
15RUN;
2 Code Block
PROC SGPLOT
Explanation :
This second `PROC SGPLOT` block generates a plot comparing the density distributions of the 'MSRP' and 'Invoice' variables from the `sashelp.cars` table. Both curves are displayed on the same plot for direct comparison, with a legend to identify them. The output is a PNG image named 'density2.png'.
Copied!
1*---------------------------------------------------;
2ods graphics /
3 reset = all attrpriority=color border = no width = 600px height = 400px
4 imagename = "density2" imagefmt = png outputfmt = png antialiasmax = 10000;
5 
6title '2# Distribution of two numerical variables MSRP and Invoice from the cars dataset';
7PROC SGPLOT DATA=sashelp.cars;
8 title "2# MSRP and Invoice Density";
9 density MSRP / scale=percent name="MSRP" legendlabel="MSRP";
10 density Invoice / scale=percent name="Invoice" legendlabel="Invoice";
11 xaxis label="Distribution" min=0;
12 yaxis label="%" min=0;
13 keylegend "MSRP" "Invoice" / across=1 position=Topleft location=Inside;
14RUN;
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.