使用 Proc SQL 计算相关查询

Counting Correlated Query with Proc SQL

我有一个 table master_t customer_id 和 product_id 和 flyer_section

我想统计 "back" 每 customer_id 出现的频率,到目前为止我有

proc sql
create table cust as
select
    distinct customer_id
    count((select(flyer_section from master_t where master_t.customer_id = distinct master_t.customer_id and flyer_section = 'back'))
from master_t
;
quit proc sql

我遇到语法错误,我不确定它是否仍然有效

谢谢。

对于您的查询,这样的事情不应该有效吗?

create table cust as
select
    customer_id
    count(flyer_section) as BackCount
from master_t
where upper(flyer_section) = 'BACK' 
      or lower(flyer_section) = 'front' 
      or lower(flyer_section) = 'inside'
group by customer_id

在 SAS 中很容易,因为它计算布尔表达式的方式。

proc sql noprint;
  create table cust as
    select customer_id
         , sum(upcase(flyer_section)='BACK') as n_back
         , sum(upcase(flyer_section)='FRONT') as n_front
    from master_t 
    group by 1
   ;
quit;

当然为什么不直接使用

PROC FREQ ;
  tables customer_id*flyer_section / missing noprint out=counts ;
run;