SAS中用于计算均值的观测数量计数

huangapple go评论48阅读模式
英文:

Count number of observations used to calculate the mean in SAS

问题

我想生成一个名为n的新列,用于回答以下问题:平均值是从多少个级别计算得出的?1个级别还是2个级别还是3个级别?

有没有一种方法可以在不将数据从长格式转换为宽格式的情况下计算N?N应该告诉我们计算平均值所需的观测数量。谢谢。

我的输入数据集如下:

   input entity $ level $ value mean; 
   datalines; 
		A	Level1	5.85	5.9
		A	Level2	5.95	5.9
		B	Level1	0.12	0.12
		B	Level2	0.12	0.12
		B	Level3	0.12	0.12
		C	Level1	0.8		0.8
		C	Level2	.		0.8
		D	Level1	.		0.23
		D	Level2	.		0.23
		D	Level3	0.23	0.23
		E	Level1	9.6		9.35
		E	Level2	9.1		9.35
		F	Level1	.		3.9
		F	Level2	3.9		3.9
;```

<details>
<summary>英文:</summary>

I want to generate a new column n which will answer the question: How many levels was the mean calculated from? 1 or 2 or 3?

Is there a way to calculate N without converting the data from long to wide format? N should tell us the number of observations that were required to calculate the mean. Thank you. 

My input dataset is:
```data inputdata; 
   input entity $ level $ value mean; 
   datalines; 
		A	Level1	5.85	5.9
		A	Level2	5.95	5.9
		B	Level1	0.12	0.12
		B	Level2	0.12	0.12
		B	Level3	0.12	0.12
		C	Level1	0.8		0.8
		C	Level2	.		0.8
		D	Level1	.		0.23
		D	Level2	.		0.23
		D	Level3	0.23	0.23
		E	Level1	9.6		9.35
		E	Level2	9.1		9.35
		F	Level1	.		3.9
		F	Level2	3.9		3.9
;```



</details>


# 答案1
**得分**: 0

不同的方法计算均值的方式相同。

因此,如果您使用PROC SUMMARY,除了MEAN统计信息之外,还要请求N统计信息。

```sas
proc summary data=have ;
  by entity level ;
  var value;
  output out=means mean=mean n=n ;
run;

如果您使用PROC SQL,则可以在MEAN()聚合函数之外使用N()聚合函数。您还可以使用COUNT()聚合函数。

proc sql;
create table want as 
   select entity,level,value,n(value) as n,mean(value) as mean
   from have
   group by entity,level
;
quit;
英文:

Just do it the same why you calculated the mean.

So if you use PROC SUMMARY then ask for the N statistic in addition to the MEAN statistic.

proc summary data=have ;
  by entity level ;
  var value;
  output out=means mean=mean n=n ;
run;

If you use PROC SQL then use the N() aggregate function in addition to the MEAN() aggregate function. You could also use the COUNT() aggregate function.

 proc sql;
 create table want as 
   select entity,level,value,n(value) as n,mean(value) as mean
   from have
   group by entity,level
 ;
 quit;

huangapple
  • 本文由 发表于 2023年3月4日 04:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定