Announcement

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

  • Issues Encapsulating a Custom Program with tempfile and postfile Usage

    Hello everyone,

    I've been attempting to encapsulate a sequence of statistical analysis and simulation steps into a custom Stata program. The goal is to streamline the invocation of repetitive analyses. However, after encapsulating and invoking the program, I encountered two main issues: an error related to a missing temporary file, and a non-symmetric corr() matrix error. Here is the core part of my encapsulated program:

    ********
    program define simulate_scenario
    args correlation placebo_3m placebo_12m intervention_3m intervention_12m
    clear
    tempname char
    tempfile char_file
    postfile `char' reps p3m_int p12m_int p3m_tot p12m_tot using `char_file', replace

    local nsize = 2200
    local interimsize = 750
    local alpha = 0.05
    local reps = 10
    local seed = 1000
    local n = 1

    while `n' <= `reps' {
    clear
    * Example rbinary command
    rbinary pain3m pain12m, means(`placebo_3m',`placebo_12m') corr(1,`correlation'\`correlation',1) n(`nsize') seed(`seed')
    * Skipping remaining simulation and analysis steps for brevity...
    local n=`n'+1
    local seed=`seed'+1
    post `char' (`n') (`p3m_int') (`p12m_int') (`p3m_tot') (`p12m_tot')
    }

    postclose `char'
    use `char_file', clear
    end
    ********
    When attempting to call this program, for example, with:

    simulate_scenario 0.33 0.25 0.20 0.146 0.084

    I encountered the following error messages:

    (file /var/folders/mm/tn03g0v1401d0dd9190q6px80000gn/T//S_72981.000001 not found)
    corr() matrix not symmetric
    r(505);

    It's worth mentioning that when I run the code directly without encapsulation through a program, everything works as expected. It seems the issues arise during the encapsulation process. I suspect it might be related to how temporary files are handled or the construction of the corr() matrix, but I'm unsure how to resolve this.

    Has anyone encountered similar issues or have suggestions for dealing with such scenarios? I attach my code

    Thanks in advance for your help!
    Attached Files

  • #2
    (file /var/folders/mm/tn03g0v1401d0dd9190q6px80000gn/T//S_72981.000001 not found)
    is not an error message. You specified the -replace- option in your -postfile- command, and Stata is just informing you that, at least at this point, the file did not yet exist, so no replace occurred. Specifying -replace- when it is not needed is not an error: but the message is given because the use of -replace- suggests you anticipated that the file might already exist, and it is prudent to alert you to the fact that it does not.

    If it had been an error message, it would have appeared in red, accompanied by an error code, and execution would have halted: Stata does not continue after error messages.


    Code:
    corr() matrix not symmetric
    r(505);
    That one is an error message. The part I don't understand is why it does work outside the -program- context, because -corr(1,`correlation'\`correlation',1)- is a subtle syntax error that is not relieved in any context. The problem is the sequence \`. Your intention here is that \ is the indication for a new row in the matrix and ` begins the reference to local macro correlation (defined in the -args- command). BUT, that is not what they are. The digraph \` is an "escape sequence" which is taken to be a literal ` character, i.e. one that prints as ` but is not taken to begin a macro. (Similarly the \ itself is not taken to be a \ character.) You can cure this simply enough just by inserting a blank space between \ and `. Then it should work OK. Again, though, I do not understand why this worked outside the -program-, because it is just clearly wrong no matter where it occurs.
    Last edited by Clyde Schechter; 07 Apr 2024, 22:50.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      is not an error message. You specified the -replace- option in your -postfile- command, and Stata is just informing you that, at least at this point, the file did not yet exist, so no replace occurred. Specifying -replace- when it is not needed is not an error: but the message is given because the use of -replace- suggests you anticipated that the file might already exist, and it is prudent to alert you to the fact that it does not.

      If it had been an error message, it would have appeared in red, accompanied by an error code, and execution would have halted: Stata does not continue after error messages.


      Code:
      corr() matrix not symmetric
      r(505);
      That one is an error message. The part I don't understand is why it does work outside the -program- context, because -corr(1,`correlation'\`correlation',1)- is a subtle syntax error that is not relieved in any context. The problem is the sequence \`. Your intention here is that \ is the indication for a new row in the matrix and ` begins the reference to local macro correlation (defined in the -args- command). BUT, that is not what they are. The digraph \` is an "escape sequence" which is taken to be a literal ` character, i.e. one that prints as ` but is not taken to begin a macro. (Similarly the \ itself is not taken to be a \ character.) You can cure this simply enough just by inserting a blank space between \ and `. Then it should work OK. Again, though, I do not understand why this worked outside the -program-, because it is just clearly wrong no matter where it occurs.
      Thanks a lot. it works! Wish u all the best!!

      Comment

      Working...
      X