Looping with while

while: another way to repeat commands.

The while command can be used in much the same way as foreach or forvalues, but it has greater flexibility. While it's generally used by programmers (writing commands), it is a tool available for do-files as well.

You can copy the following text into a do-file and run it to see how while works.

clear

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

su
 
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.
   **/


/** Say you want to disassociate the value label title from the q102_* vars.
    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 while command to disassociate 
    the value label title with the q102_* vars.  **/

local i= 1
while `i' <= 5 {
  label value q102_`i' 
  local i= `i' + 1
 }

/** It is possible to write a while command without using a local macro variable. 
    Users generally use local macro variables because they are not associated 
    to the data set.  It's not possible for their values to be different from
    one observation to the next and they can hold a number or a string of text.
 
    In this example, the while command uses the local macro variable i to step 
    through the list of variables q102_1 q102_2 q102_3 q102_4 q102_5.  `i' is 
    first evaluated to be 1 and then 2 and so on until the expression `i'<=5 is 
    not true (when `i'=6). The brackets "{" and  "}" enclose all commands to be 
    processed by the while command.  **/    

list q102_*  in 1/10

             
/* Create string variables that can hold up to 13 characters. */

local j=1  /** local i=1 would also work.  i is not the only local macro variable name allowed **/
while `j' <= 5 { 
 gen str13 titlec`j'= ""
 local j= `j' + 1
 }

/** Rename q102_* vars **/
local hi= 1
while `hi' <= 5 { 
 rename q102_`hi' title`hi'
 local hi= `hi' + 1
 }               

list title1-title5 in 1/10

local hi= 1 
while `hi' <= 10 {  
 replace titlec1= "not missing"  if title1 == `hi'
 local hi= `hi' + 1
 }                

/* More than 1 list of variables can used in while command. */

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

su title5-q103_1

local i= 1
while `i' <= 5 {
 replace title`i'= .  if title`i' == 99
 replace q103_`i'= .  if q103_`i' == 99
 local i= `i' + 1
 }                

su title5-q103_1

/** Create new variables from existing variables. **/
local i=1 
quietly while `i' <= 5 {
 gen mch_h`i'= q103_`i'  if title`i' == 8
 label var mch_h`i' "MCH Aid's hours"
 local i= `i' + 1
}

/** NOTE: Adding "quietly" to a STATA command tells STATA not to print any output for the command.  
          All lists of variable have to have the same number of elements. 
************************************************************************/


list mch_h1-mch_h5  in 11/15


/** 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.  **/

/* Here is an example using while to run the decode command 
   with all title1-title5 variables:

   local i= 1
   while `i' <= 5 {
       decode title`i', gen(title`i'c)
       local `i'= `i'+1
   }

************************************************************************/

 

Note: Dan Blanchette contributed this web page, however please direct questions to Phil Bardsley as noted below.

 


Review again?

 

Another topic?


Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme