Conditional Loops (Loops)
In this final exercise you’ll use programming if, foreach, and continue to control what happens inside a loop based on sample size.
Goal: For a list of variables, automatically summarize and graph them, but skip variables that have too few non-missing observations. Start bv defining a local macro named vars with the following variables:
yield_kgharvest_value_USDnitrogen_kgtotcons_USD
- Write a
foreachandvarlistto loop overvars. Inside the loop, for each variablev:- Run
`qui sum `v', detail - Store the number of non-missing observations in a local
Nusingr(N)(remember to use=so that the local equals the expression and not the literal textr(N)). Then, still within the loop, have Statadisplaythe number of observations for each variable. How many observations are there for each variable?
- Run