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

I have some data

Year

Student IDClass
20001Math
20002Math
20003English
20004English
20011Math
20012Chemistry
20013English
20011Chemistry

And want to create a table for the number of distinct students and distinct classes. i.e.

Year

Student IDClass
200042
200133

 

However, my code:

 

proc sql;
	create table a.registry as select distinct year as year, count(distinct i.student) as student, count(distinct i.class) as class
	from a.list i;
quit;

gives me the same value each year, i.e.

Year

Student IDClass
200043
200143

Does anyone know how to fix this?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20


data have;
input Year Student_ID	Class :$15.;
cards;
2000	1	Math
2000	2	Math
2000	3	English
2000	4	English
2001	1	Math
2001	2	Chemistry
2001	3	English
2001	1	Chemistry
;

proc sql;
 create table want as
 select year, count(distinct Student_ID) as Student_ID,
 count(distinct class) as class
 from have
 group by year;
quit;
Year Student_ID class
2000 4 2
2001 3 3

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20


data have;
input Year Student_ID	Class :$15.;
cards;
2000	1	Math
2000	2	Math
2000	3	English
2000	4	English
2001	1	Math
2001	2	Chemistry
2001	3	English
2001	1	Chemistry
;

proc sql;
 create table want as
 select year, count(distinct Student_ID) as Student_ID,
 count(distinct class) as class
 from have
 group by year;
quit;
Year Student_ID class
2000 4 2
2001 3 3
ReiYama
Calcite | Level 5
Thank you! This worked perfectly!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 615 views
  • 2 likes
  • 2 in conversation