Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65649

Fun loop challenge

$
0
0
Dear all,

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')  
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).
var coef stderr ci_lower ci_upper N year
Total Discharges 3225712 734.9375 38188 3836 7441 2010
S. aureus pneumonia 2345.05469 365.130859 836.6875 9601.42188 7441 2010
MRSA pneumonia 348.12891 233.0855 599.40625 6806.85156 7441 2010
S. aureus Septicemia 12221.52344 244.5088 864.78125 9560.26563 7441 2010
MRSA Septicemia 11248.63672 1853.1337 456.69531 50.57813 7441 2010
S. aureus Others 33044.9688 146.44727 4053.875 5236.0625 7441 2010
MRSA Others 32714.9375 745.66748 2963.0938 336.7813 7441 2010
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.




Viewing all articles
Browse latest Browse all 65649

Latest Images

Trending Articles



Latest Images