1

每个人。

我有一个让我发疯的问题。

假设我有 2 个如下所示的文本文件:

File_one.txt:

Name_sample_f1       *spans one line 
File_sample_f1       *spans one line 
String_sample_f1     *spans multiple, varying lines until the end of the file 
String_sample_f1 

File_two.txt:

Name_sample_f2       *spans one line 
File_sample_f2       *spans one line 
String_sample_f2     *spans multiple, varying lines until the end of the file 
String_sample_f2 
String_sample_f2 
String_sample_f2 

我想将它们都输入到名为test的数据集中并采用以下形式:

    Name             File             String
    ----             ----             ------
1   Name_sample_f1   File_sample_f1   String_sample_f1 
                                      String_sample_f1 
2   Name_sample_f2   File_sample_f2   String_sample_f2 
                                      String_sample_f2 
                                      String_sample_f2 
                                      String_sample_f2 

如果有人可以提供帮助,我会提前感谢!

谢谢

4

2 回答 2

1

您不必像三个数据步骤那样复杂地完成它(特别是如果您要处理 N 个文件)。这很容易,真的。使用 EOV 指示器(卷尾)查看您何时处于新文件的开头 [EOV在结束卷/文件跳闸] 并且每次您处于新文件的开头时,请阅读名称和文件名在前两行。

data test;
format name filename $100.;
retain name filename line;
infile '("c:\temp\file1.txt", "c:\temp\file2.txt")' eov=end lrecl=100 pad truncover; *or use wildcards, like infile "c:\temp\file*.txt";
input a $ @;
put _all_;
if (_n_=1) or (end=1) then do;
  end=0;
  line=1;
end;
else line+1;
if line=1 then do;
  input @1 name $100.;
end;
else if line=2 then do;
  input @1 filename $100.;
end;
else do;
  input @1 string $100.;
  output;
end;
run;
于 2013-01-18T16:56:58.163 回答
0
filename file1 'testfile1.txt';
filename file2 'testfile2.txt';

DATA file1;
LENGTH thisname thisfile thistext $ 200;
RETAIN thisname thisfile;
linecounter=0;
DO UNTIL(eof);
  INFILE file1 end = eof;
  INPUT;
  linecounter+1;
  IF (linecounter eq 1) THEN thisname=_infile_;
  ELSE IF (linecounter eq 2) then thisfile=_infile_;
  ELSE DO;
     thistext=_infile_;
     output;
  END;
END;
RUN;


DATA file2;
LENGTH thisname thisfile thistext $ 200;
RETAIN thisname thisfile;
linecounter=0;
DO UNTIL(eof);
  INFILE file2 end = eof;
  INPUT;
  linecounter+1;
  IF (linecounter eq 1) THEN thisname=_infile_;
  ELSE IF (linecounter eq 2) then thisfile=_infile_;
  ELSE DO;
     thistext=_infile_;
     output;
  END;
END;
RUN;

DATA all_files;
SET file1 file2;
RUN;

PROC PRINT DATA=all_files; RUN;
于 2013-01-18T08:45:11.020 回答