sasdatastep

Calculate mean and std of a variable, in a datastep in SAS


I have a dataset where observations is student and then I have a variable for their test score. I need to standardize these scores like this :

newscore = (oldscore - mean of all scores) / std of all scores

So that I am thinking is using a Data Step where I create a new dataset with the 'newscore' added to each student. But I don't know how to calculate the mean and std of the entire dataset IN in the Data Step. I know I can just calculate it using proc means, and then manually type it it. But I need to do I a lot of times and maybe drop variables and other stuff. So I would like to be able to just calculate it in the same step.

Data example:

__VAR testscore newscore
Student1 5 x
Student2 8 x
Student3 5 x

Code I tried:

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

(Can't post any of the real data, can't remove it from the server)

How do I do this?


Solution

  • Method1: Efficient way of solving this problem is by using proc stdize . It will do the trick and you dont need to calculate mean and standard deviation for this.

    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;   
    

    Method2: As you suggested taking out means and standard deviation from proc means, storing their value in a macro and using them in our calculation.

    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;
    

    My output:

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

    Let me know in case of any queries.