1

I have the following data set. I am writing two separate SQL steps to count the subjects with different conditions. Is there way to combine both the queries into one using SQL sub-queries. Any kind of input on this is appreciated.

data have; 
input sub       TEAE $ trt $; 
cards; 
101 N A 
101 Y A 
101 Y A 
101 N A 
102 Y B 
103 N B 
104 N A 
104 Y A 
104 Y A 
104 N A 
;; 

proc sql; 
 create table totevents as select trt,count (sub) as events 
 from have 
 where teae eq 'Y' 
 group by trt; 
quit; 

proc sql; 
  create table totsubs as select trt,count (distinct sub) as subs 
  from have 
  group by trt; 
quit;
flag

1 Answer

2

You can use a case statement inside the count function...

proc sql; 
  create table both as select trt,count (distinct sub) as subs 
  ,count (case when teae eq 'Y' then sub end) as events
  from have 
  group by trt; 
quit;
link|flag

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.