Sas 简明教程

SAS - Bar Charts

A bar chart represents data in rectangular bars with length of the bar proportional to the value of the variable. SAS uses the procedure PROC SGPLOT to create bar charts. We can draw both simple and stacked bars in the bar chart. In bar chart each of the bars can be given different colors.

Syntax

创建柱状图的基本 SAS 语法为:

PROC SGPLOT DATA = DATASET;
VBAR variables;
RUN;
  1. DATASET − 是所用数据集的名称。

  2. variables − 是用于绘制直方图的值。

Simple Bar chart

简单的柱状图是一种其中数据集的变量表示为条形图的图表。

Example

下面的脚本会创建一个将汽车长度表示为条形图的柱状图。

PROC SQL;
create table CARS1 as
SELECT make, model, type, invoice, horsepower, length, weight
   FROM
   SASHELP.CARS
   WHERE make in ('Audi','BMW')
;
RUN;

proc SGPLOT data = work.cars1;
vbar length ;
title 'Lengths of cars';
run;
quit;

当我们执行以上代码时,我们将得到以下输出:

barchart1

Stacked Bar chart

堆积柱状图是一种其中数据集的一个变量根据另一个变量计算的柱状图。

Example

下面的脚本会创建一个堆积柱状图,其中各个汽车型号的长度都会进行计算。我们使用 group 选项来指定第二个变量。

proc SGPLOT data = work.cars1;
vbar length /group = type ;
title 'Lengths of Cars by Types';
run;
quit;

当我们执行以上代码时,我们将得到以下输出:

barchart2

Clustered Bar chart

分簇柱状图用于展示变量值如何在一种文化中分布。

Example

下面的脚本会创建一个分簇柱状图,其中汽车长度围绕汽车型号进行分簇。我们看到两个长度为 191 的相邻条,一个是汽车型号“轿车”,另一个是汽车型号“旅行车”。

proc SGPLOT data = work.cars1;
vbar length /group = type GROUPDISPLAY = CLUSTER;
title 'Cluster of Cars by Types';
run;
quit;

当我们执行以上代码时,我们将得到以下输出:

barchart3