Announcement

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

  • export the data generated in each iteration to a single Excel file, with each iteration on a different sheet identified by its name

    I'm facing an issue with the putexcel command in Stata. In my
    agent-based modeling project with a while loop, each iteration analyzes
    the movements of agents in numerical units. I would like to export the
    data generated in each iteration to a single Excel file, with each
    iteration on a different sheet identified by its name. The challenge is
    that I don't know how many values will be generated in each iteration,
    making it impractical to use the putexcel command conventionally. I've
    attempted to use a foreach loop to assign values to consecutive cells in
    the corresponding sheet, but haven't achieved the desired results. Can
    anyone suggest the correct approach to address this issue?

    I present below an example of an option I have tried but it doesn't work
    for me, since it only saves the values in cell A1 and keeps overwriting:

    local i = 1
    putexcel set "out_step.xlsx", sheet("Iteration`iteration'", replace)
    modify

    foreach value in p_1 {
    putexcel A`i' = `value'
    local i = `i' + 1
    }

  • #2
    There are several problems with the code you show.

    Your -putexcel set- command attempts to set the worksheet within out_step.xlsx by reference to a local macro `iteration', which does not exist. So you are repeatedly writing to sheet "Iteration", and that never changes.

    Next, you have a loop where the iterator, value, only ever takes on the single value p_1. So, it is executed only once, and the value written into cell A1, namely p_1 (which is also never defined in the code) never changes.

    While I can easily spot these problems, I can't really suggest how to fix them because I cannot discern from what you have written just what the output you are trying to transfer to the spreadsheet is. So I think a fuller explanation of "the data generated in each iteration" is, accompanied by example data shown using the -dataex- command would greatly increase the chances that somebody will be able to give you more specific advice.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thanks

      Comment


      • #4
        Regarding your message, I would like to point out that the code snippet
        I provided in the previous message was intended to illustrate a specific
        approach for saving data within the same Excel file on different sheets.
        It was meant to be concise without including the entire code, as the
        rest of the code does not address this particular issue. The variables
        can be inferred from their names; for instance, "iterator" is a local
        variable keeping track of the iterations in the model. It changes names
        each time the line of code "putexcel set "out_step.xlsx",
        sheet("Iteration`iteration'", replace) modify" is executed, creating a
        new sheet within the "out_step.xlsx" Excel file named "Iteration1,
        Iteration2," and so on.

        Similarly, the variable "p_1" is a vector containing all the data
        generated for each iteration. I aim to save it in its corresponding
        sheet in the "out_step.xlsx" Excel file. However, the provided code
        snippet doesn't seem to save the data correctly, and the reason for this
        is not clear.

        As observed in the code snippet, a local variable "i" is created to
        serve as a counter, assigning names to sheets like A`i'. In the first
        iteration, it is A1, and for each value in the vector p_1, it increases
        by +1. Therefore, it should save each value of "p_1" in column A:A of
        the Excel file, varying in cells from A1 to A`total number of values in
        the vector p_1'. I would like to add that the variables p_1, p_2, p_3,
        p_4, and p_5 are the names of the columns in an imported Excel file.

        I am also providing the code where each iteration is generated for you
        to review the entire process:

        ```stata
        while `continue' {

        local iteration = `iteration' + 1

        twoway scatter p_1 p_2 p_3 p_4 p_5 v_i, title("Iteration
        `iteration'") ///
        ytitle("Variables p_1 to p_5") xtitle("Initial vote of each
        agent") ///
        name("grafico`iteration'", replace)

        replace p_1 = p_1 + `media_effect_p_1'
        replace p_2 = p_2 + `media_effect_p_2'
        replace p_3 = p_3 + `media_effect_p_3'
        replace p_4 = p_4 + `media_effect_p_4'
        replace p_5 = p_5 + `media_effect_p_5'

        * Applying Downs proximity distance
        foreach var in p_1 p_2 p_3 p_4 p_5 {
        local distance_`var' = -(v_i - `var')^2
        replace chosen_party = distance_`var' if distance_`var' ==
        min(distance_p_1, distance_p_2, distance_p_3, distance_p_4,
        distance_p_5)
        }

        * Reevaluate the vote preference after Downs proximity and media
        effects
        foreach var in p_1 p_2 p_3 p_4 p_5 {
        replace chosen_party = `var' if `var' > chosen_party
        }

        local i = 1
        putexcel set "out_step.xlsx",sheet("Iteracion`iteration'", replace)
        modify

        foreach value in p_1{
        putexcel A`i' = `value'
        local i = `i' + 1
        }

        * Check if any party reaches an absolute majority
        local majority = _N/2+1
        foreach var in p_1 p_2 p_3 p_4 p_5 {
        count if chosen_party == `var'

        if r(N) > `majority' & `iteration' >= 20 {
        display "Absolute majority reached by party `var'"
        local continue = 0
        break
        }
        }

        count if chosen_party != previous_chosen_party
        if r(N) == 0 & `iteration' >= 20 {
        display "No agent changed their mind. Ending the simulation."
        local continue = 0
        }

        * Update the variable for the previous vote for the next iteration
        replace previous_chosen_party = chosen_party

        * If there are no changes or the majority is reached, end the
        simulation
        if `continue' == 0 & `iteration' >= 20 {
        break
        }
        }
        ```

        Please review the entire code and let me know if you have any specific
        questions or if there are further adjustments needed.

        Comment


        • #5
          OK, the problem seems to be arising from
          Code:
          foreach value in p_1{
          putexcel A`i' = `value'
          local i = `i' + 1
          }
          Now that you have explained more fully, I infer that you want to copy successive values of the Stata variable p_1 into successive cells of column A in the spreadsheet. But that is not even close to what -foreach value in p_1- does. What you want to do is accomplished instead with:
          Code:
          forvalues i = 1/`=_N' {
              putexcel A`i' = p_1[`i']
          }

          Comment

          Working...
          X