This tutorial explains various ways to calculate z-score in SAS.
The formula of z-score is as follows:
Z = (X – μ) / σ
- X is a raw data value
- μ is the mean of a variable
- σ is the standard deviation of a variable
Let's create sample data in SAS and explain it with an example.
data mydata; input salary; cards; 65059 40539 56769 85448 51601 60018 59021 62959 61898 77322 76439 51068 76354 41666 71469 87629 ; run;
Method 1 : PROC STDIZE
The easiest way to calculate a z-score in SAS is by using the PROC STDIZE procedure. You can specify one or more variables in the VAR statement for calculating z-score.
PROC STDIZE DATA=mydata out=ZSCORES; VAR salary; RUN;
In this example, z-scores for the variable "salary" in the dataset "mydata" are calculated and stored in the new dataset "zscores".
A z-score tells us how many standard deviations a value is from the mean. It can be positive (above average), negative (below average) or zero (exactly average).
Method 2 : PROC STANDARD
The PROC STANDARD procedure, an older version of PROC STDIZE, does the same thing and can be used for calculating z-scores in SAS. You can specify values in MEAN and STD option. For e.g. MEAN=0 STD=1
PROC STANDARD DATA=mydata MEAN=0 STD=1 OUT=ZSCORES; VAR salary; RUN;
Method 3 : PROC SQL
You can write the z-score equation manually using PROC SQL. It is not recommended when you have more than one variable for calculating z-score as it involves writing a lot of code within PROC SQL.
PROC SQL; CREATE TABLE ZSCORES AS SELECT *, (salary - MEAN(salary)) / STD(salary) AS Z FROM mydata; QUIT;
Method 4 : PROC MEANS
Using PROC MEANS, you can store the mean and standard deviation of a variable and later use them to calculate the z-score.
PROC MEANS NOPRINT DATA=mydata; VAR salary; OUTPUT OUT=means_out MEAN=WTMEAN STDDEV=WTSD; RUN; DATA ZSCORES (DROP= WTMEAN WTSD _TYPE_ _FREQ_); SET mydata; IF _N_=1 THEN SET means_out; Z=(salary-WTMEAN)/WTSD; RUN;
Share Share Tweet