Hi, I need to find the lowest value in a date column, but i'm not entirely sure how to go about it.
Sample data attached.
I'm new to this, but I tried using a min function, that didn't work.
Tried creating a case to cover it, but that didn't either, any help is appreciated.
If you want to select the row with the lowest datetime value within a group then code as below should do the job.
data have;
do record='Record1', 'Record2';
do othervar=0 to 100000 by 10000;
dttm=datetime()-othervar;
output;
end;
end;
stop;
format dttm datetime20.;
run;
proc sql;
/* create table want as*/
select
record,
othervar,
min(dttm) as min_dttm format=datetime20.
from have
group by record
having min(dttm)=dttm
;
quit;
The SQL above is something you can "click together" using the EG Query Builder.
Once you create the calculated column "min_dttm" you will get additional options to chose from.
The GROUP BY in above SQL gets created based on what you define in pane "Summary Groups"
The HAVING clause gets created based on what you define under tab "Filter Data"/"Filter the summarized data"
Correction:
The having clause was: having max(dttm)=dttm
The test must use min() of course: having min(dttm)=dttm
Fixed now.
You can use PROC MEANS or PROC SUMMARY and have it compute the MIN of the column of interest.
The MIN function (which you tried) finds the minimum across a row.
proc sql;
select min(date) from have;
quit;
Replace dataset and column name as needed.
I'm trying to make a computed column with the lowest date so then I can run a select distinct query excluding the original column and result in one date per record.
If you want to select the row with the lowest datetime value within a group then code as below should do the job.
data have;
do record='Record1', 'Record2';
do othervar=0 to 100000 by 10000;
dttm=datetime()-othervar;
output;
end;
end;
stop;
format dttm datetime20.;
run;
proc sql;
/* create table want as*/
select
record,
othervar,
min(dttm) as min_dttm format=datetime20.
from have
group by record
having min(dttm)=dttm
;
quit;
The SQL above is something you can "click together" using the EG Query Builder.
Once you create the calculated column "min_dttm" you will get additional options to chose from.
The GROUP BY in above SQL gets created based on what you define in pane "Summary Groups"
The HAVING clause gets created based on what you define under tab "Filter Data"/"Filter the summarized data"
Correction:
The having clause was: having max(dttm)=dttm
The test must use min() of course: having min(dttm)=dttm
Fixed now.
@Schmageggy wrote:
Hi, I need to find the lowest value in a date column, but i'm not entirely sure how to go about it.
Sample data attached.
I'm new to this, but I tried using a min function, that didn't work.
Tried creating a case to cover it, but that didn't either, any help is appreciated.
Didn't work is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
XLSX are not SAS data sets. Any choice I make to turn such into a SAS data set may result in differences if variable type, values (especially with dates, times or datetime values), and variable lengths from your SAS data.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.