Hi rcsutherland
I don't know what your customer files actually look like, but I suppose you have a good reason for wanting to extract name lines using GREP instead of reading the files and extracting the names in SAS.
It is not easy to get your code to work as intended. So I suggest another approach. It is just an example to show the idea, and the grep result is probably more difficult to read in real life.
Test input istwo files in folder /sasdata/user/sasbatch/erlu names cus_01 and cus_o2 with content like this:
Firstname Svend Lastname Olsen
First step is to use grep to extract all first and last names from all files in the directory. The output from grep is 4 lines:
/sasdata/user/sasbatch/erlu/cus_01:Firstname Svend /sasdata/user/sasbatch/erlu/cus_01:Lastname Olsen /sasdata/user/sasbatch/erlu/cus_02:Firstname Erik /sasdata/user/sasbatch/erlu/cus_02:Lastname Larsen
filename lfname pipe "grep 'Firstname\|Lastname' /sasdata/user/sasbatch/erlu/*";
data work.grepresult;
length filename $100 type $10 name $60;
infile lfname;
input;
filename = scan(_infile_,1,':');
type = scan(_infile_,2,': ');
name = scan(_infile_,3,': ');
run;
Data from this step:
Next step is to make one record pr. file with Firstname and lastname. I use a full join to handle missing values in a simple way.
proc sql;
create table work.custnames as
select
a.name as Firstname,
b.name as Lastname,
coalesce(a.filename,b.filename) as filename
from work.grepresult (where=(type = 'Firstname')) as a
full outer join work.grepresult (where=(type = 'Lastname')) as b
on a.filename = b.filename;
quit;
The result looks like your test table with names filled in:
If you actual data has other variables, You could join on filename.
... View more