Branching

Introduction to Branching: Presentation

Branching

* Branching

sysuse auto, clear
count if foreign == 0 		// Count all domestic car models.
local obs = r(N)		// Use the scalar containing the number of observations, created by "count"
if `obs' >= 10 {		// Execute if there are at least 10 domestic car models. Execute, if this condition is true.
	sum price if foreign == 0
}
else {				// Execute if there are less than 10 domestic car models. - Can only be executed after if-condition.
	sum price if foreign == 1
}

 
* Branching with nesting

sysuse auto, clear
count if foreign == 0	   
local obs = r(N)
if `obs' >= 10 {		// Execute if there are at least 10 domestic car models.  
	if price[3] >= 1000 {   // Execute if the price of the car in observation 3 is at least 1000 US-$.
		sum price
	}
}