Looping: foreach in detail


More examples of foreach and forvalues commands.

Stata processes all observations of the data set for each element in the list of the foreach and forvalues commands.

This page shows 4 different types of lists that can be used in the foreach command: varlist, newlist, numlist, and anylist and how to use the forvalues command.

Both foreach and forvalues commands use "local macro" variables in processing whatever list they loop through. A local macro variable is not a variable in the data set but rather a variable available to Stata for programming purposes.  To learn more about local macro variables use Stata's help by searching for "macro".  Here's a quick example:

. local X= "MyVarName"  // create local macro variable X and set it equal to the text MyVarName.
. display "My variable name is: `X'"

 "My variable name is: MyVarName"

Notice that the local macro variable X is enclosed in a left and right single quote.  The left quote is under the tilde (~) key on the left side of your keyboard and the right quote is under the double quote (") key on the right side of your keyboard.  Enclosing a letter or variable name in left and right quotes tells Stata to evaluate it as a local macro variable.

NOTE: the following is a do-file. The foreach and forvalues commands are better suited to a do-file than to interactive use of Stata. Highlight and copy all text between the horizontal bars and paste into the Stata interactive do-file window so that you can run this do-file.


use "q:\utilities\statatut\examfac2.dta" 


list q102_*  in 1/10 

/************************* from log *******************************************
            q102_1         q102_2         q102_3         q102_4         q102_5 
  1.     PH NurseB          Other        ClinOff             99             99 
  2.       ClinOff             99             99             99             99 
  3.      NurseOff      PH NurseB      PH NurseB       MCH Aide       MCH Aide 
  4.       ClinOff        ClinOff  Nurse/Midwife       MCH Aide             99 
  5.      MCH Aide    NurseAssist        ClinOff             99             99 
  6. Nurse/Midwife        ClinOff  Nurse/Midwife             99             99 
  7.   NurseAssist    NurseAssist             99             99             99 
  8.       ClinOff    NurseAssist    NurseAssist             99             99 
  9.       ClinOff    NurseAssist    NurseAssist             99             99 
 10. Nurse/Midwife             99             99             99             99 

Notice that q102_* vars appear to be character data not numeric.
       The value label "title" is associated to these variables formats 
       the numeric data to look like character data.  Click here to see webpages about 
       variable labels and value labels    **/

/** use varlist for a list of variables that already exist in a data set */

/** Say you want to disassociate the value label title from the q102_* variables.
    You could type: label value q102_1  
                    label value q102_2  
                    label value q102_3  
                    label value q102_4  
                    label value q102_5  

    Or you could use a varlist in a foreach command to disassociate 
    the value label title with the q102_* vars.  **/

foreach X of varlist q102_1 q102_2 q102_3 q102_4 q102_5{ 
 label value `X'   /* Notice that the capital letter X is is enclosed in left and right quotes.
                    * This tells Stata to evaluate the local macro X. */
 display "label value `X' "  // Displays in the results window/log file what commands Stata processed.
                             // This is completely unnecessary but helpful in explaining the looping.

list q102_*  in 1/10 

 In both cases the value label title will still be available for use
 with other variables or even to be re-used again on the q102_1-q102_5 variables. 
 If the value label title is not associated to any variable when the data set
 is saved then it will be dropped.

/* Now re-assign the title value label to each q102_* variable. 
   This time use a local macro variable in the foreach command.  **/

local titles "q102_1 q102_2 q102_3 q102_4 q102_5"    
foreach X of varlist `titles' { 
 label val `X' title   
/** NOTE: The apostrophe on the left is a left apostrophe.
          It is on your keyboard next to the "1" key.  
          When a local macro variable is enclosed in a left
          and right apostrophe, it is evaluated as the 
          contents of that local macro variable.  **/

list `titles'  in 1/10 

/** NOTE: A local macro variable can be used many times throughout a program and
          thus can save a lot of typing.  It can also help keep a foeach command
          from looking very messy, like if you wanted to pass through a foreach 
          command 10 or more variables that could not be represented in shorthand 
          (like q103_1-q103_10 can be).

    NOTE: `titles' represents the text "q102_1 q102_2 q102_3 q102_4 q102_5".         
          Local macro variable names can be up to 7 (not 8) characters long in Stata 6.  
          Stata 7 allows them to be up to 31 characters long.


/* Use newlist for variables that are created/generated by the foreach command. */

/* Create string variables title1c, title2c, title3c, title4c and title5c that can hold up to 13 characters. */

foreach X of newlist title1-title5{ 
 gen str13 `X'c= "" 

/** NOTE: A foreach command like:
            foreach X of newlist title* { 
              gen str13 `X'c= "" 
          would not work because Stata wouldn't know how many title variables to create.  **/

/** Use numlist to insert numbers in place of the local macro var X into a foreach command. **/

/** Use numlist to rename q102_* vars in a foreach command  **/

foreach X of numlist 1/5 {
  rename q102_`X' title`X'  
                /** The local macro variable X is replaced with
                    the numbers 1, 2, 3, 4, 5  ***/

list title1-title5 in 1/10 

/**  NOTE: The above foreach command could have been written with a forvalues command:
 *   forval X= 1/5 { 
 *    rename q102_`X' title`X' 
 *   }
 *   The capital letter X and could be any letter, upper or lower case. 
 *   A good rule of thumb is have all variable names in lowercase.

/** The anylist is a list of words.  Shorthand notation like title* or
title1-title5 does not work.  **/

foreach X in 1 2 3 4 5 6 7 8 9 10 { 
  replace title1c="not missing" if title1==`X'

/* The anylist is the most universal type of foreach command since it is not 
   restricted to pre-existing variables, new variables, or just numbers.  */

/* More than 1 list of variables can processed at a time. */

/* Replace all values of 99 with missing in title1-title5 and q103_*  */

summarize title5-q103_1 

foreach X of varlist title1-title5 q103_* { 
  replace `X'= .  if `X' == 99 

summarize title5-q103_1 

/** Two or more lists or types of lists can be processed at the same time. **/

/** Create new variables from existing variables in a foreach command. **/

 /* First create 2 variable list and store them in local macro variables using the
  * the unab Stata command: */
unab varlist1 : title1-title5
unab varlist2 : q103_1-q103_5  

 /* Use the display command to see that local macro varlist1 contains the string:
  *  q103_1 q103_2 q103_3 q103_4 q103_5 */
di "`varlist1'"

foreach X of newlist mch_h1-mch_h5 {
 local n= `n' + 1   /* keep count of how many times the loop is processed */
 /* Use the extended macro function ": word # of" to set the local macro variable Y to the n'th variable name */
 local Y : word `n' of `varlist1'
 local Z : word `n' of `varlist2'
 gen `X'= `Z'  if `Y' == 8
 label var `X' "MCH Aid's hours" 

/** NOTE: All lists have to have the same number of elements. **/

/* Since all the variables involved have numbers involved in their name, using the forval command would be simpler: 
  forvalues X= 1/5 {  
   gen mch_h`X'= q103_1`X'  if title`X' == 8
   label var mch_h1`X' "MCH Aid's hours" 

list mch_h1-mch_h5 in 23/31 

/** NOTE: Stata reads the shorthand varlist of mch_h1-mch_h5 to be all
          variables positionally in the data set between mch_h1 and mch_h5 (not 
          necessarily mch_h1 mch_h2 mch_h3 mch_h4 mch_h5).  When using Stata
          interactively, the variable list window in the lower left-hand side
          shows the list of variables in the data set in the order of their position. 
          The describe command also shows variable order. **/


Review again?


Another topic?

Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme