检查数据集或列中是否存在值
Check if a value exists in a dataset or column
我有一个带循环的宏程序(for i in 1 to n)
。每个 i
我有一个 table 有很多列 - 变量。在这些列中,我们有一个名为 var
的列(它有 3 个可能的值:a b 和 c)。
所以对于每个 table i
,我想检查他的列 var
是否存在值 "c"。如果是的话,我想把这个table导出成excel的sheet。否则,我会将此 table 与其他人连接起来。
你能告诉我该怎么做吗?
好的,在第 i 步的宏中,您必须执行类似的操作
proc sql;
select min(sum(case when var = 'c' then 1 else 0 end),1) into :trigger from table_i;
quit;
然后,你会得到宏变量 trigger 如果你必须做出口,你会得到等于 1,如果你必须做连接,你会得到 0。接下来,你必须编写这样的代码
%if &trigger = 1 %then %do;
proc export data = table_i blah-blah-blah;
run;
%end;
%else %do;
data concate_data;
set concate_data table_i;
run;
%end;
在不知道你问题的全部九码的情况下,如果你不介意导出为 .CSV 而不是原生 xls 或 xlsx,我可能会说你可能根本不需要宏。恕我直言,如果你这样做 'Proc Export',这意味着你无论如何都不能嵌入花哨的格式,你最好在大多数设置中只使用 .CSV。如果您需要包含列标题,则需要利用元数据(字典表)并添加几行。
filename outcsv '/share/test/'; /*define the destination for CSV, change it to fit your real settings*/
/*This is to Cat all of the tables first, use VIEW to save space if you must*/
data want1;
set table: indsname=_dsn;
dsn=_dsn;
run;
/*Belowing is a classic 2XDOW implementation*/
data want;
file outcsv(test.csv) dsd; /*This is your output CSV file, comma delimed with quotes*/
do until (last.dsn);
set want1;
by dsn notsorted; /*Use this as long as your group is clustered*/
if var='c' then _flag=1; /*_flag value will be carried on to the next DOW, only reset when back to top*/
end;
do until (last.dsn);
set want1;
by dsn notsorted;
if _flag=1 then put (_all_) (~); /*if condition meets, output to CSV file*/
else output; /*Otherwise remaining in the Cat*/
end;
drop dsn _flag;
run;
我有一个带循环的宏程序(for i in 1 to n)
。每个 i
我有一个 table 有很多列 - 变量。在这些列中,我们有一个名为 var
的列(它有 3 个可能的值:a b 和 c)。
所以对于每个 table i
,我想检查他的列 var
是否存在值 "c"。如果是的话,我想把这个table导出成excel的sheet。否则,我会将此 table 与其他人连接起来。
你能告诉我该怎么做吗?
好的,在第 i 步的宏中,您必须执行类似的操作
proc sql;
select min(sum(case when var = 'c' then 1 else 0 end),1) into :trigger from table_i;
quit;
然后,你会得到宏变量 trigger 如果你必须做出口,你会得到等于 1,如果你必须做连接,你会得到 0。接下来,你必须编写这样的代码
%if &trigger = 1 %then %do;
proc export data = table_i blah-blah-blah;
run;
%end;
%else %do;
data concate_data;
set concate_data table_i;
run;
%end;
在不知道你问题的全部九码的情况下,如果你不介意导出为 .CSV 而不是原生 xls 或 xlsx,我可能会说你可能根本不需要宏。恕我直言,如果你这样做 'Proc Export',这意味着你无论如何都不能嵌入花哨的格式,你最好在大多数设置中只使用 .CSV。如果您需要包含列标题,则需要利用元数据(字典表)并添加几行。
filename outcsv '/share/test/'; /*define the destination for CSV, change it to fit your real settings*/
/*This is to Cat all of the tables first, use VIEW to save space if you must*/
data want1;
set table: indsname=_dsn;
dsn=_dsn;
run;
/*Belowing is a classic 2XDOW implementation*/
data want;
file outcsv(test.csv) dsd; /*This is your output CSV file, comma delimed with quotes*/
do until (last.dsn);
set want1;
by dsn notsorted; /*Use this as long as your group is clustered*/
if var='c' then _flag=1; /*_flag value will be carried on to the next DOW, only reset when back to top*/
end;
do until (last.dsn);
set want1;
by dsn notsorted;
if _flag=1 then put (_all_) (~); /*if condition meets, output to CSV file*/
else output; /*Otherwise remaining in the Cat*/
end;
drop dsn _flag;
run;