SAS的初级入门(二)

1. SAS 的运算符,其中常用的算术运算符,包括:+、-、*、/;比较算符,包括:=(eq)、^=(NE)、>(gt)、< (lt)、〈=(lt)、〉=(gt)、in(包含);逻辑算符,包括:&(and)、|(or);其他的运算符,〉〈(最小值),〈〉(最大值),||(连接符)

例子:

 1 data oranges;
 2      input variety $ flavor  texture looks;
 3      total=flavor+texture+looks;
 4 
 5      if variety in ('navel','valencia') 
 6          then total=total*10;
 7 
 8      if (flavor<texture & variety='mandarin') 
 9          then total=total*100; 
10 
11      a=flavor<>texture**2;
12      b=variety||'.var';
13  
14      label total="总数";
15      cards;
16 navel  9 8 6
17 temple 7 7 7
18 Valencia 8 9 9
19 mandarin 5 7 8
20 ;
21 proc sort data=oranges;
22   by descending total;
23   run;
24 proc print data=oranges;
25   /*var _numeric_;*/
26  /* var _character_;*/
27   title '对ORANGES数据集的品尝检验结果';
28 run;

2. SAS 中的函数

字符串函数:floor 取最小值;substr(S,p,x)表示截取字符串从第p开始;截取 x 长度的字符,scan(S,n)表示获得以空格分割的第n个字符串,也可以写成scan(S,n,s1)表示获得以s1分割的第n个字符串;upcase(S)表示将字符串变为大写;compress(S1,S2)表示将两个字符串取出S1中所有的S2的字符。

数学函数:max(x1,x2)取两个数中的最大值;sum(x1,x2)求两个数之和;sign(x1)符号函数;mean(x1,x2)取两个数的平均值。

时间函数:year(date)获得年;month(date)获得月份;day(date)获得天;qtr(date)获得季度。

其他函数:ranuni(seed)随机函数,其中的seed要大于0。

例子:

 1 data bbb;
 2   input x1-x5 x6 $ date yymmdd15.;
 3   /*format date yymmdd10.;*/
 4   *format date weekdate12.;
 5   x7='o';
 6   x8='I am a student';
 7   /*以下是数学函数*/
 8   a1=max(x1,x2);
 9   a2=sum(x1,x2);
10   a3=sign(x3);
11   a4=sqrt(x5);
12   a5=mean(x1,x2);
13   /*以下是截取函数*/
14   b1=floor(a5);
15   /*以下是字符函数*/
16   c1=index(x6,x7);
17   c2=substr(x6,3,3);
18   c3=scan(x8,2);
19   c4=upcase(x8);
20   c5=compress(x6,x7);
21   /*以下是时间函数*/
22   d1=weekday(date);
23   d2=year(date);
24   d3=qtr(date);
25   d4=day(date);
26   /*以下是概率函数*/
27   e1=probnorm(0);
28   e2=n(of x1-x5);
29   e3=nmiss(of x1-x5);
30   e4=var(x1,x2);
31   e5=std(x1,x2);
32   /*以下是随机数函数*/
33   f1=ranuni(1);
34   
35   cards;
36 1  2   3  5  -6  amazon   2004/9/22
37 3  5   0  8  0   box      1960/1/2
38 9  10  -7  9  4  check    1960/1/1
39 7  6   .  1  8   delete   1959/12/31
40 -2.5 -2.3 3 1 2  desk     2005/9/29 
41 ;
42 run;
43 proc print;
44 run;

3. SAS 中的数据步

SAS 中含有一些内置变量,格式为"_变量名_",其中 _N_ 表示data 步执行的次数,也就是读到了数据的第几行,_error_ 为 1 的时候表示数据步出错。

例子:

 1 data flow;
 2   /*put x= y= z= _n_;*/
 3   input x y;
 4   z=x+y;
 5   put x= y= z=;
 6   put _n_= _error_=;
 7   cards;
 8 10 20
 9 100 200
10 1000 2000
11 ;
12 run;

 

posted @ 2019-06-07 18:01  Guai人  阅读(945)  评论(0编辑  收藏  举报