作为一个更大项目的一部分,我试图在多个表中搜索公共列。但是我不知道表的数量、列的数量或列的确切名称,因此我无法对计数或名称进行硬编码。我的主要问题是我试图从列表中的长列名称中搜索关键字,例如使用“电子邮件”的 Your_Email_Address。目前,我只能通过单独列出每个“NAME”搜索功能来使列搜索工作(这对此不可行)。我尝试了多种
where NAME LIKE/contains(IN(&OPTIONS.))
的变体,但没有成功。有谁知道如何正确组合 WHERE
函数,以便从 &OPTIONS
列表中成功搜索并选择包含关键字的列名称?到目前为止我的代码片段是:
options mlogic mprint symbolgen;
%*LOOP;
%macro JOINWRAP;
%let OPTIONS = USERNAME
EMAIL
AGE
ETC...
;
%let dataset1 = A7;
%let dataset2 = Subset;
%let OPTIONS_count = %sysfunc(countw(&OPTIONS.));
%*read columns into macro variables;
%do j = 1 %to 2;
proc sql ;
select NAME into :CLMNS&j SEPARATED BY ", " /*reading columns from each proc contents
into a seperate macro variable, so clmns1 = columns from A7*/
from work.COLS&j /*cols1 = A7 PROC CONTENTS previously defined */
where NAME CONTAINS "%scan(&OPTIONS.,1)" or NAME contains "%scan(&OPTIONS.,2)"
;
quit;
%put &&CLMNS&j;
%end;
...
run;
/* %end; */
%mend JOINWRAP;
%JOINWRAP;
我假设你正在尝试做类似的事情:
data want;
set contents;
where upcase(name) contains 'EMAIL'
or upcase(name) contains 'USERNAME'
;
run;
如果要搜索的子字符串列表是不变的,只需对其进行硬编码。
如果它发生变化,那么使用 DATA 步骤来完成它比尝试强制 PROC SQL 和/或宏语言来完成工作要容易得多。
所以像这样:
%let wordlist=EMAIL USERNAME;
data want;
set contents;
found=0;
do index=1 to countw("&wordlist") until (found);
if index(upcase(name),scan("&wordlist",index)) then found=1;
end;
if found;
run;