BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Gil_
Quartz | Level 8
I have a table i need to get max date.

Proc sql;
Create table test as
Select
Id.
Balance.
Max(Create ) as Date
Fr;m tedt1
,
Run;

It brings everything. The create is a datetime20 format. The output is converted to 1859756644
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

You'll need to add a GROUP BY or HAVING clause depending what you want.

 

You want the record with the latest date?

 

 select ID, Balance, Max(create) as max format=datetime20.

 GROUP BY ID, Balance

 HAVING create=MAX(create)

 

Or the max date for each ID/Balance combination?

 

 select ID, Balance, Max(Create) as max format=datetime20.

 GROUP BY ID, Balance

 

See this post for another example.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

Can you plz post a sample of what you HAVE(Data) and what you want(Data) as output?

ChrisHemedinger
Community Manager

You'll need to add a GROUP BY or HAVING clause depending what you want.

 

You want the record with the latest date?

 

 select ID, Balance, Max(create) as max format=datetime20.

 GROUP BY ID, Balance

 HAVING create=MAX(create)

 

Or the max date for each ID/Balance combination?

 

 select ID, Balance, Max(Create) as max format=datetime20.

 GROUP BY ID, Balance

 

See this post for another example.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
ballardw
Super User

If you want the date portion of a variable containing datetime then the function is DATEPART(variable). You likely want to provide a date format to go along with that.

 

Perhaps something similar to

Proc sql;
   Create table test as
   Select 
   Id,
   Max(datepart(Create) ) as Date format date9.
   From tedt1
   group by id
   ;
quit;

You code is showing a lot of syntax problems: period instead of comma between select items; a semicolon in the middle of From; an extra comma at the end; and use of RUN instead of QUIT.

 

 

but I don't know what value of balance you want.

ShiroAmada
Lapis Lazuli | Level 10

Try this

data have;
  do id=1 to 10;
    balance=id*.452;
	create_dt=mdy(id,1,2018);
	output;
	if id>=7 then do;
    balance=id*.458;
	create_dt=mdy(id,1,2019);
	    output;
	end;

  end;
  format create_dt date9.;
run;

/* This will yield the same result */
proc sql;
  create table want as
  select 
  id,
  balance,
  max(create_dt) as max_crdt format=date9.
  from have
  group by id;
quit;

/* I think this is what you want */
proc sql;
  create table want as
  select 
  id,
  balance,
  create_dt
  from have
  group by id
  having max(create_dt)=create_dt;
quit;
Gil_
Quartz | Level 8
Im still getting all of entries not the maxdate.
Proc sql ;
Create table table as
Select
Id,
Balance,
Created
From table1
Group by dnd,balance,created
Having created =max(created)
;
Run;
Quit;

The created data looks like this
Id. Balance. Created
101. 120. 10DEC2018:19:11:30
101. 500. 10DEC2018:18:11:56

I would want
Id. Balance. Created
101. 120. 10DEC2018:19:11:30
Gil_
Quartz | Level 8
Got it thanks

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 26377 views
  • 2 likes
  • 5 in conversation