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

I am trying to aggregate data for the quarter from months.

Data is quite simple has Type, Code and then values (revenue, quantity, label etc.).

Occasionally some lines  in month 1 match exactly lines in month 2 or month 3 of the quarter. I just discovered that in that case data from one of the month is dropped ( i.e. duplicates are eliminated). I need to keep all data! Can this be done with proc sql?

 

Current program in the nutshell:

proc sql;

create table Q as

select type, code, sum (revenue) as Revenue, sum(quantity) as Quantity, label

from 

(select * from month1  union

 select * from month2 union

select  * from month3)

where code is not null

group by type, code, label,

order by type , code;

quit;

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You have to use UNION ALL to keep duplicates; unless you want to explicitly remove duplicates, it is always recommended to use UNION ALL, because it needs one less sort.

 

Your issue can also be solved with these two steps:

data temp / view=temp;
set
  month1
  month2
  month3
;
keep type code revenue quantity label;
where code ne " "; /* use . if code is numeric */
run;

proc summary data=temp nway;
class type code label;
var revenue quantity;
output
  out=want (drop=_type_ _freq_)
  sum()=
;
run;

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

You have to use UNION ALL to keep duplicates; unless you want to explicitly remove duplicates, it is always recommended to use UNION ALL, because it needs one less sort.

 

Your issue can also be solved with these two steps:

data temp / view=temp;
set
  month1
  month2
  month3
;
keep type code revenue quantity label;
where code ne " "; /* use . if code is numeric */
run;

proc summary data=temp nway;
class type code label;
var revenue quantity;
output
  out=want (drop=_type_ _freq_)
  sum()=
;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 477 views
  • 0 likes
  • 2 in conversation