Dear all,
I am trying to create a loop using two local arrays and numbers.
Here is what I have so far:
And ultimately, the output will look something like this on excel. (Numbers are not important, I just wanted to show what the final product would look like).
The first part of my 'if' statement is inserted to create the excel file with the first row, Total Discharges. If the excel file already exist, it is supposed to move on to the `else' section, and for each element of my array var1, it should calculate the totals and put it in the next line of my excel file. The tricky part is replacing "var" to equal the local macro "output" depending on which `var1' was being calculated in the subpop. For example, if the loop ran svy:total dischgs, subpop(pnuemo), I would like for var to be replaced to equal "MRSA pneumoniae" (the first element in BOTH arrays). In PHP / Java, elements of an array can be identified with brackets; replace var=`"output[2]"' if `'"name[2]". Is it possible to do something like this in Stata or get around this in another way?
Also, how can I change the export excel command to keep increasing by 1, i++, until the loop itself finishes without knowing how many times the loop will need to run. I'm not sure how many excel rows will need to be added, therefore I don't think a statement like i=1/30, makes a lot of sense.
Thanks so much for your help.
I am trying to create a loop using two local arrays and numbers.
Here is what I have so far:
Code:
svyset [pweight = trendwt], strata (nis_stratum) psu (hospid) local var1 "pneumoall pneumo sepsisall sepsis otherall other mrsa sensitive resistant" localoutput`""S. aureus pneumonia" "MRSA pneumoniae" "S. aureus Septicemia" "MRSA Septicemia" "S. aureus Others" "MRSA Others" "MRSA Totals" "Total MRSA Infections" "Total MSSA Infections""' tempfile alldata2010 save "`alldata2010'" capture confirm file "Results.xls" foreach name of local var1 { forvalues i=1/30 { if _rc!=0 { svy: total dischgs regsave, ci gen year=2010 replace var="Total Discharges" export excel using "Results", firstrow(variables) } else { use `alldata2010', replace svy: total dischgs, subpop(`"name"') regsave, ci gen year=2010 replace var=`"output"' if export excel using "Results", sheet("Sheet1") sheetmodify cell(A`i')
|
Also, how can I change the export excel command to keep increasing by 1, i++, until the loop itself finishes without knowing how many times the loop will need to run. I'm not sure how many excel rows will need to be added, therefore I don't think a statement like i=1/30, makes a lot of sense.
Thanks so much for your help.