BookmarkSubscribeRSS Feed
PetePatel
Quartz | Level 8

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

2 REPLIES 2
art297
Opal | Level 21

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

 

novinosrin
Tourmaline | Level 20
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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 589 views
  • 0 likes
  • 3 in conversation