New variable based on an expression

Create a new variable based on an expression that can include multiple variables and/or values. This works pretty much like a calculator.

Below, two examples are presented.

Practical example 1

Dataset
StataData1.dta
Variable nameunemp_42
Variable labelDays in unemployment (Age 42, Year 2012)
Value labelsN/A
Variable nameunemp_43
Variable labelDays in unemployment (Age 43, Year 2013)
Value labelsN/A
Variable nameunemp_44
Variable labelDays in unemployment (Age 44, Year 2014)
Value labelsN/A
Variable nameunemp_45
Variable labelDays in unemployment (Age 45, Year 2015)
Value labelsN/A
gen unemp=unemp_42+unemp_43+unemp_44+unemp_45

The new variable unemp contains the sum of the other four variables.

sum unemp_42 unemp_43 unemp_44 unemp_45 unemp

browse unemp_42 unemp_43 unemp_44 unemp_45 unemp

Note
If any of the variables that you include in the expression have missing values, the value for the new variable will be missing as well.

Practical example 2

Dataset
StataData1.dta
Variable namebweight
Variable labelBirth weight (Age 0, Year 1970)
Value labelsN/A
gen bweight_grams=bweight*100

The old variable bweight shows birth weight in hectograms, but now we have created the new variable bweight_grams which shows birth weight in grams instead.

sum bweight bweight_grams

browse bweight bweight_grams