在 SAS 中使用 space 输入字符串

Input string with space in SAS

我正在将字符串输入到 SAS 中的数据集中。这是我的代码。

data match;
input string1 -6
      string2 -15
      string3 -21;
position1=prxmatch('/^a/', string1);
position2=prxmatch('/a$/', string2);
position3=prxmatch('/^a.a$/', string3);
datalines;
abc      aba     aa
abcde    abcba   abba
 abcde   aaaaa   a.a
                 a$a
;
run;

您可能会注意到,第一列 abcde 的最后一行以 space 开头。此外,第二列的最后一个字符串 aaaaa 也以 space 结尾。除了这两个,其他的字符串就是这样。

我正在使用 prxmatch 进行正则表达式匹配。所以 prxmatch('/^a/', ' abcde') 应该 return 我 0,prxmatch('/a$/', 'aaaaa ') 也应该 return 我 0。

然而,结果是这样的:

那么,如何正确输入这些字符串,得到正确的正则表达式匹配结果呢?

您可以使用$CHARw. informat 读取初始空格。我更喜欢 @pos 那样阅读:

data match;
input @1 string1 $char6.
      @10 string2  $char6.
      @18 string3  $char4.;
  position1=prxmatch('/^a/', string1);
  position2=prxmatch('/a$/', string2);
  position3=prxmatch('/^a.a$/', string3);
datalines;
abc      aba     aa
abcde    abcba   abba
 abcde   aaaaa   a.a
                 a$a
;
run;

我认为现在 returns 如您所愿。 (您可能希望 STRING3 只有 3 个宽度?)

如果要在每列的开头或结尾允许空格,请使用 \s*:

position1=prxmatch('/^\s*a/', string1);
position2=prxmatch('/a\s*$/', string2);
position3=prxmatch('/^a.a$/', string3);