Hi Liz_Perkin (continued)
Instead of writing an intermidiate file, it would be nicer and more efficient to drop reading the file as a delimited file in an extra step and do the whole in one pass with scan functions. Notice that I changed the separator count to countc() and not count() in order to cope with missing values. The following gives the same result as test2 above + the file name.
* Read the file and use scan functions to split in variables;
* If 6 delimiters and not 5, change the third to # before splitting;
data test3;
length v1 v2 8 v3 v4 $40 v5 v6 8 filename fname $100;
infile 'c:\tmp\test.txt' lrecl=80 truncover filename=fname;
drop pos len;
input;
if countc(_infile_,',') = 6 then do;
call scan(_infile_,4,pos,len,',');
substr(_infile_,pos-1,1) = '#';
end;
v1 = input(scan(_infile_,1,','),??BEST5.);
v2 = input(scan(_infile_,2,','),??BEST5.);
v3 = scan(_infile_,3,',');
v4 = scan(_infile_,4,',');
v5 = input(scan(_infile_,5,','),??BEST5.);
v6 = input(scan(_infile_,6,','),??BEST5.);
filename = fname;
run;
... View more