Announcement

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

  • Postfile __000000 already exists

    Hi

    I am running Stata 15.1 on a Windows 7 8 core desktop. I am trying to run postfile but keep getting the following error

    postfile __000000 already exists

    The data is one row per individual and a
    ID Study HomeMed_affect_drug HomeMed_mood_drug HomeMed_adhd_drug HomeMed_any_psych_drug HomeMed_any_benzo

    123 1 1 1 0 1 1


    The problematic code is below


    version 15.1
    tempname handle

    loc nlist "HomeMed_affect_drug HomeMed_mood_drug HomeMed_adhd_drug"

    di "`nlist'"

    foreach x of local nlist {
    di "`x'"

    forvalues f=1/16 {

    postfile `handle' draw `x'_pop `x'_sample_1 `x'_sample_0 `x'_rp using `x'test1 ,replace every(1)

    count if `x' ==0 & draw ==`f'
    loc `x'_sample_0 =r(N)

    count if `x'==1 & draw ==`f'
    loc `x'_sample_1= r(N)

    count if `x'==1
    loc `x'_pop =r(N)

    tab `x' study_subject if draw ==`f' |draw ==. ,ex
    loc `x'_rp =r(p_exact)

    post `handle' (`f') (``x'_pop') ( ``x'_sample_1') (``x'_sample_0') ( ``x'_rp')

    }
    }

    postclose `handle'


    I tried inserting a postutil clear statement before the postfile command per a statalist poster but it results in only a single row being posted. A version of this code with just one variable works without problems and saves 16 rows to the posted file.

    Has anyone encountered this before and come up with a solution? In the example, I have listed 4 variables but I have more than 4 in the full dataset.

    Many thanks

    Paul

    Paul Walsh






  • #2
    The -postfile- command should not be inside the loop. It should precede it, and should be preceded by -capture postutil clear-. By placing it inside the loop you are asking Stata to re-create the file from scratch at each iteration of the loop, which Stata does, so that only the results from the final iteration survive. Only the -post `handle'...- command belongs inside the loop. -postfile- precedes it, and, as you have it, -postclose- follows it.

    Comment


    • #3
      Solved. Thank you so much. Here is the correct code in case anyone else in the future needs it or has been following this thread

      Paul




      version 15.1
      tempname handle

      loc nlist "HomeMed_affect_drug HomeMed_mood_drug HomeMed_adhd_drug"

      di "`nlist'"
      foreach x of local nlist {
      di "`x'"
      postutil clear
      postfile `handle' draw `x'_pop `x'_sample_1 `x'_sample_0 `x'_rp using `x'test1 ,replace every(1)

      forvalues f=1/16 {


      count if `x' ==0 & draw ==`f'
      loc `x'_sample_0 =r(N)

      count if `x'==1 & draw ==`f'
      loc `x'_sample_1= r(N)

      count if `x'==1
      loc `x'_pop =r(N)

      tab `x' study_subject if draw ==`f' |draw ==. ,ex
      loc `x'_rp =r(p_exact)


      post `handle' (`f') (``x'_pop') ( ``x'_sample_1') (``x'_sample_0') ( ``x'_rp')


      }

      }
      postclose `handle'

      /////////////

      Comment

      Working...
      X