Announcement

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

  • Program within a DO file

    I want to repeat block of code in different places of a do file, for which I tried to write a program within that do file as we normally do in an ado file, however, in the DO file it did not work. Is that illegal operation? Is there other way of doing it.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    What do you mean by "it did not work"?

    .ÿdoÿhw

    .ÿ*!ÿhw.do
    .ÿ
    .ÿversionÿ14.0

    .ÿ
    .ÿclearÿ*

    .ÿsetÿmoreÿoff

    .ÿ
    .ÿprogramÿdefineÿhello_world,ÿrclass
    ÿÿ1.ÿÿÿÿÿÿÿÿÿversionÿ14.0
    ÿÿ2.ÿÿÿÿÿÿÿÿÿsyntaxÿvarname
    ÿÿ3.ÿ
    .ÿÿÿÿÿÿÿÿÿsummarizeÿ`varlist'
    ÿÿ4.ÿÿÿÿÿÿÿÿÿlocalÿhwÿHello,ÿWorld!ÿ
    ÿÿ5.ÿÿÿÿÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ_newline(1)ÿ"`hw'"
    ÿÿ6.ÿÿÿÿÿÿÿÿÿreturnÿlocalÿhwÿ`hw'
    ÿÿ7.ÿend

    .ÿ
    .ÿsysuseÿauto
    (1978ÿAutomobileÿData)

    .ÿ
    .ÿhello_worldÿgear_ratio

    ÿÿÿÿVariableÿ|ÿÿÿÿÿÿÿÿObsÿÿÿÿÿÿÿÿMeanÿÿÿÿStd.ÿDev.ÿÿÿÿÿÿÿMinÿÿÿÿÿÿÿÿMax
    -------------+---------------------------------------------------------
    ÿÿgear_ratioÿ|ÿÿÿÿÿÿÿÿÿ74ÿÿÿÿ3.014865ÿÿÿÿ.4562871ÿÿÿÿÿÿÿ2.19ÿÿÿÿÿÿÿ3.89

    Hello,ÿWorld!

    .ÿ
    .ÿdisplayÿinÿsmclÿasÿtextÿr(hw)
    Hello,ÿWorld!

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      Joseph poses the right question. Attaullah has been on Statlist quite long enough to know that his question does not provide enough information to give good advice. Attaullah should show us what exactly he typed, what exactly Stata did in response and what exactly he did not like about it. Asking for other ways to solve a problem, as is done in the initial post, additionally requires information on what exactly Attaullah wants to do.

      Otherwise the best answer is that there is nothing wrong with defining programs within a do-file. One potential pitfall might be forgetting to capture drop the program before defining it. Otherwise when called a second time, the do-file will stop the moment we try to define an already existing program. Note that Joseph's code avoids this problem by including a clear * statement. However, this might not be the most desirable thing to do, if the do-file is not the very first thing to be run in larger project.

      Best
      Daniel

      Comment


      • #4
        Sorry for not posting the example data set and the error code. Here is the data set
        Code:
        clear
        input float(S1IN1 S1IN2 S1IN3 S1IN4 S1IN5)
        . . . . . 
        -.02860511 -.00443178 -.02976823 .00298054 -.0601031 
        .04278736 .11519402 .01909637 .09742783 .03407797 
        .00672089 .0130773 .04723911 .05500206 -.00927037 
        -.06285047 -.10712053 -.09794749 -.14870901 -.0870449 
        .07621358 .07342932 .05094782 .06919103 .05548545 
        -.05694925 -.04603029 -.06035271 -.02017163 -.01089961 
        .05029551 .03400515 .05997301 -.01600985 .01988317 
        -.05472161 -.05735027 -.03320538 -.05133278 -.0560445 
        .06523906 .07379568 .04190416 .03099541 .03580021 
        end
        This is my do file and program
        Code:
        _test3 S1*
        
        
        cap prog drop _test3
        prog def _test3
        syntax varlist 
        local i=1
        gen Rbar=.
        foreach v of varlist `varlist'{
            sum `v'
            replace Rbar = r(mean) in `i'
            local i=`i'+1
        }
        
        sum Rbar
        gen abs_dev= abs(Rbar-r(mean))
        end
        When I type _test3 S1* in the start of the do file, I receive the following error code
        Code:
        unrecognized command:  _test3
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          When I type _test3 S1* in the start of the do file, I receive the following error code
          Code:
          unrecognized command: _test3
          Of course. By that time, _test3 is not yet defined. Put the call to _test3 below the program's end statement.


          Edit:

          When you call _test3 Stata looks for a build-in command with that name, does not find any, goes on down the adopath and looks for an ado-file with this name. If there was an ado-file called _test3.ado defining _test3 as the top-level command, Stata would run this file, define the program and you would not receive the error message.

          I see where the misunderstanding might come from. Within an ado-file, you may (and often should) call subroutines, that are defined below the top-level command, as in

          Code:
          program main
              subroutine
          end
          
          program subroutine
              code goes here
          end


          Here program main may call program subroutinedespite the fact that the latter's definition appears after the former's. This works, because when you call main from outside main.ado, Stata will look for main.ado and run the complete file, thereby defining all commands inside. By the time main is actually executed, subroutine is already defined.

          I hope this makes things a little clearer.

          Best
          Daniel
          Last edited by daniel klein; 13 Aug 2015, 03:02.

          Comment

          Working...
          X