2015-06-16 37 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;