Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active October 10, 2018 16:55
Show Gist options
  • Select an option

  • Save statgeek/25c614fafe1316a2da1fab830036bb5c to your computer and use it in GitHub Desktop.

Select an option

Save statgeek/25c614fafe1316a2da1fab830036bb5c to your computer and use it in GitHub Desktop.
SAS - get a maximum and corresponding ID variable for the maximum value
/* Find a maximum value and the date that maximum value occurred
Using PROC SUMMARY to get the ID variables of statistics using MAXID and MINID statistics.
Tags: MinID, MaxID, PROC SUMMARY
*/
*create sample data;
data have;
input station $ datetime anydtdtm. calculatedpower ;
format datetime datetime.;
cards;
ABBA 28AUG2018:0:0:0 100
ABBA 31AUG2018:12:0:0 88
ABBA 01SEP2018:1:0:0 1
ZZZZ 07SEP2018:0:0:0 900
ZZZZ 09SEP2018:0:0:0 1000
ZZZZ 21SEP2018:0:0:0 3000
;;;;
run;
*calculate max/min by group and include the data the max and minimum occurred;
proc summary data=have nway;
class station;
id datetime; *specify the ID variable to be used;
var calculatedPower;
*MINID and MaxID capture the ID variable for the Min/Max values;
output out=summary min=Min_power max=max_power minid=min_date maxid=max_Date;
run;
*add data back to original table;
data final;
merge have summary;
by station;
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment