zoukankan      html  css  js  c++  java
  • sas正则式之prxparen

    sas正则式之prxparen

    今天还是要继续正则式的内容,这周的内容是prxparen函数,这个函数我个人觉得特别有用,是因为他和“|”结合使用可以同时容纳很多种情况的字符串。

    prxparse这个函数没有什么参数,就是填入prxparse正则式的字符返回对应的是praparse的哪一部分。我现在说你肯定很懵逼,来,我们举个栗子!

    re=prxparse("/(one)|(two)|(three)/")

    position=prxmatch(re,string)

     

    就是这么说prxparse("/(one)|(two)|(three)/")搜索字符函数中/(one)|(two)|(three)/ 用“|”分开了三个即将要搜索的字符,即‘’one”为第一个搜索字符,一旦在字符中发现“one”了就返回1,假设第一个发现的字符串是“three”就是返回3,所以返回刚才的例子就很容易理解了。


    现在用一个小例子再介绍这个函数

    data paren;

    if _n_=1 then pattern=prxparse("/(d )|(dd )|(ddd )/");

    retain pattern;

    input string $char30.;

    position=prxmatch(pattern,string);

    if position gt then which_paren=prxparen(pattern);

    datalines;

    one single digit 8 here

    two 888 77

    12345 1234 123 12 1

    ;

    run;

    结果:

    如图所示,position是函数prxmatch的返回结果,即字符的位置,which_paren是prxparen的返回结果,即对应的是字符搜索的哪一部分。if position gt then which_paren=prxparen(pattern); 这里的if是为了确认是prxmatch搜索到位置的时候才判断字符是属于pattern的哪一部分。

    现在用一个实际数据处理中的例子再深化这个函数。

    这是一份领导给我的数据,可以看到数据上中文字符之后还有一些乱七八糟的英文啊符号啊数字啊,当时领导的需求是,把这份数据清洗出来变成只用字符的变量以及不要用类似“黄山分行”这种字眼的存在。所以当下就写了以下这段代码解决的这个需求。来,上代码!

    data ss;

    set dd;

    if _n_=1 then

    ret=prxparse("/(D?银行)|(D?金融)|(D?贷款)|(D?融资)|(D?保险)|(D?担保)|(D?信用[社合联])/");

    retain ret;

    position=prxmatch(ret,QUERY_OPERATOR);

    if position gt then which_posit=prxparen(ret);

    else which_posit=0;

    if which_posit gt then do;

    call prxsubstr(ret,QUERY_OPERATOR,start,length);

    if start gt and which_posit=1 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"银行";

    end;

    if start gt and which_posit=2 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"金融公司";

    end;

    if start gt and which_posit=3 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"贷款公司";

    end;

    if start gt and which_posit=4 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"融资有限公司";

    end;

    if start gt and which_posit=5 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"保险有限公司";

    end;

    if start gt and which_posit=6 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"担保公司";

    end;

    if start gt and which_posit=7 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"农村信用合作社";

    end;

    end;

    else dd='其他';

    drop start length which_posit position ret;

    run;

    结果:

    现在单独取一个if语句的下的执行语句介绍一下:

    call prxsubstr(ret,QUERY_OPERATOR,start,length);

    if start gt and which_posit=1 then do;

    dd=substr(QUERY_OPERATOR,1,start)||"银行";

    end;

    call prxsubstr 在之前的文章中已经介绍过了,所以如果忘了,再翻一下。然后if start gt and which_posit=1 then do ;这个语句中的which_posit=1 即就是搜索到“银行”两个字就是认定这个机构就是银行卡,然后就截取“银行”前面的那一串字,然后再接上“银行”两个字。这里你会问,问什么长度为什么用的是“start”这个变量,因为我们搜索的是”银行”两个字,所以返回的位置是银行的位置,但是我们需要的是“银行”前面的那一串字符,所以就使用start作为长度。数据分析师培训

  • 相关阅读:
    Java高级之类结构的认识
    14.8.9 Clustered and Secondary Indexes
    14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器
    14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构
    14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用
    14.8.1 Creating InnoDB Tables 创建InnoDB 表
    14.7.4 InnoDB File-Per-Table Tablespaces
    14.7.2 Changing the Number or Size of InnoDB Redo Log Files 改变InnoDB Redo Log Files的数量和大小
    14.7.1 Resizing the InnoDB System Tablespace InnoDB 系统表空间大小
    14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB
  • 原文地址:https://www.cnblogs.com/amengduo/p/9587132.html
Copyright © 2011-2022 走看看