SAS proc SQL嵌套函数

时间:2016-11-29 01:56:50

标签: sql sas

人,

我的目标很简单。首先按组计算数字(surgid),然后获得每组中的平均值。我尝试了两个SQL,但都没有成功。徘徊任何人都知道我应该如何嵌套代码?

谢谢, 安德烈

首先尝试:

proc sql;
create table bhr_surg as
select a.*, mean(b.numsurg) as meansurg from bhr as a,
(select count(b.surgid) as numsurg from bhr as b)

where surgid not in (1,2,3,7777)
group by surgid,procyr;
quit;

第二次尝试:

proc sql;
create table bhr_surg as
select *, count(surgid) as numsurg, mean(calculated numsurg) as meansurg from bhr

where surgid not in (1,2,3,7777)
group by surgid,procyr;
quit;

1 个答案:

答案 0 :(得分:0)

您的问题是,除了surgid之外,您还要按照某些内容进行分组,因此您的计数和方法都是您的ID组子集的计数和方式。以下应该有效:

data have;
  input surgid numsurg;
  datalines;
10 1000 
10 1500 
10 2000 
20 3000 
20 4000 
30 2500 
;
run;


proc sql;
    create table want as select distinct
        *, count(numsurg) as count, mean(numsurg) as mean
        from have
        group by surgid;
quit;