We also repeat the variables ragegrp and quizgrp in our original three lines of code. Stata will run through the loop three times, inserting the value 1 in place of `i' the first time, 2 in place of `i' the second time, and 3 in place of `i' the final time.
#Putexcel stata 13 code
Type the following code in the do-file, which refers to the values as “i,” remembering to close the loop using a curly bracket on a separate line as below: The values appear twice in each line, the first time for the level of the quiz group variable and the second time in the name we give to distinguish the matrices we are creating. We can use the “forvalues” command to loop through these three consecutive values and produce the three lines of code without typing each element in turn. Note that we are repeating the instructions for three values, 1, 2, and 3. tab ragegrp quizgrp if quizgrp=3, matcell(results3).tab ragegrp quizgrp if quizgrp=2, matcell(results2).tab ragegrp quizgrp if quizgrp=1, matcell(results1).The first instruction tabulates age-group and quiz group for those observations where the value of the quiz group variable is 1 (low score) and creates a matrix of results we call “results1.” The next does the same for the middle group, coded 2, and the last instruction does so for the group with the highest scores, coded 3:
In a previous example, we created three matrices of results by cross-tabulating age-group by quiz group for each of the quiz group categories separately, and we used putexcel to write formulas which calculate percentages within Excel. If it is open, you will get an error message, which can be resolved by closing the document and running the do-file again.Īll commands must start with “putexcel” or, if you do not wish to see any output in the Stata window, “quietly putexcel.” Note that in order for Stata to save any changes to the Excel file, it must be closed. If you would like to make changes to an existing document, use “modify” instead. It is helpful to use “replace” in this exercise as we run the do-file several times. We use the “replace” option, which means that Stata will write over the file if it already exists or create a new one if it does not.