BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Schmageggy
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@Schmageggy 

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.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Reeza
Super User
What do you want to do with the minimum after you find it?

The approaches here may help, just replace mean with min.
https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic.sas
Schmageggy
Fluorite | Level 6

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. Capture.JPG

Patrick
Opal | Level 21

@Schmageggy 

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
Fluorite | Level 6
That did it, thank you. But yeah I am using EG as SQL while not completely foreign, I have not learned or been trained in my current role. Should probably work on that. Thank you again.
Reeza
Super User
SAS has two free e courses available to get you started.
ballardw
Super User

@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.

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 4061 views
  • 5 likes
  • 6 in conversation