FILEVAR helps you because it allows you to arbitrarily point an INFILE or FILE statement to a value defined as a data step variable (as opposed to a string in code).
Your grep command doesn't really make sense to me, because it seems like you're not using SAS the way it's intended to be used. SAS is quote good at reading from files/strings/etc., and doesn't need help from grep; in fact you can use regular expressions if you wish ('perl regular expressions SAS' would be a good search, or, 'PRXMATCH' etc. functions).
If you have a filename in a variable, and want to read some information from that file, then all you need is (assuming 'have' is your dataset with filenames, stored in a variable named 'file_name_variable'). 'a' in the infile statement is a meaningless token that has to be there but doesn't do anything.
data want;
set have;
infile a filevar=file_name_variable;
input lname $ fname $;
run;
Assuming they're in that order (last name then first name, space separated). I imagine they're not space separated, but you haven't clarified that.
Now, if this is a JSON file, you have some better ways to do this (including using the JSON Libname ), but one way is:
data want;
set have;
infile a filevar=file_name_variable;
input @'"FirstName":' fname $
@'"LastName":' lname $
;
run;
This basically uses @-string matching to find the string that matches '"FirstName":' and then reads the characters after that into fname. You might need to do further processing to make sure it terminates at the next " character, or read the file as quotation-delimited, or something else, but it's possible. Do an internet search on "SAS JSON read" and you'll get a lot of results.
Now, if you have a more complex question - such as, those files don't necessarily have 'first name' and 'last name' in a consistent order - you certainly could use a regular expression to solve your problem (again, you probably don't need to, but who knows).
data want;
set have;
infile a filevar=file_name_var;
input @;
_prx_fname = prxparse('~"FirstName":(".*")~ios');
_prx_lname = prxparse('~"LastName":(".*")~ios');
if prxmatch(_prx_fname,_infile_) then
fname = prxposn(_prx_fname,1,_infile_);
if prxmatch(_prx_lname,_infile_) then
lname = prxposn(_prx_lname,1,_infile);
run;
Basically, defining two regexps (one for each of fname/lname), then using PRXMATCH to test if they return a match, and if so, then using PRXPOSN to return the value from the parentheses (capture buffer).
I'm assuming in the latter that this is a JSON file, hence the quotation marks; you'll need to make the right regexp for your data here (and I didn't test this, so my PRX may be a bit rusty, but if it doesn't work feel free to come back with your code.)
... View more