DATA Step, Macro, Functions and more

Distinct variable

Reply
Contributor
Posts: 25

Distinct variable

[ Edited ]

Hi there,

 

I have a data set which contains several ID's such as this:

 

IDSubject
1High
1Med
1Med
2Low
2Low
3High
3Low
3Med
3Med

 

I would like to count all distinct values within the subject variable and arrive at this:

 

IDDistinct
12
21
33

 

Is there a data step solution I can use to achieve this?

 

Cheers,

Pete

PROC Star
Posts: 8,142

Re: Distinct variable

Posted in reply to PetePatel

Depends! If your data are already grouped as in your example, then the following would work:

data have;
  input ID  Subject $;
  cards;
1   High
1   Med
1   Med
2   Low
2   Low
3   High
3   Low
3   Med
3   Med
;

data want (drop=Subject);
  set have;
  by ID Subject notsorted;
  if first.id then distinct=0;
  if first.Subject then distinct+1;
  if last.id then output;
run;

Art, CEO, AnalystFinder.com

 

PROC Star
Posts: 1,547

Re: Distinct variable

Posted in reply to PetePatel
data have;
  input ID  Subject $;
  cards;
1   High
1   Med
1   Med
2   Low
2   Low
3   High
3   Low
3   Med
3   Med
;

proc sql;
create table want as
select id, count(distinct subject) as distinct
from have
group by id;
quit;
Ask a Question
Discussion stats
  • 2 replies
  • 93 views
  • 0 likes
  • 3 in conversation