第7讲:SAS运算符及函数

本节所用数据集:
链接:https://pan.baidu.com/s/1JPQBuL8iv2qrMrFSB4mhuw
提取码:qzg8

  1. SAS运算符
    算术运算符:+、-、*、/、**
    比较算符:=(EQ)、≠(NE)、>(GT)、<(LT)、≥(GE)、≤(LE)、in
    逻辑算符:&(AND)、 |(!)(OR)、^(~)(NOT)
    其它:><最小值、<>最大值、||(!)连接

例1:operator

data oranges;                                                                                                                          
input variety $ flavor texture looks;                                                                                                   
total = flavor + texture + looks;                                                                                                       
                                                                                                                                        
if variety in ( 'navel' , 'valencia' )                                                                                                      
then total = total * 10;                                                                                                                
                                                                                                                                        
if (flavor < textures & variety = 'mandarin')   ##只有&左右两边都符合才执行下列语句                                                                                        
then total = total * 100;                                                                                                               
                                                                                                                                        
a = flavor <> texture ** 2                                                                                                              
b = variety || '.var' ;                                                                                                                   
                                                                                                                                        
label total = '总数' ;                                                                                                                 
cards;                                                                                                                                  
navel 9 8 6                                                                                                                             
temple 7 7 7                                                                                                                            
valencia 8 9 9                                                                                                                          
mandarin 5 7 8                                                                                                                          
;                                                                                                                                       
proc sort data = oranges;                                                                                                               
by descending total;                            ##降序打印                                                                                         
run;                                                                                                                                    
proc print data = oranges;                                                                                                              
/*var _numeric_;*/                                                                                                                      
/*var _character_;*/                                                                                                                    
title '对ORANGES数据集的品尝检验结果';                                                                                                  
run;   

结果
在这里插入图片描述

  1. 字符的比较
    (1)按机器使用的字符排列次序(ASC II或 Unicode)从左到右被比较;
    (2)2个不等长的字符串被比较时,系统在较短的字符串后自动添上空格;
    (3)在比较符后加冒号,则只比较字母。

例2:operator_char
Ctrl+o:打开程序

 data;                                                                                                                                  
input (a b c)($);                           ##abc用同一变量类型(字符串型变量)                                                                                              
                                                                                                                                        
if a>b then result1='true';                                                                                                             
else result1='flase';                                                                                                                   
                                                                                                                                        
result2='Fox'<>'Foxs';/*result2变量的长度由最长的字符串决定*/                                                                           
                                                                                                                                        
result3='Fox'<>'Foxs';                                                                                                                  
                                                                                                                                        
                                                                                                                                        
if a>:'S' then result4='Great than S';       ##把a中的字母与S进行比较                                                                                           
                                                                                                                                        
cards;                                                                                                                                  
GRAY ADAMS G                                                                                                                            
TOM TOMA T                                                                                                                              
Sam Robe S                                                                                                                              
aa SA g s                                                                                                                               
{ | a                                                                                                                                   
w w b                                                                                                                                   
l l c                                                                                                                                   
;                                                                                                                                       
run;                                                                                                                                    
proc print;                                                                                                                             
run;    

  1. 运算次序
    (1)在括弧里的表达式先计算
    第一级:**、(±)前缀 、^ 、 >< 、 <>
    第二级:*、/
    第三级:+、-
    第四级:||
    第五级:其余比较算符
    第六级:&
    第七级:|
    (2)同优先级的运算符,左边运算先做。但有2个例外:
    ①对最高优先级,右边的运算先做;
    如:^ 与 <> 同时存在,<> 先做
    ②当2个比较算符围着一个量时,这个表达式看成是有一个and出现一样被计算。
    如:12 < age < 20,等价于:12 < age & age < 20

  2. SAS函数
    (1)数学函数:
    abs()、max()、min()、mod(x,y)、sign(x)、sqrt(x)、sum()、mean()、exp(x)、log(x)、log10(x)、log2(x)、sin(x)、arsin(x)
    (2)截取函数:
    ceil(x)、floor(x)、int(x)、round(x,n)
    (3)字符函数:
    index(S,S1)、substr(S,p,n)、scan(S2,n)、upcase(S)、compress(S,S1)
    (4)日期时间函数:
    weekday()、day()、month()、qtr()、year()
    SAS系统存贮日期值为1960年1月1日到指定日期之间的天数
    SAS存贮时间值为从午夜开始到指定时间的秒数
    日期时间值存贮为1960年1月1日午夜到指定日期时间之间的秒数
    (5)概率统计函数:
    正态分布:probnorm(x);
    有效数:n(of x1-xn)、缺失值:nmiss(of x1-xn);
    方差:var(of x1-xn)、标准差:std(of x1-xn)
    (6)随机数函数:
    返回正态分布的随机数:rannor(seed);
    返回均匀分布的随机数:ranuni(seed)
    seed≥0

例3:functions

data bbb;                                                                                                                               
input x1-x5 x6 $ date yymmdd15.;  ##生成数值型变量x1-x5,字符串变量x6,时间变量date(年月日共占15位)                                                                                                      
/*format date yymmdd10.;*/                                                                                                              
*format date weekdate12.;                                                                                                               
x7='o';                           ## x7为字符串o                                                                                                      
x8='I am a student';              ## x8为字符串 I am a student                                                                                                     
/*以下是数学函数*/                                                                                                                      
a1=max(x1,x2);                                                                                                                          
a2=sum(x1,x2);                                                                                                                          
a3=sign(x3);                                                                                                                            
a4=sqrt(x5);                                                                                                                            
a5=mean(x1,x2);                                                                                                                         
/*以下是截取函数*/                                                                                                                      
b1=floor(a5);                     ##如:a5 = 5.6,则 b1 = 5                                                                                                      
/*以下是字符函数*/                                                                                                                      
c1=index(x6,x7);                                                                                                                        
c2=substr(x6,3,3);                ##从 x6 的第3个字符开始,取3个数赋值给 c2                                                                                                      
c3=scan(x8,2);                                                                                                                          
c4=upcase(x8);                                                                                                                          
c5=compress(x6,x7);                                                                                                                     
/*以下是时间函数*/                                                                                                                      
d1=weekday(date);                 ##求出 date 是星期几并赋值给 d1                                                                                                      
d2=year(date);                                                                                                                          
d3=qtr(date);                                                                                                                           
d4=day(date);                                                                                                                           
/*以下是概率函数*/   
e1=probnorm(0);                   ## e1 = 0的正态分布的概率值                                                                                                      
e2=n(of x1-x5);                                                                                                                         
e3=nmiss(of x1-x5);                                                                                                                     
e4=var(x1,x2);                    ## e4 = x1与x2的方差                                                                                                      
e5=std(x1,x2);                                                                                                                          
/*以下是随机数函数*/                                                                                                              
f1=ranuni(1);                                                                                                                           
                                                                                                                                        
cards;                                                                                                                                  
1    2    3  5 -6 amazon 2004/9/22                                                                                                      
3    5    0  8 0  box    1960/1/2                                                                                                       
9    10   -7 9 4  check  1960/1/1                                                                                                       
7    6    .  1 8  delete 1959/12/31                                                                                                     
-2.5 -2.3 3  1 2  desk   2005/9/29                                                                                                      
;                                                                                                                                       
run;                                                                                                                                    
proc print;                                                                                                                             
run;

结果:
在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值