Announcement

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

  • Postfile command posts variable values instead of variable names

    Hi there,

    I am trying to use the -postfile- command to summarize multiple regression outputs into a table.

    Code:
    tempname results;
    postfile `results' str10 outcome coef1 using "file", replace;
    
    foreach y of varlist *_gap {;
    
    qui reg     `y'  var1, robust;
    post `results' (`y') (_b[var1]);
    
    };
    postclose `results';
    This code doesn't run through because of "type mismatch" for the variable "outcome". If I remove the "str10" type requirement, the code runs through but the posted dataset has the average value of each `y' rather than the name of each `y' (which is what I want).

    I would appreciate any suggestions on my code or an alternative method to summarize the result.

  • #2
    Code:
    post `results' (`"`y'"') (_b[var1])

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      post `results' (`"`y'"') (_b[var1])
      Thank you Clyde! This solved the problem. Just for my better understanding, could you please explain what is the function of the outmost ` ' ?

      Comment


      • #4
        The digraphs `" and "' are a peculiarity of Stata called compound double quotes. (If any other software uses them, I am not aware.) These were invented to solve a problem: because there is no distinction between the opening " and the closing " character (except in packages that use so-called smart quotes, which are not ASCII characters), you cannot nest quoted expressions, because in such an expression there is no way to know if the second " encountered marks the end of the first quote or the beginning of a second, embedded quote.

        Sometimes it is necessary to process strings that, themselves, contain quoted material, so this is a problem. Stata's solution is to use `" to begin a quote and "' to end a quote. You can still use plain old " and ", so long as what's between them does not, itself, contain any quotes. And, in fact, in your particular situation, `y' is always the name of a variable, so it is guaranteed not to contain any quotes. So I could have just recommended ("`y'") and that would work just as well. The reason I didn't is that it is a better coding practice to always use the compound double quotes to work with quoted material in Stata, because you will never get into any trouble that way, and the two extra keystrokes are a very small price to pay for that. Someday you may do something similar to what you are doing in #1 and `y' may iterate over things that do contain quoted material. In that situation, without the `" and "', the code would break.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          The digraphs `" and "' are a peculiarity of Stata called compound double quotes. (If any other software uses them, I am not aware.) These were invented to solve a problem: because there is no distinction between the opening " and the closing " character (except in packages that use so-called smart quotes, which are not ASCII characters), you cannot nest quoted expressions, because in such an expression there is no way to know if the second " encountered marks the end of the first quote or the beginning of a second, embedded quote.

          Sometimes it is necessary to process strings that, themselves, contain quoted material, so this is a problem. Stata's solution is to use `" to begin a quote and "' to end a quote. You can still use plain old " and ", so long as what's between them does not, itself, contain any quotes. And, in fact, in your particular situation, `y' is always the name of a variable, so it is guaranteed not to contain any quotes. So I could have just recommended ("`y'") and that would work just as well. The reason I didn't is that it is a better coding practice to always use the compound double quotes to work with quoted material in Stata, because you will never get into any trouble that way, and the two extra keystrokes are a very small price to pay for that. Someday you may do something similar to what you are doing in #1 and `y' may iterate over things that do contain quoted material. In that situation, without the `" and "', the code would break.
          Thank you very much for your clear and detailed explanation! I hope you have a good day.

          Comment

          Working...
          X