【问题标题】:split a string using a word delimiter in sas在 sas 中使用单词分隔符拆分字符串
【发布时间】:2016-03-24 23:03:52
【问题描述】:

我正在寻找一种使用此分隔符拆分很长字符串的方法:'| '

扫描功能似乎不接受单词分隔符,所以如果我接受的话

scan(string,3,'| ')

它将在每个| 和空格处拆分
而不是像我需要的那样每个'| '

在文档中,我没有看到任何允许这样做的修饰符。 http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000214639.htm

【问题讨论】:

  • 我刚刚有了一个想法:用transwrd将| 替换为_,然后使用scan
  • 从扫描功能中的分隔符中删除空格,它应该可以正常工作。如果你得到一个额外的空间,使用条带或修剪来删除前导空格。

标签: string sas


【解决方案1】:

INFILE 具有 DLMSTR,当与 infile 魔法结合使用时,可以完全满足您的需求。你的 transwrd 想法应该很有效。

data test;
   input string $50.;
   cards4;
This|is | pipe space| delmimited
This| is| too I believe
;;;;
   run;
data test2;
   infile cards missover dlmstr='| ';
   if _n_ eq 1 then input @;
   array w[5] $64;
   do while(not eof);
      set test end=eof;
      _infile_ = string;
      input @1 w[*] @;
      output;
      end; 
   stop;
   cards;
Necessary evil
   run;

【讨论】:

    【解决方案2】:

    我自己发现的

    使用tranwrd'| ' 替换为'_'

    newstring=tranwrd(string,'| ','_');
    

    然后我就可以正常使用扫描功能了

    xxx=scan(newstringstring,3,'_');
    

    【讨论】:

    • 您可能需要考虑一个更模糊的替换字符串。 'FF'X
    • 好主意,我发现 _ 已经在字符串中,我切换到 %,但会尝试你的想法
    猜你喜欢
    • 2011-09-11
    • 1970-01-01
    • 2013-06-28
    • 2022-01-13
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    相关资源
    最近更新 更多