SAS输入来自.txt,输入跨越多行

时间:2013-01-18 02:11:39

标签: sas

大家。

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

假设我有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 

如果有人能提供帮助我提前感谢!

谢谢

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;