You "example" doesn't show any repeats of values for "lab month" so I am not quite sure what you mean. Your example output seems to imply that the high low and mean are calculated regardless of "month".
Here is a way to get the the min, max and mean plus an identification date for the high value.
Please note that I made a LOT of guesses about the content of your data to make a working data step example. The data step would is only needed to have some data that works with the code.
data work.have;
infile datalines dlm='|' dsd;
input patient_id :$6. pt_name :$10. lab_date :yymmdd10. lab_type :$5. result normal_range :$15. visit_description :$15. HD date_HD :yymmdd10.;
format lab_date date_HD yymmdd10.;
datalines;
01234|Smith, J|2023-10-23|gfr|30|60-100|chest pain 1|0|.
01234|Smith, J|2024-02-23|gfr|20|60-100|dyspnea|0|.
01234|Smith, J|2024-10-21|gfr|10|60-100|asymptomatic|1|2023_10_30
01234|Smith, J|2023-07-23|trop|0.06|<0.010|chest pain 5|0|.
01234|Smith, J|2024-01-20|trop|0.08|<0.010|dyspnea|0|.
01226|wong, J|2023-04-23|gfr|15|<0.010|chest pain 4|1|2023_04_30
01226|wong, J|2024-2-20|trop|0.08|<0.010|dyspnea|0|.
;
proc sort data=work.have;
by patient_id Pt_name lab_type lab_date;
run;
proc summary data=work.have nway;
by patient_id Pt_name lab_type ;
var result;
output out=work.examplesummary (drop=_type_) min = mean= max=
idgroup (max(result) out[1] (lab_date) = date_max)
/autoname autolabel
;
run;
The Sort is so we can process like lab_type values together per patient as that appears to be what you want.
Proc summary is used to get the min/mean/max (and other statistics if desired like STD, RANGE, percentiles) and uses the IDGROUP option to get the date for the max resulting score.
The Autoname means the output variables are named by suffixing the statistic to the variable (in case you have multiple variables that might need summary at the same time then you don't have to be verbose in your code unless you want to). The Autoname applies to any statistic or option output requested that yo u do not supply a name for. Without specific requests per variable all VAR variables would have the MAX MIN and MEAN calculated
The IDGroup will allow getting one or more id variables associated with one or more max or min values of the var variable(s).
I suspect that what you want is this data set and then merge it back to your original data by Patient_id Pt_name and Lab_type.
The only way to stick multiple values into a single variable is a data step and has been answered on this forum multiple times. So perhaps do that as a step by Patient_id Pt_name and Lab_type and combine this output with that result.
Since you didn't provide any rules about what that HD variable is supposed to be I am not sure where to place it. It might belong is the Proc Summary as a var variable asking for the max which would require some modification to the OUTPUT statement.
The Drop removes a variable that indicates which combinations of the BY and Class variables (if any) are in the output.
... View more