Sas 简明教程

SAS - Format Data Sets

有时我们希望以不同于数据集中已经存在的格式显示分析后的数据。例如,我们要给带有价格信息的变量添加美元符号和小数点后两位。或者,我们可能想要显示一个全部大写的文本变量。我们可以使用 FORMAT 应用内置 SAS 格式, PROC FORMAT 用于应用用户定义的格式。一个格式还可以应用于多个变量。

Sometimes we prefer to show the analyzed data in a format which is different from the format in which it is already present in the data set. For example we want to add the dollar sign and two decimal places to a variable which has price information. Or we may want to show a text variable, all in uppercase. We can use FORMAT to apply the in-built SAS formats and PROC FORMAT is to apply user defined formats. Also a single format can be applied to multiple variables.

Syntax

应用内置 SAS 格式的语法基本语法为:

The basic syntax for applying in-built SAS formats is −

format variable name format name

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. variable name is the variable name used in dataset.

  2. format name is the data format to be applied on the variable.

Example

让我们考虑下面的 SAS 数据集,其中包含某个组织的员工详细信息。我们希望以大写形式显示所有名称。使用 formatstatement 来实现此目的。

Let’s consider the below SAS data set containing the employee details of an organization. We wish to show all the names in uppercase. The formatstatement is used to achieve this.

DATA Employee;
   INPUT empid name $ salary DEPT $ ;
   format name $upcase9. ;
DATALINES;
1 Rick 623.3	IT
2 Dan 515.2 	OPS
3 Mike 611.5 	IT
4 Ryan 729.1    HR
5 Gary 843.25   FIN
6 Tusar 578.6   IT
7 Pranab 632.8  OPS
8 Rasmi 722.5   FIN
;
RUN;
   PROC PRINT DATA = Employee;
RUN;

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

When the above code is executed, we get the following output.

format1

Using PROC FORMAT

我们也可以使用 PROC FORMAT 格式化数据。在下面的示例中,我们将新值分配给变量DEPT扩展部门名称。

We can also use PROC FORMAT to format data. In the below example we assign new values to the variable DEPT exapnding the name of the department.

DATA Employee;
   INPUT empid name $ salary DEPT $ ;

DATALINES;
1 Rick 623.3 IT
2 Dan 515.2 OPS
3 Mike 611.5 IT
4 Ryan 729.1 HR
5 Gary 843.25 FIN
6 Tusar 578.6 IT
7 Pranab 632.8 OPS
8 Rasmi 722.5 FIN
;
proc format;
value $DEP 'IT' = 'Information Technology'
      'OPS'= 'Operations' ;
RUN;
   PROC PRINT DATA = Employee;
   format name $upcase9. DEPT $DEP.;
RUN;

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

When the above code is executed, we get the following output.

format2