簡體   English   中英

當表被覆蓋時,如何在proc sql中創建此case語句並追加?

[英]How do I create this case statements in proc sql and append when tables are overwritten?

我花了幾個月的時間進行數據挖掘,並創建了一種技術來查找公司的信息。 我很難將數據放入有關如何在SAS中匯總信息的適當且有價值的演示文稿中。 我有3個問題。

1)如何在proc sql中說“如果matched_by_t2> b2_c2,則添加新列“ no bueno”

2)有沒有辦法讓“((BC_C2 / original_count)%”)我怎么插入那個百分號

3)同樣,如果我多次運行此查詢以獲取數據集列表,那么每次創建新表並覆蓋新表時,如何獲取那些名稱相同的“ e_data_unmatched”追加的新表呢?運行表會被覆蓋,因此我想確保每次循環運行時,新記錄都會追加到表中而不是覆蓋。 1 http://imgur.com/bzLefXy

謝謝!

    proc sql;
create table wanted as
select t1.occurences as original_count
      ,t2.occurences as matched_by_T1
      ,t3.occurences as matched_by_T2
      ,t2.occurences+t3.occurences as B2_C2
      ,t4.occurences as not_matched
      ,t5.occurences as matched_by_t2
from (select count(*) as occurences from query_for_reports1) t1
    ,(select count(*) as occurences from query_for_reports1 where edsys is not null) t2
    ,(select count(*) as occurences from e_data_unmatched where ip is not null) t3
    ,(select count(*) as occurences from WORK.E_DAT_UNMATCHED where IpS= .) t4
    ,(select count(*) as occurences from work.Append_table13) t5
;
quit;
1) Add new column "no bueno" in sql set value based on conditiong " If matched_by_t2 > b2_c2" 
2) concatenate percentage sign
 proc sql;
create table wanted as
select t1.occurences as original_count
      ,t2.occurences as matched_by_T1
      ,t3.occurences as matched_by_T2
      ,t2.occurences+t3.occurences as B2_C2
      ,t4.occurences as not_matched
      ,t5.occurences as matched_by_t2,
case when t3.occurences> t5.occurences then 0 
else 1 end as no_bueno,
 CAST(((t2.occurences+t3.occurences)/ t1.occurences) as nvarchar(5)) +'%'
from (select count(*) as occurences from query_for_reports1) t1
    ,(select count(*) as occurences from query_for_reports1 where edsys is not null) t2
    ,(select count(*) as occurences from e_data_unmatched where ip is not null) t3
    ,(select count(*) as occurences from WORK.E_DAT_UNMATCHED where IpS= .) t4
    ,(select count(*) as occurences from work.Append_table13) t5
;
quit;

要建立您的查詢並嘗試回答所有三個問題:

1)如果在條件為真時想要一個包含“ no bueno”值的新列,則將以下列添加到您的select子句中:

case 
  when matched_by_t2>t2.occurences+t3.occurences then "no bueno"
end as new_column_1

但是,如果您想要的是不帶符號的列中的標志類型值,則可以使用@Trushna的建議

2)有兩種方法可以做到這一點。 您可以將值存儲為字符變量,並在其中硬編碼百分號:

catx(' ',(t2.occurences+t3.occurences)/t1.occurences,'%') as new_column_2

或者您可以將值存儲為數字變量,並使用以下格式顯示為百分號:

(t2.occurences+t3.occurences)/t1.occurences as new_column_2 format=percent.

3)每次使用此查詢將輸出添加到表的一種方法是使用insert into查詢而不是create table

proc sql;
insert into wanted
select [...]
;
quit;

切記,為了成功insert into表,必須存在該表。 在您的情況下,您將不得不第一次使用create table語法,然后在以后每次使用insert into syntax

我必須指出,盡管我不知道您的工作流程或執行此操作的上下文,但是您嘗試實現此目標的方式似乎不是很理想,您可能需要考慮重新考慮您的流程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM