Announcement

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

  • Appending results to an existing file

    I have written a very simple program to conduct a lottery among nine people. The results of a given run look like this.
    Code:
    
    "Dave"   .9838091 9
    "Chuck"  .9698822 8
    "Jim"    .9612048 7
    "Karen"  .8696807 6
    "Katy"   .8337011 5
    "Shane"  .6246807 4
    "Dick"  .50225854 3
    "Bill"  .21390353 2
    "Sue"   .16608635 1
    end


    Variable draw comes from a call to runiform()

    I'd like to see how this little routine behaves over a number of trials, say a thousand. I thought that a simple way to do it would be to run a foreach loop, saving the results in a .dta file from the first trial and then appending subsequent results to the existing file. Then I could just look at the distribution of ranks by name. But the Stata save command does not allow one to append to an existing file. In fact, no Stata output command, e.g. export excel appears to allow one to append so far as I can tell. I looked at the post command, but I don't think it will do what I want to do either. So, how can I run multiple trials of this, each time saving the results for all nine of the cases? Clearly, I'm missing something obvious here, but I don't know what it is.
    Richard T. Campbell
    Emeritus Professor of Biostatistics and Sociology
    University of Illinois at Chicago

  • #2
    I am not clear re: what you are trying to do but suggest you look at:
    Code:
    h simulate

    Comment


    • #3
      Thanks Rich. I thought of simulate, but it will save, as I understand it, the results of a statistical result such as a mean or regression coefficient, computed across an entire data set, or perhaps a subset of it. I want to save an entire data set or, put perhaps more clearly, the results of nine random draws, one for each case in the the file.
      Richard T. Campbell
      Emeritus Professor of Biostatistics and Sociology
      University of Illinois at Chicago

      Comment


      • #4
        Code:
        clear*
        tempfile building
        save `building', emptyok
        
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str12 name
        "Dave"
        "Chuck"
        "Jim"  
        "Karen"
        "Katy"
        "Shane"
        "Dick"
        "Bill"
        "Sue"  
        end
        
        set seed 1234
        local n_names = _N
        
        forvalues i = 1/1000 {
            gen double shuffle = runiform()
            sort shuffle
            gen int rank = _n
            drop shuffle
            append using `building'
            save `"`building'"', replace
            keep in 1/`n_names'
            drop rank
        }
        
        use `building', clear
        Added: Crossed with #2 and #3

        Comment


        • #5
          Thanks Clyde! Works like a charm.
          Richard T. Campbell
          Emeritus Professor of Biostatistics and Sociology
          University of Illinois at Chicago

          Comment

          Working...
          X