Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to run a do file using a loop with capture

    Hi, I got a do file that only label variables and I need to use it by running it inside another 2 do files which are basically the same but one has variables from 1996 and the other one from 2010. So I tried using this:
    foreach x in 96 10{
    cap label var ind1_`x' "Nro docentes /Nro alumnos pregrado v.0"
    cap label var ind1_`x'_1 "Nro docentes /Nro alumnos pregrado v.1"
    cap label var ind1_`x'_2 "Nro docentes /Nro alumnos pregrado v.2"
    cap label var ind1_`x'_3 "Nro docentes /Nro alumnos pregrado v.3"
    cap label var ind1_`x'_4 "Nro docentes /Nro alumnos pregrado v.4"
    cap label var ind1_`x'_5 "Nro docentes /Nro alumnos pregrado v.5"
    cap label var ind1_`x'_6 "Nro docentes /Nro alumnos pregrado v.6"
    cap label var ind1_`x'_7 "Nro docentes /Nro alumnos pregrado v.7"
    cap label var ind1_`x'_8 "Nro docentes /Nro alumnos pregrado v.8"
    cap label var ind1_`x'_9 "Nro docentes /Nro alumnos pregrado v.9"
    }
    and It worked in both files ( 1996 and 2010). However I can't include this command on those do files cause the idea is to only use the comand run dofile/labels.do in both of them and the labels file should work the same way as if I were using the command on them. The thing is, it doesn't and I can't figure out why... Any thoughts?

  • #2
    The only thing I can think of is that the macro x is defined in the first do file.
    Is your setup like this? :
    Code:
    --------dofile1.do---------
    foreach x in 96 10 {
        do dofile2.do
        }
    ---------------------------
    
    --------dofile2.do---------
    cap label var ind1_`x' "Nro docentes /Nro alumnos pregrado v.0"
    ---------------------------

    If your macros are created in one do file, they are not available in another do file if you use do filename.do. Instead, either re-define the macro in the 2nd do file, or use include filename.do.

    So, either use this:
    Code:
    --------dofile1.do---------
    foreach x in 96 10 {
        include dofile2.do
        }
    ---------------------------
    or do this:
    Code:
    --------dofile2.do---------
    foreach x in 96 10 {
        cap label var ind1_`x' "Nro docentes /Nro alumnos pregrado v.0"
        }
    ---------------------------
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Thanks a lot! It worked.

      Comment

      Working...
      X