Reputation: 853
everyone.
I have a question that is driving me crazy.
Say I have 2 text files that look like this:
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
I would like to input both of them into a dataset named test and take the following form:
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
I appreciate it ahead of time if anyone can help!
Thanks
Upvotes: 1
Views: 1349
Reputation: 63424
You don't have to do it quite as complicatedly as three datasteps (especially if you're going to do N files). It's pretty easy, really. Use the EOV indicator (End of Volume) to see when you're at the start of a new file [EOV is tripped after ending a volume/file] and each time you're at the start of a new file, read the name and filename in the first two lines.
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;
Upvotes: 1
Reputation: 21
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;
Upvotes: 0