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

I'd like to automate my SAS code to calculate what percentile (Q1,Q2,Q3,Q4) my teachers spent in the classroom by school year.

 

My dataset looks like this:

 

teacher  school_year  hours_spent_in_classroom

A             2012                    350

A             2013                    150

B             2014                    250

C             2014                    350

ALL_TEACHERS 2012        600

 

 

I can use proc univariate to know what percentiles are for each year.  I could hard code those percentile values into my dataset, and then do "if hours_spent_in_classroom GE X then quartile = 1". But this is a lot of manual work, and it would change by school_year.

 

I'd like to have a final dataset that looks like this:

 

teacher  school_year  hours_spent_in_classroom   Quartile

A             2012                    350                                 1

A             2013                    150                                 2

B             2014                    250                                 3

C             2014                    350                                 4

ALL_TEACHERS 2012        600                                 4

 

What's the best way to go about this?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do you actually have individual records for each teacher each year or is that "Total all teachers" actually the value you have in your data?

And are the 350 hours of teacher A included in the 600? Are you attempting to get a common value for the quartiles across years or use a different one per year?

 

If you have individual records then Proc Rank may do this.You didn't provide enough records to do much with quartiles so I made a few up.

 

data have;
   input teacher $ school_year  hours_spent_in_classroom;
datalines;
A   2012  350
B   2012  100
C   2012  150
D   2012   50
A   2013  150
B   2013  300
C   2013  100
D   2013  175
A   2014   50
B   2014  250
C   2014  350
D   2014  200
;
run;

proc sort data=have;
   by school_year teacher;
run;
                                                           
proc rank data=have out=want groups=4 descending;
   by school_year;
   var hours_spent_in_classroom;
   ranks HourRanking;
run;

However if you do not individual hours for all of the teachers then this won't work.

 

Note that PROC rank has the first rank as 0. If you really want 1 then pass the want data set through a data step to add one to the ranks.

View solution in original post

2 REPLIES 2
ballardw
Super User

Do you actually have individual records for each teacher each year or is that "Total all teachers" actually the value you have in your data?

And are the 350 hours of teacher A included in the 600? Are you attempting to get a common value for the quartiles across years or use a different one per year?

 

If you have individual records then Proc Rank may do this.You didn't provide enough records to do much with quartiles so I made a few up.

 

data have;
   input teacher $ school_year  hours_spent_in_classroom;
datalines;
A   2012  350
B   2012  100
C   2012  150
D   2012   50
A   2013  150
B   2013  300
C   2013  100
D   2013  175
A   2014   50
B   2014  250
C   2014  350
D   2014  200
;
run;

proc sort data=have;
   by school_year teacher;
run;
                                                           
proc rank data=have out=want groups=4 descending;
   by school_year;
   var hours_spent_in_classroom;
   ranks HourRanking;
run;

However if you do not individual hours for all of the teachers then this won't work.

 

Note that PROC rank has the first rank as 0. If you really want 1 then pass the want data set through a data step to add one to the ranks.

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
  • 693 views
  • 5 likes
  • 3 in conversation