背景是我需要使用filename命令执行grep并将结果用作输入。
这是我的输入数据集,名为 test
firstname lastname filename
<blank> <blank> cus_01.txt
<blank> <blank> cus_02.txt
文件名值是我需要grep的实际文件,因为我需要这些文件中的某些字符串来填充名字和姓氏
代码如下:
data work.test;
set work.test;
call symputx('file', filename);
filename fname pipe "grep ""Firstname"" <path>/&file.";
filename lname pipe "grep ""Lastname"" <path>/&file.";
infile fname;
input firstname;
infile lname;
input lastname;
run;
但是,直到完成数据步骤过程之后,才能使用在数据步骤内部创建的宏变量。所以,这就是&file。无法解析,无法在文件名中使用。
是否可以解析宏变量?
谢谢!
答案 0 :(得分:2)
这未经测试。您需要使用INFILE语句选项FILEVAR。
data test;
input (firstname lastname filename) (:$20.);
cards;
<blank> <blank> cus_01.txt
<blank> <blank> cus_02.txt
;;;;
run;
data work.grep;
set work.test;
length cmd $128;
cmd = catx(' ','grep',quote(strip(firstname)),filename);
putlog 'NOTE: ' cmd=;
infile dummy pipe filevar=cmd end=eof;
do while(not eof);
input;
*something;
output;
end;
run;
答案 1 :(得分:0)
如果您有许多客户文件,则使用管道到grep可能是一项昂贵的操作系统操作,并且在SAS服务器上可能被禁止使用(管道,x,系统等)
您可以使用infile
和filename=
通配符功能在一个数据步骤中读取所有以模式命名的文件,以捕获正在读取的活动文件。
示例:
%let sandbox_path = %sysfunc(pathname(WORK));
* create 99 customer files, each with 20 customers;
data _null_;
length outfile $125;
do index = 1 to 99;
outfile = "&sandbox_path./" || 'cust_' || put(index,z2.) || '.txt';
file huzzah filevar=outfile;
putlog outfile=;
do _n_ = 1 to 20;
custid+1;
put custid=;
put "firstname=Joe" custid;
put "lastname=Schmoe" custid;
put "street=";
put "city=";
put "zip=";
put "----------";
end;
end;
run;
* read all the customer files in the path;
* scan each line for 'landmarks' -- either 'lastname' or 'firstname';
data want;
length from_whence source $128;
infile "&sandbox_path./cust_*.txt" filename=from_whence ;
source = from_whence;
input;
select;
when (index(_infile_,"firstname")) topic="firstname";
when (index(_infile_,"lastname")) topic="lastname";
otherwise;
end;
if not missing(topic);
line_read = _infile_;
run;