Hi there,
I am using Stata to calculate the median, 1st quartile, and 3rd quartile and then output to excel. It is part of a larger loop, so some of the variables are unique to the loop (`row', `q50', `q25, q`75')
The code I am using:
The code only partially works. It calculates the median, 25%-tile, 75%-tile for the whole data set. But the data set is split into two groups defined by the variable d_HGBL. 1 is the Increase group and -1 is the Decrease group. Stata seems to be correctly ignoring the values that are empty.
My output to Excel:

The data set for HSCRP. The data is
Regardless, Excel calculates the third quartile as 2.725 with QUARTILE.EXC and 2.375 as QUARTILE.INC.
It seems I am not the only one who has issues with Excel calculating the third quartile:
https://stackoverflow.com/questions/...ed-calculation
https://superuser.com/questions/3433...on-doesnt-work
https://www.reddit.com/r/excel/comme...tile_in_excel/
I am using Stata to calculate the median, 1st quartile, and 3rd quartile and then output to excel. It is part of a larger loop, so some of the variables are unique to the loop (`row', `q50', `q25, q`75')
The code I am using:
Code:
local row = 2 if d_HGBL == 1{ putexcel set results_dd.xlsx, sheet (test) modify summarize HSCRP, detail local q50 = r(p50) putexcel B`row' = "Increase group Median =", right putexcel C`row'= `q50' local row = `row'+1 local q25 = r(p25) putexcel B`row' = "Q25% =", right putexcel C`row'= `q25' local row = `row'+1 local q75 = r(p75) putexcel B`row' = "Q75% =", right putexcel C`row'= `q75' } else{ local row = `row'+3 summarize HSCRP, detail local q50 = r(p50) putexcel B`row' = "Decrease group Median =", right putexcel C`row'= `q50' local row = `row'+1 local q25 = r(p25) putexcel B`row' = "Q25% =", right putexcel C`row'= `q25' local row = `row'+1 local q75 = r(p75) putexcel B`row' = "Q75% =", right putexcel C`row'= `q75' }
My output to Excel:
The data set for HSCRP. The data is
Code:
. dataex HSCRP d_HGBL ----------------------- copy starting from the next line ----------------------------------------- copy up to and including the previous line ------------------ Listed 30 out of 30 observationsCode:* Example generated by -dataex-. For more info, type help dataex clear input double HSCRP float d_HGBL . -1 . -1 2.1 -1 1 -1 . -1 3.8 -1 .7 -1 1.5 -1 .2 -1 .5 -1 . -1 2.2 -1 1.8 -1 8.5 -1 .7 -1 1.1 -1 .4 -1 1 1 2.9 1 . 1 .3 1 . 1 2.9 1 . 1 1.5 1 . 1 . 1 1.7 . 14.7 . . . end label values d_HGBL d_HGBLlab label def d_HGBLlab -1 "Decrease", modify label def d_HGBLlab 1 "Increase", modify
It seems I am not the only one who has issues with Excel calculating the third quartile:
https://stackoverflow.com/questions/...ed-calculation
https://superuser.com/questions/3433...on-doesnt-work
https://www.reddit.com/r/excel/comme...tile_in_excel/
Comment