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

Good Morning All,

  I have been trying to wrap my brain around this but I can't seem to come up with an acceptable solution.  I am thinking I might have to use a nested select, but I wanted to ask my question here first.  I am using PROC SQL to select data below.  What I want to happen is to select ID and SUM(Count), however I want to limit this data on value = 20.  The issue is that I want the entire ID to be calculated when this happens, not just limited to the value = 20 line.  Appreciate any guidance on this one.  

 

Thank you!

 

ID Value Count
1 20 1000
1 19 200
1 18 100
1 17 9992
2 20 10000
3 12 1000
4 13 100
5 12 200
5 13 200
5 20 10000
5 21 2000
6 19 200
7 22 300
7 11 400
7 10 2000
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

In Proc SQL:

 

data have;
input ID Value Count;
datalines;
1 20 1000
1 19 200
1 18 100
1 17 9992
2 20 10000
3 12 1000
4 13 100
5 12 200
5 13 200
5 20 10000
5 21 2000
6 19 200
7 22 300
7 11 400
7 10 2000
;

proc sql;
   create table want as
   select ID, sum(count) as count
   from (select * from have
         group by ID
         having sum(Value = 20) > 0)
   group by ID;
quit;

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

What does your desired result look like?

IgawaKei29
Quartz | Level 8

@PeterClemmensen  It would look like this:

 

ID Count
1 11292
2 10000
5 12400

 

 

PeterClemmensen
Tourmaline | Level 20

Ok. Is it a requirement to do so in PROC SQL?

IgawaKei29
Quartz | Level 8

@PeterClemmensen If at all possible yes, I am trying to do a SQL passthru.

PeterClemmensen
Tourmaline | Level 20

In Proc SQL:

 

data have;
input ID Value Count;
datalines;
1 20 1000
1 19 200
1 18 100
1 17 9992
2 20 10000
3 12 1000
4 13 100
5 12 200
5 13 200
5 20 10000
5 21 2000
6 19 200
7 22 300
7 11 400
7 10 2000
;

proc sql;
   create table want as
   select ID, sum(count) as count
   from (select * from have
         group by ID
         having sum(Value = 20) > 0)
   group by ID;
quit;
IgawaKei29
Quartz | Level 8
Thank you! I appreciate the help. I don't use having often, so I am glad to see it used here.
novinosrin
Tourmaline | Level 20

data have;
input ID Value Count;
datalines;
1 20 1000
1 19 200
1 18 100
1 17 9992
2 20 10000
3 12 1000
4 13 100
5 12 200
5 13 200
5 20 10000
5 21 2000
6 19 200
7 22 300
7 11 400
7 10 2000
;

proc sql;
 create table want as
 select id, sum(count) as count
 from have 
 where id in (select id from have where value=20)
 group by id;
quit;

 Basically if i understand you correctly, you want to sum the count for those ID's that have one or more occurance of value =20.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1785 views
  • 0 likes
  • 3 in conversation