Published on :
Reporting CREATION_INTERNE

Data Visualization with PROC CHART

This code is also available in: Deutsch Español Français English
Awaiting validation
The script initializes two datasets, 'htwt' and 'Emart', by incorporating data directly via DATALINES blocks. It then utilizes PROC CHART to produce a series of visualizations: vertical and horizontal histograms for the categorical and numerical variables of the 'htwt' dataset, as well as bar charts for department frequencies, sales distribution, sales grouped by department, and the sum of sales by year from the 'Emart' dataset. Options such as `Midpoints`, `Group`, `Sumvar`, `Type`, `Discrete`, and formats are used to refine the presentation of the charts.
Data Analysis

Type : CREATION_INTERNE


The 'htwt' and 'Emart' datasets are entirely created within the SAS script using DATALINES statements, meaning all source data is internal to the program.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates the temporary dataset 'htwt' and inserts raw data using the DATALINES statement. It defines four variables: 'subject' (numeric), 'gender' (character), 'height' (numeric), and 'weight' (numeric).
Copied!
1DATA htwt;
2INPUT subject gender $ height weight;
3DATALINES;
41 M 68.5 155
52 F 61.2 99
63 F 63.0 115
74 M 70.0 205
85 M 68.6 170
96 F 65.1 125
107 M 72.4 220
118 M . 188
12;
2 Code Block
PROC CHART
Explanation :
This PROC CHART generates a vertical bar chart for the 'gender' variable of the 'htwt' dataset. Each bar represents the frequency of the different 'gender' categories.
Copied!
1PROC CHART DATA= htwt;
2title "Bar chart from Proc Chart";
3Vbar gender;
4RUN;
3 Code Block
PROC CHART
Explanation :
This PROC CHART produces a vertical bar chart for the numerical variable 'height' of the 'htwt' dataset. The `Midpoints` option specifies the central points of the intervals to group height values, thereby creating a class distribution.
Copied!
1PROC CHART DATA= htwt;
2title "Bar chart from Proc Chart";
3Vbar height / Midpoints=60 to 74 BY 2;
4RUN;
4 Code Block
PROC CHART
Explanation :
Similar to the previous block, this PROC CHART generates a bar chart but uses the `Hbar` statement for a horizontal display. It shows the distribution of 'height' with the same midpoints and includes a frequency table on the right side.
Copied!
1* using Hbar statement replacing Vbar, there is frequency table showing up by the right side of the bar
2proc chart data= htwt;
3title "Bar chart from Proc Chart";
4Hbar height / Midpoints=60 to 74 BY 2;
5RUN;
5 Code Block
DATA STEP Data
Explanation :
This second DATA STEP block creates a new temporary dataset named 'Emart' from in-line data. It contains three variables: 'YEAR' (numeric), 'DEPT' (character), and 'SALES' (numeric).
Copied!
1DATA Emart;
2INPUT YEAR DEPT $ SALES;
3DATALINES;
42001 TOYS 5000
52001 TOYS 4500
62001 TOYS 5500
72001 FOOD 4100
82001 FOOD 3300
92002 TOYS 6344
102002 TOYS 4567
112002 TOYS 4567
122002 TOYS 4567
132002 TOYS 4300
142002 FOOD 3700
152002 FOOD 3900
162003 TOYS 7000
172003 TOYS 7200
182003 TOYS 6000
192003 TOYS 7900
202003 FOOD 4000
212003 FOOD 5800
222003 FOOD 5600
23;
6 Code Block
PROC CHART
Explanation :
This PROC CHART displays a simple bar chart of the frequencies of the categorical variable 'dept' from the 'Emart' dataset. Each bar represents the number of occurrences of each department.
Copied!
1PROC CHART DATA=Emart;
2title "Simple Frequency Bar Chart";
3vbar dept;
4RUN;
7 Code Block
PROC CHART
Explanation :
This PROC CHART generates a bar chart for the numerical variable 'sales' of the 'Emart' dataset, showing the distribution of sales values as frequency bars.
Copied!
1PROC CHART DATA=emart;
2title "Bar Chart on a Numerical Variable (Sales)";
3VBAR sales;
4RUN;
8 Code Block
PROC CHART
Explanation :
This block uses `pattern` to define the fill style of the bars (black L2 line). The subsequent PROC CHART creates a vertical bar chart of the 'sales' variable, grouped (`Group=`) by 'Dept'. Sales are displayed with specific midpoints and formatted as currency.
Copied!
1pattern value=L2 color=black;
2PROC CHART DATA=emart;
3title "Distributiion of sales by Department";
4Vbar sales /Group= Dept Midpoints=4500 to 5500 BY 1000;
5FORMAT sales dollar8.0;
6RUN;
9 Code Block
PROC CHART
Explanation :
This PROC CHART generates a bar chart that visualizes the sum (`type=sum`, `sumvar=sales`) of sales by 'year'. The `discrete` option ensures that the year is treated as a distinct categorical variable. Sales are formatted as currency.
Copied!
1PROC CHART DATA=emart;
2title "Sum of Sales by Year";
3vbar year /sumvar=sales type=sum discrete;
4* TPYE= FREQ PCT CFREQ CPCT SUM MEAN (Frequncy, Percent, Cumulative Freq, Cumulative Pct, sum, mean)
5format sales dollar8.;
6RUN;
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.