BookmarkSubscribeRSS Feed
inason
Calcite | Level 5

In the data below I would like proc sql to select the minimum date for subject 123 as the missing date.

 

data visit;

input subject $1-3  dtc $4-24 ;

cards;

123  2014-01-15T00:00

123 

123  2014-01-17T00:00:00

124  2014-01-15T00:00:00

124  2014-01-15T00:00:00

124  2014-01-17T00:00:00

;

run;

 

proc sql;
create table want. as
select distinct subject, min(dtc) as mindt format = date9.
from have
where subject ne ''
group by subject;
quit;

3 REPLIES 3
Patrick
Opal | Level 21

Read your source data in a way so the datetime string gets converted to a SAS datetime value.

The SAS min() function excludes missings.

Below one possible approach.

data have;
  infile datalines truncover;
  input subject $1-3 @4 dtc :e8601dt19.;
  format dtc datetime20.;
  cards;
123  2014-01-15T00:00
123 
123  2014-01-17T00:00:00
124  2014-01-15T00:00:00
124  2014-01-15T00:00:00
124  2014-01-17T00:00:00
;

proc sort data=have out=inter;
  where subject ne ' ';
  by subject dtc;
run;

data want;
  set inter;
  by subject;
  if first.subject;
run;

 

Ksharp
Super User
data have;
  infile datalines truncover;
  input subject $1-3 @4 dtc :e8601dt19.;
  format dtc datetime20.;
  cards;
123  2014-01-15T00:00
123 
123  2014-01-17T00:00:00
124  2014-01-15T00:00:00
124  2014-01-15T00:00:00
124  2014-01-17T00:00:00
;

proc sql;
create table want as
select distinct subject, dtc as mindt format = dtdate9.
from have
group by subject
having coalesce(dtc,-9999999)=min(coalesce(dtc,-9999999))
;
quit;
FreelanceReinh
Jade | Level 19

Hello @inason,

 

If you want MINDT to be a SAS date value (as suggested by your format = date9. specification), you can use a CASE expression:

proc sql;
create table want as
select subject, case when nmiss(dtc) then . else min(input(dtc,yymmdd10.)) end as mindt format=date9.
from visit
where subject ne ' '
group by subject;
quit;

Edit: Or, a bit shorter, replace the above CASE expression by an IFN function call:

ifn(nmiss(dtc),.,min(input(dtc,yymmdd10.)))

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2573 views
  • 1 like
  • 4 in conversation