2015-06-16 38 views
-2

我有一个循环的宏程序(for i in 1 to n)。随着每个i我有一个表与许多列 - 变量。在这些列中,我们有一个名为var(谁有3个可能的值:a和c)。检查数据集或列中是否存在值

因此,对于每个表i,我想检查他的列var是否存在值“c”。如果是的话,我想将这个表格导出为一张excel表格。否则,我将把这张表连接起来。

你能告诉我该怎么办?

+1

你好Quang,你会在这里发布你的代码,并带有示例数据和你以前试过的吗? –

+0

@VasilijNevlev:我直接在我的项目上试了一下,所以我不知道该如何发布。 –

回答

0

好了,在您的宏在步骤我,你必须做这样的事情

proc sql; 
select min(sum(case when var = 'c' then 1 else 0 end),1) into :trigger from table_i; 
quit; 

那么,你会得到宏观变量触发等于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; 
+0

你好,和函数返回'。'是正常的。如果table_i有0观察值? –

+0

它工作;谢谢 !我只是改变了一下代码。我只计算了SUM。如果> = 1,我会做出口。我不使用Min,因为如果数据有0个观察值,总和将返回'。'。 ,它会使min始终= 1. –

+0

以避免'。'在0观察的情况下,你可以编码max(min(...,1),0) - 所以你得到max(。,0)= 0 – burduk

0

不知道整个九院中的问题的代码的东西,我在危险地说,你可能并不需要宏观可言,如果你不介意出口到。 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;