【问题标题】:Keep reading input from next row in the same variable继续从同一变量中的下一行读取输入
【发布时间】:2015-08-04 11:13:33
【问题描述】:

我有来自聊天的数据,当时我想在一个条目中读取。每次一个人点击“发送”应该是一个观察。问题是文本中有中断(输入)时。我无法让 SAS 继续将其视为相同的观察结果。这是一些虚拟数据:

   08:23 - Greg: Hi!
   08:24 - Sue: Hello
   08:24 - Greg: How are you?
   08:25 - Sue: Just fine :)

   How are you then?
   08:26 - Greg: All good. 

我希望这是 5 个观察值,但我只能管理 SAS 将其读取为 7 个 obs。所需的数据集应如下所示:

Obs   VAR1
1    08:23 - Greg: Hi!
2    08:24 - Sue: Hello
3    08:24 - Greg: How are you?
4    08:25 - Sue: Just fine :) How are you then?
5    08:26 - Greg: All good. 

我玩弄代码:

data testing;
infile datalines ;
input var1 $60. ;
datalines;
08:23 - Greg: Hi!
08:24 - Sue: Hello
08:24 - Greg: How are you?
08:25 - Sue: Just fine :)

How are you then?
08:26 - Greg: All good. 
;

但实际文件是 txt 并且比上面的虚拟示例有更多的不规则性。我试图使用尾随 @ 但无法让它以我想要的方式工作。也许尾随@不是我所追求的。任何建议如何进行?

【问题讨论】:

  • 您是否需要将其从输入中完美格式化,或者您也可以对字符串进行后处理以获得所需的结果?
  • 后期处理很好,我一直在想。当我阅读可能轻松解决这个问题的数据时,只是感觉我错过了一些东西。
  • 您能否验证换行与发送是如何存储在您的文件中的?有时像这样的文件使用'0A'x (LF) 进行内部换行,而'0D0A'x 仅在发送之后使用。
  • 似乎只是LF中断。好想你,它本来可以让事情变得更容易。

标签: sas


【解决方案1】:

试试这个。

保留一个作为最后一个值的运行变量。如果当前值在前 4 个字符中有时间戳,则将其输出并将值重置为“”。将当前值附加到运行变量。最后,无论如何输出最后一行。

data testing(keep=line);
set testing end=last;

format line $2000.;
retain line;

if _n_ > 1 then do;
    if index(substr(var1,1,4),":") then do;
        output;
        line = "";
    end;
end;

put line= var1=;
line = catx(" ",line , var1);
put line=;

if last then do;
    output;
    put "AT LAST";
end;
run;

【讨论】:

  • 如果有这样的聊天怎么办:15:00 - 我:这是一个简单的字符串,这里换行[换行] 所以:这个标点符号会导致错误的换行15:01 - 你:你说得对,我的代码不起作用。
  • 如果有人按回车键并键入看起来像时间戳的内容,您的会出现同样的问题。您将无法围绕该问题编写代码。
  • 确实如此,但请尝试使用我的代码,'so:' 不会产生错误的分类,但是是的,如果在按回车键后他们用 hhmm 格式写了一个小时,显然我的代码会有同样的不良行为。
  • 您是否能够插入自己的标记来识别消息的开始/结束位置,或者您是否从已经提供给您的文件中导入?提供文件的人可以为您插入令牌吗?这将允许您围绕该问题编写代码。
  • 我会使用 ??TIME5 读取前 5 个字符。通知并测试它是否为非空值,因此具有冒号但不是有效时间的值不会触发新条目。您还可以测试第 7 个字符是否为连字符。
【解决方案2】:

我尝试在行数据输入中找到解决方案没有成功,无论如何我希望这对你有用,后处理字符串:

data testing;
infile datalines ;
input var1 $60.;
datalines;
08:23 - Greg: Hi!
08:24 - Sue: Hello
08:24 - Greg: How are you?
08:25 - Sue: Just fine :)

How are you then?
08:26 - Greg: All good. 
;

data testing01;
set testing;
retain row 0;
if input(substr(var1,1,2),8.) le 24 and input(substr(var1,1,2),8.) ne .
and substr(var1,3,1)=':' 
and input(substr(var1,4,2),8.) le 59 and input(substr(var1,4,2),8.) ne . then row = row+1; else row=row;
run;

proc transpose data=testing01 out=testing02;
var var1;
by row;
run;

data testing03;
length final $2000;
set testing02;
array str[*] col:;
do i=1 to dim(str);
if str[i] ne '' then final=cats(strip(final)||' '||strip(str[i]));
end; 
drop col: row i _name_;
run;

【讨论】:

    【解决方案3】:
    filename FT15F001 temp;  
    data testing ;
    infile FT15F001 end=eof ;
    length string $6323;
    retain string;
    input @;
    if _n_=1 then string=_infile_;
    else if not missing(_infile_) and anydigit(_infile_)^=1 then string=catx(' ',string,_infile_);
    else if not missing(_infile_) and anydigit(_infile_)=1 then do;
       output;
       call missing(string);
       string=_infile_;
    end;
    if eof then output;
    PARMCARDS;
    08:23 - Greg: Hi!
    08:24 - Sue: Hello
    08:24 - Greg: How are you?
    08:25 - Sue: Just fine :)
    
    How are you then?
    08:26 - Greg: All good. 
    ;
    

    【讨论】:

      【解决方案4】:

      有很多方法可以做到这一点,具体取决于您的特定用例。

      这是一个正则表达式。如果您的总字符数 > 32767,这将不起作用,除非您有某种方法将其拆分为块,但对于较小的文件效果很好;即使一次读一行也可以使用通用方法。

      data test;
      infile "c:\temp\chat.txt" recfm=f lrecl=32767;
      input @;
      rx_find = prxparse('~(\d\d:\d\d -.*?)(?=(?:\b\d\d:\d\d)|$)~ios');
      rc_find = prxmatch(rx_find,_infile_);
      pos=1;
      pos2=0;
      start=1;
      call prxposn(rx_find,1,pos,len);
      do until (pos2=0);
          call prxposn(rx_find,1,pos,len);
          found=substr(_infile_,pos,len);
          output;
          start=pos+len;
          call prxnext(rx_find,start,-1,_infile_,pos2,len2);
      end;
      stop;
      run;
      

      【讨论】:

      • 有趣的解决方案。这些 prx 功能对我来说是新的。似乎它们可能有用。我的文件很大,所以我必须对此解决方案进行一些修改。
      猜你喜欢
      • 1970-01-01
      • 2021-04-02
      • 2020-07-31
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      相关资源
      最近更新 更多