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

data T;
input ID L CF CD EXP;
Datalines;
1 1 A1 AAA 50
1 2 A1 BBB 100
1 4 A2 CCC 200
2 1 A1 AAA 50
2 2 A1 BBB 100
2 4 A2 CCC 200
;

proc sql;
create table wanted as 
select cf, sum (exp) as A
from T
group by distinct cd;
quit;

Hi. In this dataset I want to find to create a table sum of EXP per CF but only per the distinct values of CD i.e. the output should be

A1 150

A2 200

 

Thank you. 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

Hi,

 

Do you mean something like this:

data T;
  input ID L CF :$ CD :$ EXP;
Datalines;
1 1 A1 AAA 50
1 2 A1 BBB 100
1 4 A2 CCC 200
2 1 A1 AAA 50
2 2 A1 BBB 100
2 4 A2 CCC 200
;

proc sql;
create table want as 
select cf, sum (exp) as A
from 
  (select distinct CF, CD, EXP from T)
group by cf;
quit;

?

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Amethyst | Level 16

Hi,

 

Do you mean something like this:

data T;
  input ID L CF :$ CD :$ EXP;
Datalines;
1 1 A1 AAA 50
1 2 A1 BBB 100
1 4 A2 CCC 200
2 1 A1 AAA 50
2 2 A1 BBB 100
2 4 A2 CCC 200
;

proc sql;
create table want as 
select cf, sum (exp) as A
from 
  (select distinct CF, CD, EXP from T)
group by cf;
quit;

?

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



cmemtsa
Quartz | Level 8
It needs a subquery!
Thanks for your help.