SAS技巧-特殊作用函数使用示例
函数1:lag<n>(变量名) 函数
此函数用于DATA STEP中,返回当前变量前N行该变量的值,默认返回上一行信息
data a;
infile datalines ;
length a 8.;
input a;
datalines;
1
2
3
4
5
;
run;
data _null_;
set a;
c=lag(a);
put c= a=;
run;
输出
c=. a=1
c=1 a=2
c=2 a=3
c=3 a=4
c=4 a=5
函数2:dif<n>(变量名) 函数
dif<n>(变量名)=变量名-lag<n>(变量名)
其返回当前行变量值和上N行变量值的差异
函数3:COUNT(字符串,子串<,限定符>)
返回字符串中子串出现的次数,可用限定符来不区分大小写匹配,默认是区分
xyz='This is a thistle? Yes, this is a thistle.';
howmanythis=count(xyz,'this');
put howmanythis;
结果:3
函数4:INDEX(字符串,搜索的字符串)
返回搜索的字符串在字符串中首次出现的位置
a=' (X=Y)';
b='X=Y';
x=index(a,b);
put x;
结果:10
函数indexw和index雷同,只是前者搜索是单个的字
函数5:find(字符串,子串,开始位置,限定符号)
这个函数在我看来就是用来取代index函数的
其可以指定从哪儿开始寻找,并可以指定是否区分大小写,比index方便多了
xyz='She sells seashells? Yes, she does.';
startposvar=22
whereisshe_22=find(xyz,'she',startposvar);
put whereisshe_22;
结果:27
函数6:VERIFY(source,excerpt-1<,...excerpt-n>)
返回source中不在excerpt-1<,...excerpt-n>中的第一个字符的位置
data scores;
input Grade : $1. @@;
check='abcdf';
if verify(grade,check)>0 then
put @1 'INVALID ' grade=;
datalines;
a b c b c d f a a q a b d d b
;
结果:INVALID Grade=q
函数7:RXMATCH(匹配规则,字符串)
寻找字符串中匹配上的第一个字符的位置
需要和RXPARSE 函数搭配使用,其匹配规则符合正则表达匹配规则,如下
data test;
input string $;
datalines;
abcxyzpq
xyyzxyZx
x2z..X7z
;
data _null_;
set;
length to $20;
if _n_=1 then
rx=rxparse("` x < ? > 'z' to ABC =1 '@#%'");
retain rx;
drop rx;
put string=;
match=rxmatch(rx,string);
put @3 match=;
call rxsubstr(rx,string,position);
put @3 position=;
call rxsubstr(rx,string,position,length,score);
put @3 position= Length= Score=;
call rxchange(rx,999,string,to);
put @3 to=;
call rxchange(rx,999,string);
put @3 'New ' string=;
run;
相关文章
- asp内置函数formatdatetime使用示例代码
- matlab中sum怎么用,Matlab中求和函数sum的使用示例
- OpenCV相机标定与3D重建(20)将单应性矩阵分解为旋转矩阵、平移向量和法向量函数decomposeHomographyMat的使用-代码示例
- CSS 技巧一则 -- 在 CSS 中使用三角函数绘制曲线图形及展示动画
- SAS技巧-特殊作用函数使用示例
- 帆软动态过滤、函数eval使用小技巧
- 《企业云桌面实施》-小技巧-03-vSAN6.5中SAS和SSD的使用建议
- Matlab(1): 随机函数rand使用的技巧
- C++的find函数使用小技巧
- MySQL GROUP_CONCAT函数使用示例:如何用一个SQL查询出一个班级各个学科第N名是谁?