11-07-2012 11:42 AM
I am trying to select the first observation from two different variables and my code does not seem to be working. Can anyone help me. The code does not return the correct records. There could be the same stukey in different schools and I am trying to capture the stukey in every school. Code is as follows:
BY STUKEY SCHOOL;
IF FIRST.STUKEY AND FIRST.SCHOOL; RUN;
11-07-2012 01:58 PM
I am not sure what you are try to find. The following will determine students that attend more than one school and also students in each school.
input stukey $ school $;
proc sort data=enr nodupkey;
by stukey school ;
Data dups each;
BY stukey school ;
IF not(FIRST.STUKEY and last.stukey) then output dups;
if first.school then output each;
title1 students in more than one school;
proc print data=dups;
title1 Student school combinations;
proc print data=each;
11-07-2012 12:18 PM
When you do a BY statement you have an indicator variable for each by variable that says first and last.
So by selecting first.stukeyand first.school your saying give me the records where its is the first stukey AND the first school, which is the same as saying first.stukey essentially.
If you say first.school, everytime school changes (and by default the stukey) that record will be output.