简体   繁体   中英

Using Perl Regular Expressions in SAS Proc SQL statements

I was attempting to use regular expressions in a SAS SQL statement and couldn't get them working. It runs, but it doesn't return the matched expression (column xx is always blank). Not sure if this is something I'm doing wrong or if SAS doesn't let you do this.

proc sql noprint;
  create table xx as
  select *,
         prxposn(prxparse("/a/i"), 0, name) as xx
  from sashelp.class
  ;
quit;

Thanks Rob

EDIT: I know I could just do the PROC SQL and then do the regex in a datastep - I can get that working fine, I just want to know if it's possible to do it all in the PROC sql.

I don't think the documentation is particularly clear on the matter, but "the PRXPOSN function uses the results of PRXMATCH, PRXSUBSTR, PRXCHANGE, or PRXNEXT to return a capture buffer" and so you have to call one of those functions first, using the regular expression ID you generate via PRXPARSE, prior to calling PRXPOSN.

The following SAS code works for me on 9.1.3. Your intention is not entirely clear to me, but I'm assuming you want to capture the suffix starting from the first "a", and so I modified your regular expression accordingly:

proc sql; 
   create table xx as 
   select *, 
      prxparse("/a\w*/i") as re,
      ifc(
         prxmatch(calculated re, name), 
         prxposn(calculated re, 0, name), 
         " "
      ) as xx 
   from sashelp.class; 
quit; 

One downside to this approach (besides its decided lack of elegance) is that it adds an additional variable (re) to the output data set. The following sources were helpful to me in tracking down the behavior of PRXPOSN:

This may be different than what you want, but this will populate xx with the location of the first a in name :

proc sql noprint;
  create table xx as
  select *,
         prxmatch('/a/i', name) as xx
  from sashelp.class
  ;
quit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM