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

I am trying to create a data set using SQL  in which the count is grouped by another variable. It happens that no record meet the criteria, so I expect it to bring back zero by the group variable. For some reasons, I keep on getting zero rows.

 

For example, suppose my data is like this:

data have;
input id a b trtpn;
datalines ;
1 2 4 1
2 3 5 1
3 4 0 2
4 5 1 2
4 2 1 3
3 3 4 3
3 5 7 3
;
run;

proc sql noprint;
create table want as
select trtpn,count(distinct id) as count
from have
where a=10
group by trtpn;

quit;

 

If it's correct ,the output should look like the following:


trtpn count
1     0
2     0

3     0

 Any help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Get what you want with:

 

proc sql;
create table want as
select 
    trtpn,
    count(distinct case when a=10 then id else . end) as count
from have
group by trtpn;
select * from want;
quit;
PG

View solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

Pay attention:

     ... from have where a=10 ...

results into zero observations.

PGStats
Opal | Level 21

Get what you want with:

 

proc sql;
create table want as
select 
    trtpn,
    count(distinct case when a=10 then id else . end) as count
from have
group by trtpn;
select * from want;
quit;
PG
SWEETSAS
Obsidian | Level 7

Many thanks! It worked.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3812 views
  • 1 like
  • 3 in conversation