2017-04-19 72 views
0

我有一個數據集設置空值的情況下,當在PROC SQL

Period Brand ID 
    Jan A  X1 
    Jan A  K1 
    Jan B  CT2 
    Feb C  X2 
    Feb A  P4 

我願做ID的重複計數每個週期下各個品牌。我嘗試了在proc sql中使用CASE WHEN來計算每個時期的不同數量,但我不確定爲其他部分設置了什麼,因爲我猜測SAS也會將其他部分計算爲不同的項目。我的代碼如下:

proc sql; 

    create table items as 
    select period, 
    count(distinct case when brand="A" then ID else "." end) as Count_A, 
    count(distinct case when brand="B" then ID else "." end) as Count_B, 
    count(distinct case when brand="C" then ID else "." end) as Count_C 
from Data 
group by period; 
quit; 

我不明白,我可以使用子查詢來構建每個計數變也,但代碼很可能會變得非常冗長乏味。

謝謝!

回答

0

我認爲只要您使用NULL作爲ELSE條件,您的查詢就可以正常工作。您可以通過簡單地不列出任何ELSE條件來達到此目的,在這種情況下默認情況下會使用NULL

select 
    period, 
    count(distinct case when brand = "A" then ID end) as Count_A, 
    count(distinct case when brand = "B" then ID end) as Count_B, 
    count(distinct case when brand = "C" then ID end) as Count_C 
from Data 
group by period 
0

這應該爲你工作: -

/*Creating Dataset*/ 
data a; 
input Period $3. Brand$1. [email protected]; 
datalines; 
JanAX1 
JanAK1 
JanBCT2 
FebCX2 
FebAP4 
JanA 
FebB 
JanAX1 
FebCX2 
; 
run; 

/*Counting distinct by giving another condition for Id with brand should do 
    the job for you*/ 
proc sql; 

    create table items as 
    select period, 
    count(distinct case when (brand="A" and ID not=" ") then ID end) as 
    Count_A, 
    count(distinct case when (brand="B" and ID not=" ") then ID end) as 
    Count_B, 
    count(distinct case when (brand="C" and ID not=" ") then ID end) as 
    Count_C 
from a 
group by period; 
quit; 

希望這有助於:-)

0

如果你不想硬編碼每個列,然後添加品牌到group by聲明,然後轉置結果。如果有很多不同的品牌,這是一個有用的方法。

data have; 
input Period $ Brand $ ID $; 
datalines; 
Jan A  X1 
Jan A  K1 
Jan B  CT2 
Feb C  X2 
Feb A  P4  
; 
run; 

proc sql; 
create table temp 
as select 
    period, 
    brand, 
    count(distinct id) as count 
from have 
group by period, brand; 
quit; 

proc transpose data=temp out=items (drop=_NAME_) prefix=Count_; 
by period; 
id brand; 
var count; 
run;