Announcement

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

  • reusing lines of code, similar to javascript function

    I have some lines of code in a STATA do file that I would like to reuse/execute and different points in the do file. Similar to a javascript function... however I do not necessarily need an input variable.

    for example:

    I have some code:

    for x in test1 test2 test3{
    rename my_`x' mynew_`x'
    }


    and i want to execute those 3 lines of code at various points in the do file. As if there was a way to label those three lines "codeA" and then "execute codeA" later in the do file.
    Any suggestions?

  • #2
    Cross-posted here: http://stackoverflow.com/questions/2...cript-function

    Please follow recommendations given in the FAQ, which include use of full real names and explicit mention of cross-posts.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      Originally posted by As3ad Rob View Post
      I have some lines of code in a STATA do file that I would like to reuse/execute and different points in the do file. Similar to a javascript function... however I do not necessarily need an input variable.

      for example:

      I have some code:

      for x in test1 test2 test3{
      rename my_`x' mynew_`x'
      }


      and i want to execute those 3 lines of code at various points in the do file. As if there was a way to label those three lines "codeA" and then "execute codeA" later in the do file.
      Any suggestions?

      You can do this by defining a program within your do-file. You need to choose a name for your program which is not already a Stata command. Let's say you want to call it myprog. In Stata, type which myprog to verify that myprog is not already an existing Stata command.

      Next, near the top of your do-file, define your program:

      Code:
      capture program drop myprog
      program myprog
          for x in test1 test2 test3 {
              rename my_`x' mynew_`x'
          }
      end
      The first line, capture program drop myprog ensures that when you re-run your do-file, it won't exit with an error telling you that you already defined myprog in a previous run.

      Then, later in your do-file, you can use myprog just like any other command.

      Comment


      • #4
        FWIW this particular example does not require a loop and is better written as the one line

        Code:
        ren (my_text(#)) (mynew_text(#))
        Even if the names of the original variables is not test, something along the lines

        Code:
        ren my_* mynew_*
        should still work. It's amazing what rename group can do and I highly recommend exploring it more closely.

        Best
        Daniel

        Comment


        • #5
          You could take Daniel's example and store it in a local/global macro and then execute it by calling the macro.

          Comment

          Working...
          X