1 / 11

Working with Array in Stata

Working with Array in Stata. Vivien W. Chen Izumi Mori. Do you …. W rite repetitive programs for a set of variables or data files? Use longitudinal data that repeat the same data management for multiple years? Want to reduce the chance of coding errors due to repeated programming?.

Antony
Télécharger la présentation

Working with Array in Stata

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Array in Stata Vivien W. Chen Izumi Mori

  2. Do you … • Write repetitive programs for a set of variables or data files? • Use longitudinal data that repeat the same data management for multiple years? • Want to reduce the chance of coding errors due to repeated programming?

  3. The Purpose of This Workshop • Introduce the corresponding programs to SAS array in Stata: forvalue& foreach • Include the use of macro local & global • Introduce programs for temporary files and variables: tempvar&tempvar • Reshapedata structure for longitudinal data

  4. Example Data (Source data: PISA 2000, 2003, 2006)

  5. The Use of forvalues & foreach • forvalues is a loop that executes commends with a defined numeric local macro. Specific numbers or a range of numbers can be added in the macro before braces “{” and “}”. A range may be 1/5, meaning 1 to 5; or 0(5)100, indicating 0 to 100 in steps of 5.

  6. Example 1: replace missing values for XXX in each year file One solution is: use file2000,clear replace XXX=. If XXX<0 use file2003,clear replace XXX=. If XXX<0 use file2006,clear eplace XXX=. If XXX<0 Alternatively: forv n=2000 2003 2006 { use file`n’,clear replace XXX=. If XXX<0 }

  7. foreachis a more generally useful statement. It can work with a list of variables, numbers, and names. It also can create new variable list. • Example 2: (the same task as example 1) foreach n of numlist 2000 2003 2006 { use file`n’,clear replace XXX=. If XXX<0 }

  8. Example 3: In each year file, (1) create dummy variables for mother’s education; (2)create interactions terms for gender, foreign born with mother eduand parental occ; (3) create a year ID in each file; and (4) append those files together. foreach n of numlist 2000 2003 2006 { use file`n’,clear tab med, gen(med) foreach x of varlist female fborn { gen `x’_occ=`x’*occ foreach n2 of numlist 0/6 { gen `x’_med`n2’=`x’*med`n2’ } } gen yr=`n’ } use file2000,clear append using file2003 file 2006 1 2 3 4

  9. Are these the alternatives? foreach n of numlist 2000 2003 2006 { use file`n’,clear tab med, gen(med) foreach x of varlist female fborn { gen `x’_occ=`x’*occ gen `x’_med=`x’*med (?) } gen yr=`n’ append using file`n’ (?) } If you want to interact with the continuous variable of mother’s education, you can do so. This won’t work, because it’s in the loop of each year.

  10. Make Use of Macro –local & global • A local macro is created in a do file and ceases while the do file terminates. • A global macro terminates while a user exits Stata program

  11. Example 4: regression analysis local x1 female fborn local x2 occ med1-med6 cultposs /* global x1 female fborn global x2 occ med1-med6 cultposs */ regp1vmath `x1’ reg p1vmath `x1’ `x2’ /* reg p1vmath $x1 reg p1vmath $x1 $x2 */ Can I just use ”by” instead of “foreach” country?

More Related