/***************************************************************************** Macro CalcMedian calculates a single median from a frequency distribution (a one-dimensional SAS ARRAY of "grouped data") by using linear interpolation to estimate the exact location of the median within the target class of the frequency distribution. Modification by Roy Williams at MISER (Mass. State Data Ctr.) of code originally produced by the California SDC. *****************************************************************************/ %MACRO CalcMedian (Median, N, LowBound, Frequency, TotFreq) ; /***************************************************************************** Parameters must be provided in the given order: Median (output) Estimated location of median (the name of the variable the result should be returned to) N (input) Number of classes in the frequency distribution LowBound (input) Array of size N, which contains the lower bounds of the N classes Frequency (input) Array of size N which contains the frequencies TotFreq (input) The total of the frequency distribution Local variables (just SAS vars with unlikely names, not %LOCAL Macro vars): _RunningSum_ is the running sum while searching for the target class _TargetFreq_ is the frequency below the median (1/2 of the total frequency) _Share_ is the estimated proportion of the target class above _TargetFreq_ _Width_ is the width of the target class *****************************************************************************/ IF &TotFreq = 0 THEN &Median = . ; /* empty frequency distribution */ ELSE DO ; _TargetFreq_ = &TotFreq * 0.5 ; /* Compute cumulative distribution until target exceeded */ _RunningSum_ = 0 ; DO i = 1 TO &N while (_RunningSum_ < _TargetFreq_) ; _RunningSum_ = _RunningSum_ + &Frequency[i] ; END ; i = i - 1 ; /* since loop goes one past the target class */ /* linearity assumption rarely reasonable in top and bottom classes */ IF i = &N OR i = 1 THEN &Median = . ; ELSE DO ; _Share_ = (_RunningSum_ - _TargetFreq_) / &Frequency[i] ; _Width_ = &LowBound[i+1] - &LowBound[i] ; &Median = &LowBound[i+1] - _Width_ * _Share_ ; END ; END ; %MEND CalcMedian ; /***************************************************************************** Sample call (Median Age of Latinos, both sexes, male & female, used in Census2000 SF1 profiles Age-2, AgeRace-H, and Race-H): (Macro definition must be placed BEFORE Macro call). ARRAY PCT12LowBounds [103] _TEMPORARY_ (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65, 66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90, 91,92,93,94,95,96,97,98,99,100,105,110) ; ARRAY P12LowBounds [23] _TEMPORARY_ (0, 5,10, 15,18, 20,21,22, 25,30,35,40, 45,50,55, 60,62, 65,67, 70,75,80,85) ; ARRAY P12Hmale [23] P12Hi3 -P12Hi25 ; ARRAY P12Hfeml [23] P12Hi27-P12Hi49 ; ARRAY P12HBoth [23] ; * if not already defined ; DO age = 1 TO 23 ; P12HBoth[age] = P12Hmale[age] + P12Hfeml[age] ; END ; IF Custom THEN DO ; * if 'Custom' is true (non-0) for this observation ; %CalcMedian (P13Hi1, 23, P12LowBounds, P12HBoth, P12Hi1) %CalcMedian (P13Hi2, 23, P12LowBounds, P12Hmale, P12Hi2) %CalcMedian (P13Hi3, 23, P12LowBounds, P12Hfeml, P12Hi26) END ; *****************************************************************************/