Exodia16
Exodia16

Reputation: 177

Adding variables in Stata and then dividing by a number is giving unexpected results

I am trying to calculate z-scores by creating a variable D from 3 other variables, namely A, B, and C. I am trying to generate D as : D= (A-B)/C but for some reason when I do it, it produces very large numbers. When I did just (A-B) it did not get what it should have when I calculated by hand, instead of -2, I for -105.66.

Variable A is 'long' and variable B is 'float', I am not sure if this is the reason? My stata syntax is:

gen zscore= (height-avheight)/meansd

did not work.

Upvotes: 1

Views: 4594

Answers (2)

Metrics
Metrics

Reputation: 15458

I am not sure what you are trying to get, but I will use the auto data from Stata to explain. This is basic stuff in Stata. Say I want to test that the price=3

sysuse auto
sum price
#return list which is optional command
scalar myz=(3-r(mean))/r(sd) #r(mean) and r(sd) gives the mean and sd of price, if that is given you can simply enter the value for that
dis myz
-2.0892576

So, z value is -2.09 here.

Upvotes: 1

Fr.
Fr.

Reputation: 2885

You are confusing scalars and variables. Here's a solution (chop off the first four lines and replace x by height to fit the calculation into your code):

// example data
clear
set obs 50
gen x = runiform()

// summarize
qui su x
// store scalars
sca de mu = r(mean)
sca de sd = r(sd)

// z-score
gen zx = (x - mu) / sd
su zx

x and its z-score zx are variables that take many values, whereas mu and sd are constants. You might code constants in Stata by using scalars or macros.

Upvotes: 2

Related Questions