2017-05-04 2484 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

相關問題