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

How would I select distinct persons per year, but only count each person once total.

An example of my data is:

ID Date
1 20NOV2018
2 06JUN2017
2 29JUL2011
3 05MAY2014
4 04APR2002
4 25APR2009

 

I want my output to look like:

Year Cnt
2002 1
2009 0
2011 1
2014 1
2017 0
2018 1

1 ACCEPTED SOLUTION

Accepted Solutions
JeffMeyers
Barite | Level 11

I think you're only trying to count the person in their earliest date if I understand correctly.  I believe it would be something like this:

data blah;
format date date9.;
input ID 1. @3 Date date9.;
datalines;
1 20NOV2018
2 06JUN2017
2 29JUL2011
3 05MAY2014
4 04APR2002
4 25APR2009
;
run;

proc sql;
    select year(date) as year,count(distinct ifn(date=min_date,id,.)) as num
        from (select *,min(date) as min_date from blah group by id) group by year;
quit;

Which gives me this:

Capture.JPG

I use the IFN function (IFC if ID is a character variable) to limit the IDs to the first occuring date so the patient isn't double counted (COUNT function doesn't add a missing value).  I use an inline view to calculate the minimum date for each patient since you can't nest the MIN function and the COUNT function in the same query.

View solution in original post

1 REPLY 1
JeffMeyers
Barite | Level 11

I think you're only trying to count the person in their earliest date if I understand correctly.  I believe it would be something like this:

data blah;
format date date9.;
input ID 1. @3 Date date9.;
datalines;
1 20NOV2018
2 06JUN2017
2 29JUL2011
3 05MAY2014
4 04APR2002
4 25APR2009
;
run;

proc sql;
    select year(date) as year,count(distinct ifn(date=min_date,id,.)) as num
        from (select *,min(date) as min_date from blah group by id) group by year;
quit;

Which gives me this:

Capture.JPG

I use the IFN function (IFC if ID is a character variable) to limit the IDs to the first occuring date so the patient isn't double counted (COUNT function doesn't add a missing value).  I use an inline view to calculate the minimum date for each patient since you can't nest the MIN function and the COUNT function in the same query.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 1 reply
  • 593 views
  • 1 like
  • 2 in conversation