cd N:\workshop\May2015 clear all set more off sysuse auto, clear *Example 1: Attach the prefix _78 to the variables named price and mpg foreach v in price mpg { rename `v' `v'_78 } *The following is equivalent: foreach v of varlist price mpg { rename `v' `v'_78 } ******************************************************************************** *Example 2: Count the number of observations in this dataset local counter = 0 local N = _N forvalues i = 1 / `N' { local counter = `counter'+1 } display `counter' *Note that this exactly what the count command does. ******************************************************************************** *Example 3: Count the number of observations in this dataset local counter = 1 local N = _N while `counter' < `N' { local counter = `counter'+1 } display `counter' ******************************************************************************** *Example 4: Count the number of foreign and domestic vehicles local count1 = 0 local count0 = 0 local N = _N forval orig = 0/1 { forval row = 1/`N' { if foreign[`row'] == `orig' { local count`orig' = `count`orig'' + 1 } } } display "No. of foreign cars: `count1'" display "No. of domestic cars: `count0'"