数值型变量包括日期和普通数值型
数值型变量
- 所有变量设置长度用length.
- 计算mean min max mid
data class1;
set course.exam;
Mean = mean(math, chinese,english,physics,chemistry);
Mid = median(math, chinese,english,physics,chemistry);
Max = max(math, chinese,english,physics,chemistry);
Min = min(math, chinese,english,physics,chemistry);
run;
math到chemistry多个变量连续出现,可以简写
data class1;
set course.exam;
Min = min(of math--chemistry);
run;
- 《The Little SAS Book》中提到,还可以根据变量类型采用简便的用法
/* 求所有数值型变量的平均数 */ Mean = mean(OF _numeric_); /* 求所有变量的平均数 */ Mean = mean(OF _all_);
所有的字符型变量用_character_表示。
- 数字的化简:1.四舍五入.可设置保留位数
math1 = round(math,0.01) ;
2.向下取。比如89.1的结果为89
math1 = floor(math);
3.向上取值。结果为90
math1 = ceil(math);
- 其他数学运算:乘方** 开平方sqrt 绝对值abs 自然对数log 以10为底的对数log10 以2为底的对数log2 正弦函数sin 余弦函数cos 正切函数tan 反正弦函数asin 反余弦函数acos 反正切函数atan
SAS不用角度值计算,而是弧度值。如sin30°的计算需要将30°设定为1/6π。
data a ;
/* 使用constant来定义常量。此处使用它来定义π(可能在SAS中π的表达方式为pi?) */
pi = constant( 'pi');
b = sin(pi/6);
run;
日期型变量
- 起始时间为1960年1月1日00时00分00秒。
- 日期和时间数值意义均为与起点日期间隔。可以为负。
- mdy(m,d,yr) 生成yr年m月d日的SAS日期值 year/month/day/weekday/qtr(date) 由日期值得到年/月/日/周几/季度
- 日期间的加减:①计算日期间隔 intck ( " interval " , start , end) ②计算一定间隔后的日期 intnx ( " interval " , start , n)
interval可以的取值:day week month year quarter
- 现在时间的函数为 today() .
data a ; set lesson.birth; yyyy=year(birthday); mm= month(birthday); age = intck('year',birthday,today()); retire = intnx('year',birthday,65); format retire E8601DA.;