Announcement

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

  • combine multiple lines of codes into one command!

    Hey Statalist,

    I want to find a way in which I can combine multiple lines of codes into one single command. for example, I have many regressions, and I want to run certain commands after each regression. I want to save the workspace and just write one command to run all the same lines at once. Any hint would be appreciated.

    ps: one of the codes after each regress is that I want to store each regression's results. So I want to assign each regression a unique name (model1, model2, etc.).

    Thanks,

  • #2
    Let's be clear at the outset: there is no way in Stata to combine this all into just a single command.

    That said, there are plenty of ways in Stata to shorten the code and avoid having to write out every single one of them. The most appropriate way, however, depends on what the relationships among these commands are. If the regressions all use the same variables but are carried out on different subsets of the data, there is one way to do it. If the regressions all use the same data but differ as to which variables are included, another way is available. And there are other possibilities. So you need to provide more information about what you want to do. Probably the most helpful thing you could do to get useful advice is to show the actual code you are trying to shorten, or if it is very long, show enough of it that the pattern among them will be evident.

    Comment


    • #3
      Without disagreeing with Clyde Schechter, let's underline that you could put all your commands in one do-file or program and then you have a one-line answer. But I am puzzled by the question, as I think you want something else

      Comment


      • #4
        Cross-posted at https://www.reddit.com/r/stata/comme...o_one_command/

        Please note our policy on cross-posting, which is that you are asked to tell us about it. https://www.statalist.org/forums/help#crossposting

        Comment


        • #5
          So if I'm not wrong, there is no way to combine multiple lines of codes and run it with only one single command, to save time and space.

          Here are my codes:

          Code:
          global num = 1
          
          regress y on some x
          outreg2 using model_$num
          global num = $num + 1
          
          reg y on some other x
          outreg2 using model_$num
          global num = $num + 1
          What I want to do is to combine the two lines after each regression. something like:

          Code:
          regress ...
          newcommand
          where the
          Code:
          newcommand
          automatically does the 2 lines of the code each time!

          I tried both 'global' and 'program' commands to generate something, however, it was not successful!


          Thanks, Nick Cox for providing the link here.
          Last edited by Sa Fe; 07 Nov 2021, 15:21.

          Comment


          • #6
            Code:
            local xvars list_of_variables_to_be_used_as_x_here
            
            capture program drop my_regression
            program define my_regression
                  syntax varname
                  regress y `varlist'
                  estimates store `varlist'
                  exit
            end
            
            foreach x of local xvars {
                 my_regression `x'
            }
            This will carry out all of the regressions and will store all of the estimates. The name of each set of estimates will be the name of the x-variable for that regression, rather than a number. This will probably be more convenient than indexing the estimates by numbers: you won't have to remember which number corresponds to which variable, and you avoid the unsafe programming practice of using a global macro (or, alternatively, making my_regression slightly more complicated so you can pass the number to it as an argument.)

            Comment


            • #7
              Thanks, Clyde Schechter,

              But I'm a little bit confused here. There are [ic] xvars [\ic], [ic] varname [/ic], and [ic] varlist [/ic]. I know [ic] xvars [/ic] is related to the [ic] foreach [/ic] command. Though, I have no idea about the other two.

              Comment


              • #8
                Yes, that's a bit confusing when you're not very familiar with Stata programs. Let me try to unpack it.

                xvars is just a name that I chose for the list of variables. I chose that name for its mnemonic value. I could have called almost anything.

                -varname- in the -syntax- command is a Stata keyword. The command -syntax varname- tells Stata that when this program is invoked, it should expect the command name to be followed by the name of one, and only one, existing variable. Another effect of that command is that whatever the name of that one variable turns out to be, it is placed in a local macro named varlist. (That is part of the way the -syntax- command works: you cannot chose to have varlist called something else.)

                By the time we get to the -regress- command, local macro varlist contains the name of one of the x-variables listed in the first line of code. So -regress `varlist'- (note the ` and ' around -varlist-) tell Stata: to run this -regress- command, look inside local macro varlist and see what it contains. Then substitute that into the -regress- command after the y. And in the -estimates store- command, the same thing happens: the contents of local macro varlist are retrieved and substituted in the place where -estimates store- sees `varlist' and expects the name of a new set of estimates.

                Next, there is the -foreach- command. Here is where xvars, which was created at the top of the code, is actually used. -foreach x of local whatever- tells Stata that all of the code between the curly braces {} is to be executed repeatedly. The first time, whatever is the first "word" in the contents of the local macro named whatever is retrieved and placed in a new local macro x. Thereafter, inside the braces, any mention of `x' in the code gets substituted by the current contents of local macro x. In this particular code, the local macro whatever is local macro xvars, which contains a list of variables in your data set. So on the first time through, the first listed variable name is copied into local macro x. Then the `x' in the -my_regression `x'- command is replaced by that first variable name, and program my_regression is executed with that variable name as its argument. This process is then repeated once for each "word" in the local macro xvars until the end of the list is reached. By the way, the use of x here was my choice, again I could have called it almost anything, and, again, I chose x for mnemonic reasons.

                It takes a bit of practice to get used to how local macros and loop structures and the -syntax- command all work. But sooner or later, usually sooner, it "clicks" and everything becomes suddenly clear.
                Last edited by Clyde Schechter; 07 Nov 2021, 17:18.

                Comment


                • #9
                  I do not fully understand the issue here but I note that in certain cases (e.g., within -mi xeq-) one can combine several commands on one "line", separating them by semi-colons; since I don't fully understand what is wanted and have virtually no understanding of why it is wanted, I don't comment any further

                  Comment


                  • #10
                    Thanks for your comprehensive explanation, Clyde Schechter.

                    To test this command, I have used the auto data. Here is my codes:

                    Code:
                    sysuse auto, clear 
                    local xvars weight make
                    
                    capture program drop my_regression
                    program define my_regression
                          syntax varname
                          regress mpg `varlist'
                          estimates store `varlist'
                          exit
                    end
                    
                    foreach x of local xvars {
                         my_regression `x'
                    }
                    And I get the following error:
                    no observations
                    r(2000);

                    What is the problem with this?

                    Comment


                    • #11
                      Originally posted by Rich Goldstein View Post
                      I do not fully understand the issue here but I note that in certain cases (e.g., within -mi xeq-) one can combine several commands on one "line", separating them by semi-colons; since I don't fully understand what is wanted and have virtually no understanding of why it is wanted, I don't comment any further
                      Thanks, Rich Goldstein, that was helpful.

                      Comment


                      • #12
                        local xvars weight make
                        The variable make is string, you can not use it as regressor.

                        Comment


                        • #13
                          Thanks Chen, it worked.

                          Comment

                          Working...
                          X