Announcement

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

  • How to execute a do file with local macros and calling other do files within the file which inturn use those macros??

    Hi Stata listers

    I am trying to work with local and perhaps global macros in do-files.

    The code below works when it is run in its entirety. If you put the code nominated (marked by ******) in a separate do file and call it by:

    do "dofilename.do"

    Nothing happens. I tried replacing local with global for the macros (indv, strv etc) and this also didn't work.

    Almost certain it is the local and global issue but not sure how to fix.

    Can anyone point me in the right direction?

    Thanks very much.

    Darren

    __________________________________________________ __________________________________________________ __________________________________
    use "Q:\STATA\Surveys\2013-14\Adult\data_pooling_process\Data\SRHS2013_14_MAS TER_TEMP.dta", clear
    svyset [pweight= pwt_18plus_FINAL]
    gen dumif=1
    local indv daily_smoke
    local strv persons rsex agecat_nhs
    local if1 edu_sum marri_sum
    local if2 dumif
    local geov lga2014_FINAL

    ************************************************** **********************************
    //CONTENT FOR A SEPARATE DO FILE BELOW
    ************************************************** **********************************

    mata
    cmndf=""
    colf=("","","")
    estf=(.,.,.,.,.,.)
    end
    foreach ifs1 in `if1' {
    levelsof `ifs1', local(ifl1)
    foreach l1 of local ifl1 {
    foreach ifs2 in `if2' {
    levelsof `ifs2', local(ifl2)
    foreach l2 of local ifl2 {
    foreach geo in `geov' {
    levelsof `geo', local(geog)
    foreach g of local geog {
    foreach ind in `indv' {
    foreach str in `strv' {
    capture estpost svy: tab `str' `ind' if `ifs1'==`l1' & `ifs2'==`l2' & `geo'==`g', row
    if _rc==0 {
    estpost svy: tab `str' `ind' if `ifs1'==`l1' & `ifs2'==`l2' & `geo'==`g', row notot
    mat t=e(row)\e(se)\e(lb)\e(ub)\e(obs)\e(count)
    mat t=t'
    local cmnd=e(cmdline)
    local wtvar=e(wvar)
    mata: cmd=st_local("cmnd")
    mata: wtv=st_local("wtvar")
    mata: cmnd = J(cols(st_matrix("e(b)")),1,cmd)
    mata: wtvar =J(cols(st_matrix("e(b)")),1,wtv)
    mata: est=st_matrix("t")
    mata: col=st_matrixcolstripe("e(b)")
    mata: col=col,wtvar
    mata: colf=colf\col
    mata: estf=estf\est
    mata: cmndf=cmndf\cmnd
    }
    }
    }
    }
    }
    }
    }
    }
    }
    ************************************************** ******************************************
    ************************************************** ******************************************

  • #2
    when working with locals and multiple do files there are two tricks that can be useful:

    The first trick is you can use include instead of do. That way the included .do-file acts as if it were part of the main .do-file and local macros in the main .do-file are available in the included file.

    The second trick is that you can type something like do somefile.do foo bar, where foo bar can be anything you want to pass on to the .do-file somefile.do. The first argument (in this case foo
    ) will be stored in the local macro `1' and the second argument (in this case bar) will be stored in the local macro `2', etc.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks Maarten, much appreciated, a handy command.


      Comment


      • #4
        Useful trick

        Comment

        Working...
        X