SAS技巧-特殊作用函数使用示例

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='ABC.DEF (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;

   
   
   


   
   




 
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值