类似GREP的函数,用于检索SAS中的文本

sta*_*uyz 3 string substring sas

我想检索SAS文件中列中的特定文本.

该文件将如下所示:

Patient    Location    infoTxt
001        B           Admission Code: 123456 X
                       Exit Code: 98765W
002        C           Admission Code: 4567 WY
                       Exit Code: 76543Z
003        D           Admission Code: 67890 L
                       Exit Code: 4321Z
Run Code Online (Sandbox Code Playgroud)

我想只检索排序代码和退出代码的冒号之后的信息,并将它们放在各自的列中."代码"可以是字母,数字和空格的任意组合.新数据如下所示:

Patient    Location    AdmissionCode      ExitCode
001        B           123456 X            8765W
002        C           4567 WY             76543Z
003        D           67890 L             4321Z
Run Code Online (Sandbox Code Playgroud)

我不熟悉SAS中的功能,但逻辑可能如下所示:

data want;
  set have;
  do i = 1 to dim(infoTxt)

    AdmissionCode = substring(string1, regexpr(":", string) + 1);
    ExitCode = substring(string2, regexpr(":", string) + 1);

run;
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,string1表示infoTxt中的第一行文本,string2表示第二行文本infoTxt.

Ric*_*ard 7

SAS可以通过一系列函数开始使用Perl正则表达式PRX.该提示表是如果你熟悉正则表达式大汇总.

PRXMATCH并且PRXPOSN可以使用捕获组测试正则表达式模式并检索组文本.

data have;
input;
text = _infile_;
datalines;
Admission Code: 123456 X Exit Code: 98765W
Admission Code: 4567 WY Exit Code: 76543Z
Admission Code: 67890 L Exit Code: 4321Z
run;

data want;
  set have;

  if _n_ = 1 then do;
    retain rx;
    rx = prxparse ('/Admission Code: (.*)Exit Code:(.*)/');
  end;

  length AdmissionCode ExitCode $50;

  if prxmatch(rx,text) then do;
    AdmissionCode = prxposn(rx, 1, text);
    ExitCode = prxposn(rx, 2, text);
  end;

  drop rx;
run;
Run Code Online (Sandbox Code Playgroud)


dat*_*ll_ 5

我和下一个人一样喜欢带有捕获缓冲区的 RegEX,但您也可以使用输入语句功能来读取这些数据。

data info;
   infile cards n=2 firstobs=2;
   input #1 patient:$3. location :$1. @'Admission Code: ' AdmissionCode &$16. #2 @'Exit Code: ' ExitCode &$16.;
   cards;
Patient    Location    infoTxt
001        B           Admission Code: 123456 X
                       Exit Code: 98765W
002        C           Admission Code: 4567 WY
                       Exit Code: 76543Z
003        D           Admission Code: 67890 L
                       Exit Code: 4321Z
;;;;
   run;
proc print;
   run;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明