Quartz | Level 8

## Counting observations per ID

I am trying to get the number of diagnoses per ID and grab only those with 2 or less:

``````proc sql;
create table diagcount as
select
dxSeqNo,
count(icd) as diagct
from denomip
group by claimheaderid, dxSeqno;
run;

/*less than or equal to 2 dx codes per claimheaderid*/
data diagct;
set diagcount;
where diagct <=2;
run;``````

Am I on the right track?

2 REPLIES 2
SAS Employee

## Re: Counting observations per ID

Next time please include a data example as i have included.  Seem like the code is working fine. see below

also you could get rid of the data step procedure and include this line in your sql .

where calculated diagct < 2;  (hint- in-between the from and the group by)

``````data denomip ;
input claimheaderid  dxSeqNo icd ;
datalines ;
10 3 1
40 1 2
40 1 2
40 2 2
10 1 2
10 3 2
10 2 2
40 1 3
40 3 3
40 2 3
;
run;

proc sql;
create table diagcount as
select
dxSeqNo,
count(icd) as diagct
from denomip
group by claimheaderid, dxSeqno;
run;

/*less than or equal to 2 dx codes per claimheaderid*/
data diagct;
set diagcount;
where diagct <=2;
run;``````

Super User

## Re: Counting observations per ID

Your SQL is counting by ID AND Dxseqno. That does not match your description of counting the number of diagnoses.

Does the SQL give what you expect?

My guess is that you may not want the Dxseqno in that counting as you will get separate count for ID=1 and DXseqno=1, Id=1 and Dxseqno=2, Id=1 and Dxseqno=3. If there exactly 3 observations for Id=1 that look like that list and you expect a count to be 3 then you want to remove Dxseqno from the Group by  and the Select.

Provide some example data and expected result for that small sample.

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