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

 

proc sql;
create table public_school as
select ID,time_period,score,class from data 
where ID = (select ID from data
		where time_period = (select max(time_period )from data))
group by class  
order by time_period 
;quit;
IDtime_periodsectionpercentage
2810.25a_section 83
2810.5a_section 82
2810.75a_section 98
2811a_section 88
2811.25a_section 81
2820.25a_section 80
2820.5a_section 82
2820.75a_section 84
2821a_section 86
2821.06a_section 54
2821.75a_section 80
2822a_section 82
2822.25a_section 84
2822.5a_section 86
2822.75a_section 88
2823.0a_section 90
2823.25a_section92
3010.25b_section70
3010.5b_section76
3010.75b_section82
3011b_section88
3020.25b_section90
3020.75b_section100
3030.25b_section84

In Sample table shown above, I have different no. of students in each section(a,b,c,d) and school_type( public, private) and their final % obtained for each quarterly held exam. Objective is to select student ID with maximum number of time period from each section. Output table should should look like(including score also) :

 

IDtime_periodsection
2820.25a_section 
2820.5a_section 
2820.75a_section 
2821a_section 
2821.25a_section 
2821.5a_section 
2821.75a_section 
2822a_section 
2822.25a_section 
2822.5a_section 
2822.75a_section 
3010.25b_section
3010.5b_section
3010.75b_section
3011b_section
 :::

 

I have tried code displayed above , but getting error as:

A GROUP BY clause has been discarded because neither the SELECT clause nor the optional HAVING clause of the associated table-expression referenced a summary function

 

Also, if u can tell how to create macro on this code for school type, it will be quite helpful 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

The remerging message is intended and expected, it's just a feature of SAS/SQL. When I run this test, I get 16 rows into table Public_School:

 

data have;
input ID  time_period section $ percentage;
datalines;
281 0.25    a_section   83
281 0.5 a_section   82
281 0.75    a_section   98
281 1   a_section   88
281 1.25    a_section   81
282 0.25    a_section   80
282 0.5 a_section   82
282 0.75    a_section   84
282 1   a_section   86
282 1.06    a_section   54
282 1.75    a_section   80
282 2   a_section   82
282 2.25    a_section   84
282 2.5 a_section   86
282 2.75    a_section   88
282 3.0 a_section   90
282 3.25    a_section   92
301 0.25    b_section   70
301 0.5 b_section   76
301 0.75    b_section   82
301 1   b_section   88
302 0.25    b_section   90
302 0.75    b_section   100
303 0.25    b_section   84
;

proc sql;
create table public_school as
select a.*
from 
    have as a inner join
    (select section, id, count(time_period) as total_period 
     from have 
     group by section, id) as b on a.section=b.section and a.id=b.id
group by a.section
having total_period = max(total_period)
;
select * from public_school;
quit;

image.png

PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

To get all the data from the "student ID with maximum number of time period from each section" :

 

proc sql;
create table public_school as
select a.*
from 
	data as a inner join
	(select section, id, count(time_period) as total_period 
	 from data 
	 group by section, id) as b on a.section=b.section and a.id=b.id
group by a.section
having total_period = max(total_period)
;
quit;

(untested)

or maybe you'l want to replace count(time_period) with sum(time_period).

PG
2ratika7
Calcite | Level 5

I have got this note: The query requires remerging summary statistics back with the original data & dataset created with 0 rows. 

PGStats
Opal | Level 21

The remerging message is intended and expected, it's just a feature of SAS/SQL. When I run this test, I get 16 rows into table Public_School:

 

data have;
input ID  time_period section $ percentage;
datalines;
281 0.25    a_section   83
281 0.5 a_section   82
281 0.75    a_section   98
281 1   a_section   88
281 1.25    a_section   81
282 0.25    a_section   80
282 0.5 a_section   82
282 0.75    a_section   84
282 1   a_section   86
282 1.06    a_section   54
282 1.75    a_section   80
282 2   a_section   82
282 2.25    a_section   84
282 2.5 a_section   86
282 2.75    a_section   88
282 3.0 a_section   90
282 3.25    a_section   92
301 0.25    b_section   70
301 0.5 b_section   76
301 0.75    b_section   82
301 1   b_section   88
302 0.25    b_section   90
302 0.75    b_section   100
303 0.25    b_section   84
;

proc sql;
create table public_school as
select a.*
from 
    have as a inner join
    (select section, id, count(time_period) as total_period 
     from have 
     group by section, id) as b on a.section=b.section and a.id=b.id
group by a.section
having total_period = max(total_period)
;
select * from public_school;
quit;

image.png

PG
2ratika7
Calcite | Level 5

thankyou!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 784 views
  • 1 like
  • 2 in conversation