DATA Step, Macro, Functions and more

PROC SQL Select Distinct Count

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

PROC SQL Select Distinct Count

Hi Everybody! I am fairly new to SAS and still have trouble with some things. 

 

I have a dataset and want to get the distinct count of members based on two variables. Variable 1 (SDA) has 13 values and Variable 2 (FFYEAR) HAS 7 values. Instead of writing out the code 91 different times (13 X 7 = 91) for all the different combination of variables, is there a way I can write the code once but refer to all the different values in the variable? 

 

I am using SAS 9.2 and here is the basic structure of my current code:

 

proc sql;
select count (distinct member_id) as count
from ELIGIBLE
where sda='A' AND ffyear=2009;
quit; 

 

I want to avoid having to write the code over and over for each combination of variable values. 


Accepted Solutions
Solution
‎04-15-2016 12:03 PM
Respected Advisor
Posts: 4,926

Re: PROC SQL Select Distinct Count

Posted in reply to TXSASneophyte

Use group by

 

proc sql;
select sda, ffyear, count (distinct member_id) as count
from ELIGIBLE
group by sda, ffyear;
quit; 
PG

View solution in original post


All Replies
Super User
Posts: 11,343

Re: PROC SQL Select Distinct Count

Posted in reply to TXSASneophyte

I think you need to provide some example data and what you expect the output to look like.

You may be looking for GROUP BY if you really need to do sql;

Or possibly:

proc summary data=eligible nway;
   class SDA FFYEAR Member_id;
   output out=want (drop=_type_ rename=(_freq_=Count));
run;
Solution
‎04-15-2016 12:03 PM
Respected Advisor
Posts: 4,926

Re: PROC SQL Select Distinct Count

Posted in reply to TXSASneophyte

Use group by

 

proc sql;
select sda, ffyear, count (distinct member_id) as count
from ELIGIBLE
group by sda, ffyear;
quit; 
PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 2515 views
  • 0 likes
  • 3 in conversation