Trying to create new var, can't figure out if/and options


New Member
Hi there,

I'm a beginner with Stata, so was hoping for some help.

I have two variables: age and qualification

The qualification variable looks like this:

Value 1 CSE or equivalent/GCSE (grades D-G)
Value 2 O level or equivalent/GCSE (Grades A-C)
Value 3 A level or equivalent
Value 4 Degree or equivalent
Value 5 Postgraduate degree or equivalent
Value 6 None of these

I want to construct a years of schooling variable as follows:

If the individual is over 40 AND has value 6, then in the new variable, years of schooling = 10
If the individual is under and including 40 AND has value 6, then in the new variable, years of schooling = 11

For the others, age is not important, so if the value of the qualification variable is 3, for example, the years of schooling value should be 13. If it's 4, years of schooling should be 16 etc

How would I go about writing a command to do this?



gen years=10 if age>40 & !missing(age) & qualification==6
replace years=11 if age<=40 & qualification==6
replace years=13 if qualification==3
... etc

Some things to note:
= means assign a value
== means "is equal to"
use & for logical AND, | for logical OR
! means NOT

You need the:
& !missing(age)
because otherwise a missing age will match the expression ">40" (since in Stata, missing values are very very very large numbers)