2017-05-04 563 views
0

我有一个观察值是学生的数据集,然后我有一个测试分数的变量。我需要规范这些分数是这样的:在SAS中的datastep中计算变量的平均值和标准差

newscore =(oldscore - 平均所有得分的)所有得分/ STD

使我的想法是使用数据的步骤,其中我创造一个新的数据集与'newscore'添加到每个学生。但我不知道如何计算数据步骤中整个数据集IN的平均值和标准偏差。我知道我可以用proc方法计算它,然后手动输入它。但我需要做很多次和mabye下降变量和其他stuf。所以我希望能够在同一步骤中进行计算。

数据为例:

___VAR_ testscore newscore
Student1 5×
STUDENT2 8×
学生三5×

代码我尝试:

data new; 
set old; 
newscore=(oldscore-(mean of testscore))/(std of testscore) 
run; 

(广东话发布任何的真实数据,无法将其从服务器上删除)

任何人都知道如何拉这个把戏?

最好的问候

+1

请编辑您的问题,包括一些示例数据和迄今尝试过的sas代码。 – user667489

回答

0

方法一:解决这个问题的有效的方法是通过使用PROC stdize。它会做的伎俩,你不需要计算平均值和标准偏差。

data have; 
input var $ testscore; 
cards; 
student1 5 
student2 8 
student3 5 
; 
run; 

data have; 
set have; 
newscore = testscore; 
run; 

proc stdize data=have out=want; 
    var newscore; 
run; 

方法2:如你所说取出手段和PROC手段的标准偏差,在宏存储他们的价值和我们的计算使用它们。

proc means data=have; 
var testscore; 
output out=have1 mean = m stddev=s; 
run; 

data _null_; 
set have1; 
call symputx("mean",m); 
call symputx("std",s); 
run; 

data want; 
set have; 
newscore=(testscore-&mean.)/&std.; 
run; 

我的输出:

var   testscore newscore 
student1  5   -0.577350269 
student2  8   1.1547005384 
student3  5   -0.577350269 

让我知道任何疑问的情况下。

+0

嘿,thansk很多! Proc标准确实让这项工作变得简单。 并感谢其他方法,我知道你可以使用proc手段来存储输出,然后再次使用它。虽然我只是做了一张桌子。 Thansk很多! – Storm

+0

是的。但是你也需要知道如何存储这些值并进一步使用它们,而不用手动输入它们(在这种情况下意味着方法和标准偏差),因此我也用另一种方式告诉它们。不用谢。高兴地帮助:) –

+0

阿尔的课程,完成,即时通讯非常新的这里在stackoverflow :) – Storm

1

你不应该尝试在数据步骤中做到这一点。用proc means做到这一点。您不需要输入任何内容,只需获取数据集中的值即可。

你不能提供足够的答案给出完整的代码,但基本的想法。

proc means data=sashelp.class; 
var height weight; 
output out=class_stats mean= std= /autoname; 
run; 

data class; 
    if _n_=1 then set class_Stats; *copy in the values from class_Stats; 
    set sashelp.class; 
    height_norm = (height-height_mean)/(height_stddev); 
    weight_norm = (weight-weight_mean)/(weight_stddev); 

run; 

另外,只需使用PROC STDIZE这将为你做这个。

proc stdize data=sashelp.class out=class_Std; 
    var height weight; 
run; 
+0

非常感谢您的回答! 我不知道proc stdize,它的工作就像一个魅力! 再次感谢 – Storm

相关问题