Yesterday and the day before, I gave an example of using SAS to sort student responses into the class they were in using the DATEPART function, TIMEPART function and a few DO-loops. After making sure my code runs, I decided it was pretty redundant and thus a classic case for a macro.

What I want to do is follow this logic, based on the class schedule in the computer lab

If it is day X, and between times A and B, then it is teacher number one’s class.

Else, if it is day X and between times A and B, then it teacher number two’s class.

The problem is  a bit complicated by the fact that it is not the same number of classes each day. Sometime it’s one, sometimes two and sometimes three. It is not always the same order, that is Mary’s class does not always follow Samir’s class. Also, the class lengths are not the same. The middle school classes are longer than elementary school.  Here is the code, to be explained below

options mprint ;
%macro sched(the_day,start1,finish1,teacher1,start2=0, finish2=0, teacher2=” “, start3=0,finish3=0,  teacher3=” “);
if date_data = &the_day then do ;
if minutes > &start1 & minutes < &finish1 then tclass = &teacher1 ;
else if (&start2 > 0) & (minutes > &start2) & minutes < &finish2  then tclass = &teacher2 ;
else if &start3 > 0  & (minutes > &start3) & minutes < &finish3 then tclass = &teacher3 ;
end ;
%mend sched ;

Data coded ;
attrib tclass length = $19 ;
set in.studentdata ;
set SASUSER.QUERY_FOR_INPUTFORMDATA ;
%sched(19290,800,850,”Flintstone”,start2= 850, finish2=900, teacher2= “Rubble”);
%sched(19292,790,840,”Elmo”,start2= 840, finish2=900, teacher2= “Bert”, start3=940,finish3=990, teacher3= “Snuffleupagus”);

 

 

options mprint ;

— Prints the code generated by the macro to your log. Invaluable for de-bugging. Delete this statement after your macro is churning away.
%macro sched(the_day,start1,finish1,teacher1,start2=0, finish2=0, teacher2=” “, start3=0,finish3=0,  teacher3=” “);

Names the macro, gives it four required parameters – the date, start of FIRST  class, time class finishes and teacher name

gives it six optional parameters, the start of the second class, finish of second class, teacher of second class, start of third class, etc.

*** mega-important here, I am also assigning default values for each of these so later in the program it doesn’t run into a null value and throw an error.
if date_data = &the_day then do ;
if minutes > &start1  &  minutes < &finish1 then tclass = &teacher1 ;

If the record matches the value for day, and the start and finish times match the values given, the record is assigned to teacher1 ‘ s class.

else if (&start2 > 0) & (minutes > &start2) & minutes < &finish2  then tclass = &teacher2 ;
            else if &start3 > 0  & (minutes > &start3) & minutes < &finish3 then tclass = &teacher3 ;

For the next two possibilities, IF there is a value supplied for start2 (or start3), then the statement executes and looks for students who were logged on and answering questions during that time, and assigns their records to the appropriate teacher’s class.
    end ;
%mend sched ;

Here we end the DO-loop and then end the macro.

Data coded ;
attrib tclass length = $19 ;
set in.studentdata ;
set SASUSER.QUERY_FOR_INPUTFORMDATA ;
%sched(19290,800,850,”Flintstone”,start2= 850, finish2=900, teacher2= “Rubble”);
%sched(19292,790,840,”Elmo”,start2= 840, finish2=900, teacher2= “Bert”, start3=940,finish3=990, teacher3= “Snuffleupagus”);

The section above creates a data set, assigns an appropriate length for teacher name and then shows two examples, calling the macro for a day with two class and another day when there were three classes. One immediate change I can see would be useful might be to change the input parameter from a SAS date value to something more comprehensible to humans. Maybe I’ll do that next week.

Tomorrow I have a slight problem with javascript animation to work out.

 

Comments

Leave a Reply