Announcement

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

  • Can I create cells/blocks in the .do file so that when I run it, it only runs that code block?

    Just started learning and using Stata, so far used python. Trying to figure out whats mentioned above as its quite cumber some to keep highlighting a large section of code everytime I want to run a block.

  • #2
    Well, you could comment out everything but the block you want to run. If you highlight the lines you want to exclude and press Ctrl-/, that will comment it out. Later, when you want to bring that back, just highlight and type Ctrl-/ again, and it will be uncommented.

    Comment


    • #3
      Can you give an example of when you would want it to only run a certain block of code? Like when would you want this?

      Comment


      • #4
        For when I a testing out a certain set of commands, I would prefer for only these to run. Example a block on data cleaning. A block of defining my variables, a block on something else. That makes it a little convenient to run just that code and troubleshoot.

        Comment


        • #5
          Stata doesn't have the feature that R does. But honestly, this shouldn't matter very much. Your code should be able to run top to bottom without every time, so if I ever wanna run up to a certain part, I just do
          Code:
          reg y x
          0
          So Stata runs until that point. Technically, you can do
          Code:
          
          // Data Analysis
          
          {
          
          
          reg y x
          
          
          }
          compress the braces and run this as a block. Not as elegant as R, but it does what you want.

          Comment


          • #6
            Probably not very advanced, but I also just put a word that is not a Stata command such as Stop, where I want to stop code from executing.

            If I want to skip on some code above I comment is out /* */.

            Comment


            • #7
              Based on #4, it sounds like Dev is talking about Jupyter notebooks; a relatively new innovation in anaconda python that resembles a marginally smarter version of R markdown. I think it might be helpful to point out that there is an important difference between the language (python) and the text editing software that is used to write code (in this case Jupyter). Executing code in a notebook style environment is definitely useful in data science, but it is not the way python has typically been used over its lifespan, and jupyter notebooks would be downright cumbersome when doing software development. I think this might help to explain some of the confusion around what OP is asking about.

              Jupyter is indeed a small improvement in quality of life for data science, especially if you've ever written python in an IDE like pycharm or a text editor like emacs or (if you are especially unlucky) IDLE. I don't know of such a technology for Stata, but regardless, I don't happen to think notebooks provide all that much more quality of life. OP just needs some time to get used to a different workflow. Wishing a new language and the surrounding technology was more like something you are already familiar with is a common experience when learning a new language. It takes 1 to 2 years to really get used to a new system. Feeling frustrated with the new technology is natural during the learning curve period. Just give it some time.

              Comment


              • #8

                First, note: running code snippets from a do file are like copying the lines into a separate file; only locals defined within the marked lines are defined:
                Code:
                local a A
                local b B
                
                {   // running this block only return assertion is false
                 
                    assert "`a'" != "`b'"  
                }
                Second, a structure often used is to:
                • Write short scripts that do one task each.
                • Define a master.do which call (do/run/include) a sequence of one-task dofiles.
                • Make a directory structure to organize files in project.
                Do files can be nested, and take arguments see [U] 16 Do-files.

                Comment


                • #9
                  Originally posted by Daniel Schaefer View Post
                  ...jupyter notebooks would be downright cumbersome when doing software development. ...

                  Jupyter is indeed a small improvement in quality of life for data science... I don't know of such a technology for Stata, but regardless, I don't happen to think notebooks provide all that much more quality of life. ...
                  For Stata 17, there's now nbstata (which builds on past open-source development of a Jupyter kernel for Stata). It's fairly young but already boasts better autocomplete (in some ways) than Stata's default version, in addition to the other quality-of-life features of notebooks. I agree the advantages of notebooks are small, on balance--I tend to go back and forth between the official Stata UI and working in a Stata notebook, myself, depending on the task.

                  p.s. For python software development in notebooks, I'd recommend considering nbdev, which I used to build nbstata.

                  Comment

                  Working...
                  X