Search code examples
sasrangemedian

Select median value per time range


I need to select a median value for each id, in each age range. So in the following table, for id = 1, in age_range of 6 months, I need to select value for row 2. Basically, I need to create a column per id where only median for each range is selected.

id      wt    age_range
1       22       6
1       23       6
1       24       6
2       25       12
2       24       12
2       44       18

Solution

  • If I understand correctly, you're looking to make a new column where for each id and age_range you have the median value for comparison. You could do this in base SAS by using proc means to output the medians and then merge it back to the original dataset. However proc sql will do this all in one step and to easily name your new column.

    proc sql data;
      create table want as
      select id, wt, age_range, median(wt) as median_wt
      from have
      group by id, age_range;
    quit;
    
    
    id  wt  age_range  median_wt
    1   24         6          23
    1   22         6          23
    1   23         6          23
    2   24        12        24.5
    2   25        12        24.5
    2   44        18          44