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

Hi There,

 

I'm new to SAS, using SAS Enterprise Guide 7.12 and running this code. I can't seem to get a distinct list based on the dttm_create. I tried using group by and having, sub-query and all other different variations but nothing seem to work. The SQL runs fine (no sql error) but just not returning the correct results.

 

Can someone please help? I've been at this for 2 days now Smiley Sad Thank you so much in advance!

 

PROC SQL;
CREATE TABLE WORK.TABLE2 AS
SELECT DISTINCT ID1, ID2, CYCLE_DATE, AMT, STATUS,
MAX(DTTM_CREATE) FORMAT=DATETIME25.6 AS MAX_of_DTTM_CREATE
FROM WORK.TABLE1
WHERE ID1 = '123456789'
GROUP BY ID1,
ID2, CYCLE_DATE,
AMT, STATUS, DTTM_CREATE;
QUIT;

 

This is the result I'm getting but the SQL should only give me the first row.

ID1ID2CYCLE_DATEAMOUNTSTATUSMAX_of_DTTM_CREATE
12345678998765432131-Dec-191.67Active03JAN2020:03:04:35.353335
12345678998765432131-Dec-192.01Active06DEC2019:02:48:35.810525
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Mary001 wrote:

 

@PaigeMiller I need the latest and greatest dttm_create per record per cycle date.

 


Try this:

 

PROC SQL;
CREATE TABLE WORK.TABLE2 AS
SELECT ID1, ID2, CYCLE_DATE, AMT, STATUS,
DTTM_CREATE FORMAT=DATETIME25.6
FROM WORK.TABLE1
WHERE ID1 = '123456789'
GROUP BY ID1,ID2, CYCLE_DATE
having dttm_create=max(dttm_create);
QUIT;
--
Paige Miller

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

Why are you including all of those extra variables in the GROUP BY?  Which variables do you actually want to use to define the group that you want to use to find max of  DTTM_CREATE in the group ? 

 

Here is simpler query you might try to see the difference that changing the GROUP BY makes.

This is like your query:

select sex,max(age) as max_age
from sashelp.class
group by sex, age
;

This is more appropriate:

select sex,max(age) as max_age
from sashelp.class
group by sex
;
PaigeMiller
Diamond | Level 26

Distinct works only on the entire collection of variables in the CREATE TABLE. It doesn't work if you want one or two variables to be distinct and not the other.

 

What is the criteria that lets you determine that you want the first record here and not the other records?

--
Paige Miller
novinosrin
Tourmaline | Level 20

HI @Mary001  First off

 

1. Are you sure DTTM_CREATE should be part of GROUP BY Clause

2. Are you sure AMT should be part of GROUP BY clause

 

If the above questions make sense, it appears the SQL you are prolly after is

 

 

PROC SQL;
CREATE TABLE WORK.TABLE2 AS
SELECT DISTINCT ID1, ID2, CYCLE_DATE, AMT, STATUS,
MAX(DTTM_CREATE) FORMAT=DATETIME25.6 AS MAX_of_DTTM_CREATE
FROM WORK.TABLE1
WHERE ID1 = '123456789'
GROUP BY ID1,ID2, CYCLE_DATE, STATUS
having MAX_of_DTTM_CREATE=DTTM_CREATE;
QUIT;

 

/*Or with the corrected GROUP BY clauses knowing there isn't any column that is specified in the SELECT Clause not part of the Group by that may cause potential remerging*/

PROC SQL;
CREATE TABLE WORK.TABLE2 AS
SELECT DISTINCT ID1, ID2, CYCLE_DATE, AMT, STATUS,
MAX(DTTM_CREATE) FORMAT=DATETIME25.6 AS MAX_of_DTTM_CREATE
FROM WORK.TABLE1
WHERE ID1 = '123456789'
GROUP BY ID1,ID2, CYCLE_DATE, STATUS;
QUIT;

The above assumes there are no ties in MAX of DTTM_CREATE and secondly status is either ACTIVE or NOT forming a group. Please let us know. Thank you

 

Mary001
Fluorite | Level 6

Hello Again and thank you so much for the quick responses!

 

@Tom I updated the query twice and re-run (1st with Group by ID1 and 2nd without Group by) It ran successfully but still not the correct results ... also the log says, "NOTE: The query requires remerging summary statistics back with the original data." What does this mean, please?

 

Here is the result but again it should only return with the 1st row:

ID1ID2CYCLE_DATEAMOUNTSTATUSMAX_of_DTTM_CREATE
12345678998765432131-Dec-191.67Active03JAN2020:03:04:35.353335
12345678998765432131-Dec-192.01Active03JAN2020:03:04:35.353335

 

@PaigeMiller I need the latest and greatest dttm_create per record per cycle date.

 

@novinosrin My apologies on the group by clause, I'm not really sure what I'm doing 😞

PaigeMiller
Diamond | Level 26

@Mary001 wrote:

 

@PaigeMiller I need the latest and greatest dttm_create per record per cycle date.

 


Try this:

 

PROC SQL;
CREATE TABLE WORK.TABLE2 AS
SELECT ID1, ID2, CYCLE_DATE, AMT, STATUS,
DTTM_CREATE FORMAT=DATETIME25.6
FROM WORK.TABLE1
WHERE ID1 = '123456789'
GROUP BY ID1,ID2, CYCLE_DATE
having dttm_create=max(dttm_create);
QUIT;
--
Paige Miller
Mary001
Fluorite | Level 6

THANK YOU so much everyone!!! You guys are amazing!

 

I re-ran with the updated sql from Paige and received the correct results 🙂

 

Thanks again!

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
  • 6 replies
  • 2101 views
  • 0 likes
  • 4 in conversation