SAS - 向后读取文件?

发布于 2024-08-10 21:42:36 字数 291 浏览 6 评论 0原文

我需要 SAS 读取许多大型日志文件,这些文件被设置为在底部包含最新的活动。我所需要的只是特定活动发生的最近时间,我想知道 SAS 是否可以跳过解析文件的(长)开始部分。

我在网上查找并找到了如何向后读取数据集,但这需要 SAS 首先将 .log 文件中的所有内容解析到数据集中。是否可以直接从最后开始读取文件,以便我可以在找到特定类型的最新活动时立即停止数据步骤?

我也阅读了 infile 和 firstobs 选项,但我不知道这些日志文件在解析之前需要多长时间,对吧?对我来说听起来像是第 22 条军规。那么我所描述的情况可行吗?

I need SAS to read many large log files, which are set up to have the most recent activities at the bottom. All I need is the most recent time a particular activity occurred, and I was wondering if it's possible for SAS to skip parsing the (long) beginning parts of the file.

I looked online and found how to read a dataset backwards, but that would require SAS to first parse everything in the .log file into the dataset first. Is it possible to directly read the file starting from the very end so that I can stop the data step as soon as I find the most recent activity of a particular type?

I read up on infile as well, and the firstobs option, but I have no idea how long these log files are until they are parsed, right? Sounds like a catch-22 to me. So is what I'm describing doable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜巴黎 2024-08-17 21:42:36

我可能会设置一个文件名管道语句来使用 tail -rtac 等操作系统命令以相反的顺序向 SAS 呈现文件。这样SAS就可以正常读取文件而不必担心文件有多长。

I'd probably set up a filename pipe statement to use an operating system command like tail -r or tac to present the file in reverse order to SAS. That way SAS can read the file normally and you don't have to worry about how long the file is.

看透却不说透 2024-08-17 21:42:36

如果您的意思是解析 sas 日志文件,我不确定在实践中向后读取日志文件是否值得。例如,以下代码在我的 PC 上执行时间不到十分之一秒,并且正在写入和读取 10,000 行日志文件。您的日志文件有多大?有多少个?同样如下所示,您不必“解析”每一行上的所有内容。您可以有选择地阅读该行的某些部分,如果这不是您要查找的内容,则可以转到下一行。

%let pwd = %sysfunc(pathname(WORK));
%put pwd=&pwd;
x cd &pwd;

/* test file. more than 10,000 line log file */
data _null_;
  file "test.log";
  do i = 1 to 1e4;
    r = ranuni(0);
    put r binary64.;
    if r < 0.001 then put "NOTE: not me!";
  end;
  put "NOTE: find me!";
  do until (r<0.1);
    r = ranuni(0);
    put r binary64.;
  end; 
  stop;
run;

/* find the last line that starts with
   NOTE: and get the rest of the line. */
data _null_;
  length msg $80;
  retain msg;
  infile "test.log" lrecl=80 eof=eof truncover;
  input head $char5. @;
  if head = "NOTE:" then input @6 msg $char80.;
  else input;
  return;
eof: 
  put "last note was on line: " _n_ ;
  put "and msg was: " msg $80.;
run;
/* on log
   last note was on line: 10013
   and msg was:  find me!
*/

If you mean parsing a sas log file, I am not sure if reading the log file backward is worth the trouble in practice. For instance, the following code executes less than a tenth of a second on my PC and it is writing and reading a 10,000 line log file. How big is your log files and how many are there? Also as shown below, you don't have to "parse" everything on every line. You can selectively read some parts of the line and if it is not what you are looking for, then you can just go to the next line.

%let pwd = %sysfunc(pathname(WORK));
%put pwd=&pwd;
x cd &pwd;

/* test file. more than 10,000 line log file */
data _null_;
  file "test.log";
  do i = 1 to 1e4;
    r = ranuni(0);
    put r binary64.;
    if r < 0.001 then put "NOTE: not me!";
  end;
  put "NOTE: find me!";
  do until (r<0.1);
    r = ranuni(0);
    put r binary64.;
  end; 
  stop;
run;

/* find the last line that starts with
   NOTE: and get the rest of the line. */
data _null_;
  length msg $80;
  retain msg;
  infile "test.log" lrecl=80 eof=eof truncover;
  input head $char5. @;
  if head = "NOTE:" then input @6 msg $char80.;
  else input;
  return;
eof: 
  put "last note was on line: " _n_ ;
  put "and msg was: " msg $80.;
run;
/* on log
   last note was on line: 10013
   and msg was:  find me!
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文