如何在SAS中的字符串中仅保留特定的日期模式?

时间:2016-02-25 14:32:50

标签: sas

我有一个包含单词,数字和日期的文本字符串(格式为mm / dd / yy),并且只想保留日期值。

到目前为止,使用compress函数我可以保留所有数字和“/”字符:

data _null_;
    string = 'text 2342 12/11/15 text 54 11/01/14 49 text 10/23/16 423';
    parsed = compress(string, '0123456789/ ', 'k');
    put parsed;
run;

返回: 12/11/15 54 11/01/14 49 10/23/16 423

我想要的是:12/11/15 11/01/14 10/23/16

我该如何做到这一点?

1 个答案:

答案 0 :(得分:2)

(改编自SAS'关于CALL PRXNEXT例程的文档)

data dates(keep = Text Dates);
   ExpressionID = prxparse('/\d{2}\/\d{2}\/\d{2}/');
   text = 'text 2342 12/11/15 text 54 11/01/14 49 text 10/23/16 423';
   length Dates $ 120;
   start = 1;
   stop = length(text);
      /* Use PRXNEXT to find the first instance of the pattern, */
      /* then use DO WHILE to find all further instances.       */
      /* PRXNEXT changes the start parameter so that searching  */
      /* begins again after the last match.                     */
   call prxnext(ExpressionID, start, stop, text, position, length);
      do while (position > 0);
         found = substr(text, position, length);
         put found= position= length=;
         Dates = catx(" ", dates, found);
         call prxnext(ExpressionID, start, stop, text, position, length);
      end;
run;

结果数据集:

resulting dataset