2017-03-07 96 views
0
proc sql; 

select sum(counts) 
from (
     select count(*) "counts" from table1 
     union 
     select count(*) "counts" from table2 
    ); 
quit; 

我不斷收到下面的錯誤。我甚至嘗試刪除別名。爲什麼此SQL查詢不能在Base SAS中運行?

ERROR: The SUM summary function requires a numeric argument.
ERROR: The following columns were not found in the contributing tables: counts.

+0

我想那些是兩個不同的錯誤?你需要一個別名爲你的列, –

回答

0

在oracle中,如果你使用的標識符,你總是有那些引用列(S)identifiers.Of當然,如果罪名是不是保留字,你可以完全刪除它們。

select sum("counts") 
from (
     select count(*) "counts" from table1 
     union 
     select count(*) "counts" from table2 
    ); 
quit; 
+0

我不確定問題中的[tag:oracle]來自哪裏,但問題清楚地詢問了Base SAS;這在Base SAS中不起作用。 – Joe

3

在SAS中,標識符通常用作未引號。如果您想要標識符包含空格或其他特殊字符,您可以使用"identifier"N來指示引用的字符串是名稱("my counts"n下方)。

至於查詢,我會使用union all而不是union到正確總結計數。

proc sql; 
create table table1 (x num); 
create table table2 (x num); 
proc sql; 
select sum(counts) as "my counts"n 
from (
     select count(*) as counts from table1 
     union all 
     select count(*) from table2 
    ); 
quit; 

對於SAS表,您可以通過使用行號從SAS字典表這樣可以獲得更好的性能:

proc sql; 
select sum(nobs - delobs) as "my counts"n 
from dictionary.tables 
where libname = 'WORK' and memname in ('TABLE1', 'TABLE2'); 
quit; 
1

你的代碼是給內部COUNT(*)結果的標籤,但它沒有給他們一個變量名稱。

SAS PROC SQL可以讓你寫的代碼,你不給你的變量名,只是在做一個選擇,甚至做當SELECT *時。

select count(*) "Total number of rows" from sashelp.class ; 
select * from 
    (select count(*) "Label 1" from sashelp.class 
    union all 
    select count(*) "Label 2" from sashelp.class 
    ) 
; 

你甚至可以用它們來製作數據集,在這種情況下,SAS會生成一個變量名。試試這個:

create table test1 as 
    (select count(*) "Total number of rows" from sashelp.class) 
; 
describe table test1 ; 

但是如果你想使用的變量像SUM()表達式,那麼你必須給變量的名稱,而不是隻是一個標籤。否則,你不知道該把什麼放在聚合函數調用中。一般來說,給出你的派生變量名是個好主意。

select sum(counts) as total_count label="Total of count(*)" from 
    (select count(*) as counts from sashelp.class 
    union all 
    select count(*) as counts from sashelp.class 
    ) 
; 

你可以嘗試DQUOTE=ANSI選項添加到您的PROC SQL聲明。這會將"name"視爲變量名而不是字符串。但是您仍然需要在名稱前添加AS關鍵字。

proc sql dquote=ansi; 
select sum(counts) 
from (
    select count(*) as "counts" from sashelp.class 
    union 
    select count(*) as "counts" from sashelp.class 
    ) 
; 
quit; 
相關問題