Sas 简明教程
SAS - Functions
SAS 拥有各种内置函数,有助于分析和处理数据。这些函数用作 DATA 语句的一部分。它们以数据变量作为参数,并返回存储到另一个变量中的结果。根据函数的类型,它所采用的参数数量可能会发生变化。某些函数接受零个参数,而其他一些函数则接受固定数量的变量。以下是 SAS 提供的函数类型列表。
SAS has a wide variety of in built functions which help in analysing and processing the data. These functions are used as part of the DATA statements. They take the data variables as arguments and return the result which is stored into another variable. Depending on the type of function, the number of arguments it takes can vary. Some functions accept zero arguments while some other accept fixed number of variables. Below is a list of types of functions SAS provides.
Syntax
在 SAS 中使用函数的常规语法如下所示。
The general syntax for using a function in SAS is as below.
FUNCTIONNAME(argument1, argument2...argumentn)
这里的参数可以是常量、变量、表达式或另一个函数。
Here the argument can be a constant, variable, expression or another function.
Function Categories
根据其用途,SAS 中的函数被归类如下。
Depending on their usage, the functions in SAS are categorised as below.
-
Mathematical
-
Date and Time
-
Character
-
Truncation
-
Miscellaneous
Mathematical Functions
这些函数用来对变量值施加一些数学运算。
These are the functions used to apply some mathematical calculations on the variable values.
Examples
下面的 SAS 程序演示了几个重要的数学函数的使用。
The below SAS program shows the use of some important mathematical functions.
data Math_functions;
data Math_functions;
v1=21; v2=42; v3=13; v4=10; v5=29;
/* Get Maximum value */
max_val = MAX(v1,v2,v3,v4,v5);
/* Get Minimum value */
min_val = MIN (v1,v2,v3,v4,v5);
/* Get Median value */
med_val = MEDIAN (v1,v2,v3,v4,v5);
/* Get a random number */
rand_val = RANUNI(0);
/* Get Square root of sum of the values */
SR_val= SQRT(sum(v1,v2,v3,v4,v5));
proc print data = Math_functions noobs;
run;
当运行上述代码时,我们会得到以下输出:
When the above code is run, we get the following output −
Date and Time Functions
这些函数用来处理日期和时间值。
These are the functions used to process date and time values.
Examples
下面的 SAS 程序演示了日期和时间函数的使用。
The below SAS program shows the use of date and time functions.
data date_functions;
INPUT @1 date1 date9. @11 date2 date9.;
format date1 date9. date2 date9.;
/* Get the interval between the dates in years*/
Years_ = INTCK('YEAR',date1,date2);
/* Get the interval between the dates in months*/
months_ = INTCK('MONTH',date1,date2);
/* Get the week day from the date*/
weekday_ = WEEKDAY(date1);
/* Get Today's date in SAS date format */
today_ = TODAY();
/* Get current time in SAS time format */
time_ = time();
DATALINES;
21OCT2000 16AUG1998
01MAR2009 11JUL2012
;
proc print data = date_functions noobs;
run;
当运行上述代码时,我们会得到以下输出:
When the above code is run, we get the following output −
Character Functions
这些函数用来处理字符或文本值。
These are the functions used to process character or text values.
Examples
下面的 SAS 程序演示了字符函数的使用。
The below SAS program shows the use of character functions.
data character_functions;
/* Convert the string into lower case */
lowcse_ = LOWCASE('HELLO');
/* Convert the string into upper case */
upcase_ = UPCASE('hello');
/* Reverse the string */
reverse_ = REVERSE('Hello');
/* Return the nth word */
nth_letter_ = SCAN('Learn SAS Now',2);
run;
proc print data = character_functions noobs;
run;
当运行上述代码时,我们会得到以下输出:
When the above code is run, we get the following output −
Truncation Functions
这些函数用来截断数值。
These are the functions used to truncate numeric values.
Examples
下面的 SAS 程序演示了截断函数的使用。
The below SAS program shows the use of truncation functions.
data trunc_functions;
/* Nearest greatest integer */
ceil_ = CEIL(11.85);
/* Nearest greatest integer */
floor_ = FLOOR(11.85);
/* Integer portion of a number */
int_ = INT(32.41);
/* Round off to nearest value */
round_ = ROUND(5621.78);
run;
proc print data = trunc_functions noobs;
run;
当运行上述代码时,我们会得到以下输出:
When the above code is run, we get the following output −
Miscellaneous Functions
现在让我们了解一下 SAS 的其他一些函数,并提供一些示例。
Let us now understand the miscellaneous functions of SAS with some examples.
Examples
下面的 SAS 程序演示了杂项函数的使用。
The below SAS program shows the use of Miscellaneous functions.
data misc_functions;
/* Nearest greatest integer */
state2=zipstate('01040');
/* Amortization calculation */
payment = mort(50000, . , .10/12,30*12);
proc print data = misc_functions noobs;
run;
当运行上述代码时,我们会得到以下输出:
When the above code is run, we get the following output −