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

Hi,

 

I am trying to calculate cumulative total using proc sql. I know how to do in a data step but i am trying to understand sas better and wanted to try using proc sql

 

data is this. I want a cumulative column for the monthly_pmt variable by ID. I am just testing this code for one ID right now

IDENT_SEQ_NOACTV_DATEMonthly_pmtmonthcount
75996642798852225007JUL2019:00:00:0015071
75996642798852225102AUG2019:00:00:0015082
75996642798852225206SEP2019:00:00:0015093
75996642798852225305OCT2019:00:00:00100104
75996642798852225408NOV2019:00:00:00400115
75996642798852225621DEC2019:00:00:00200126

 

I wrote this but i am getting duplication

 


PROC SQL;
CREATE TABLE CUMULATIVE AS
SELECT a.*, SUM(b.MONTHLY_PMT) AS CUML
FROM TEST3 A INNER JOIN TEST3 B
ON A.ID= B.ID and b.count <= a.count
GROUP BY A.ID,a.ent_seq_no,a.actv_date
ORDER BY A.ID,a.ent_seq_no,a.actv_date
;
QUIT;

 

result is:

 

IDENT_SEQ_NOACTV_DATEMonthly_pmtmonthcountCUML
75996642798852225007JUL2019:00:00:0015071150
75996642798852225102AUG2019:00:00:0015082300
75996642798852225102AUG2019:00:00:0015082300
75996642798852225206SEP2019:00:00:0015093450
75996642798852225206SEP2019:00:00:0015093450
75996642798852225206SEP2019:00:00:0015093450
75996642798852225305OCT2019:00:00:00100104550
75996642798852225305OCT2019:00:00:00100104550
75996642798852225305OCT2019:00:00:00100104550
75996642798852225305OCT2019:00:00:00100104550
75996642798852225408NOV2019:00:00:00400115950
75996642798852225408NOV2019:00:00:00400115950
75996642798852225408NOV2019:00:00:00400115950
75996642798852225408NOV2019:00:00:00400115950
75996642798852225408NOV2019:00:00:00400115950
75996642798852225621DEC2019:00:00:002001261150
75996642798852225621DEC2019:00:00:002001261150
75996642798852225621DEC2019:00:00:002001261150
75996642798852225621DEC2019:00:00:002001261150
75996642798852225621DEC2019:00:00:002001261150
75996642798852225621DEC2019:00:00:002001261150

 

 

I got around it by modifying to ask for distinct a.* in the above query but is this the only way? why is it doing this?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @TheNovice  It's simple. Your GROUP BY is not complete.

 

You could include A.* in group by to not allow PROC SQL to REMERGE

So, 

GROUP BY A.ID,a.ent_seq_no,a.actv_date,a.monthly_pmt,a.month,a.count

should solve the problem

 

Full Version:

 

PROC SQL;
CREATE TABLE CUMULATIVE AS
SELECT a.*, SUM(b.MONTHLY_PMT) AS CUML
FROM have A INNER JOIN have B
ON A.ID= B.ID and b.count <= a.count
GROUP BY A.ID,a.ent_seq_no,a.actv_date,a.monthly_pmt,a.month,a.count
ORDER BY A.ID,a.ent_seq_no,a.actv_date
;
QUIT;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

HI @TheNovice  It's simple. Your GROUP BY is not complete.

 

You could include A.* in group by to not allow PROC SQL to REMERGE

So, 

GROUP BY A.ID,a.ent_seq_no,a.actv_date,a.monthly_pmt,a.month,a.count

should solve the problem

 

Full Version:

 

PROC SQL;
CREATE TABLE CUMULATIVE AS
SELECT a.*, SUM(b.MONTHLY_PMT) AS CUML
FROM have A INNER JOIN have B
ON A.ID= B.ID and b.count <= a.count
GROUP BY A.ID,a.ent_seq_no,a.actv_date,a.monthly_pmt,a.month,a.count
ORDER BY A.ID,a.ent_seq_no,a.actv_date
;
QUIT;
TheNovice
Quartz | Level 8

yes, that makes so much sense.... thank you so much. it worked!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2131 views
  • 0 likes
  • 2 in conversation