I am trying to find the minimum value within a group at a point in time in SAS. For example, I have the following dataset
ID NO| DATE |RESULT
1 |28NOV2015 |5.6
1 |25JAN2016 |4.8
1 |8MAR2016 |5.2
1 |14MAY2016 |5.0
1 |20JUL2016 |4.3
1 |18SEP2016 |3.8
2 |20AUG2012 |8.6
2 |14NOV2012 |7.2
2 |03JAN2013 |5.5
2 |12MAR2013 |5.3
I would like to programatically create a new column that will return the minimum result within the ID number group for values that occurred prior to the current value. The resulting output should look as follows:
ID NO| DATE |RESULT |PMINVAL
-----|------------|-------|--------
1 |28NOV2015 |5.6 |5.6
1 |25JAN2016 |4.8 |5.6
1 |8MAR2016 |5.2 |4.8
1 |14MAY2016 |5.0 |4.8
1 |20JUL2016 |4.3 |4.8
1 |18SEP2016 |3.8 |4.3
2 |20AUG2012 |8.6 |8.6
2 |14NOV2012 |7.2 |8.6
2 |03JAN2013 |5.5 |7.2
2 |12MAR2013 |5.3 |5.5
I tried to accomplish this by using a lag function in combination with some conditional statements. I do not think this approach will work since the lag function when used with conditionals evaluates to the last record where the condition was met, which is what is desired in this case. I first created an additional column that defined the "baseline" or first record in each ID group to use as a reference for the first two PMINVAL records since these will always be the baseline result value.
data MDX2;
set MDX1;
count + 1;
by ID_NO;
if first.ID_NO then count=1;
run;
data MDX3;
set MDX2;
if count <=2 then PMINVAL=BLRESULT;
if count >2 and lag(PMINVAL)<=lag(RESULT) then PMINVAL=lag(PMINVAL);
if count >2 and lag(PMINVAL)>lag(RESULT) then PMINVAL=lag(RESULT);
run;