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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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