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:
Data Enr2;
Set Enr;
BY STUKEY SCHOOL;
IF FIRST.STUKEY AND FIRST.SCHOOL; RUN;
If you are trying to select the first records for each school under each stukey, then try:
Data Enr2;
Set Enr;
BY STUKEY SCHOOL;
IF FIRST.SCHOOL;
RUN;
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.
data enr;
input stukey $ school $;
datalines;
1 a
1 a
1 b
1 b
1 b
2 a
2 b
2 c
3 b
4 d
run;
proc sort data=enr nodupkey;
by stukey school ;
run;
Data dups each;
Set Enr;
BY stukey school ;
IF not(FIRST.STUKEY and last.stukey) then output dups;
if first.school then output each;
RUN;
title1 students in more than one school;
proc print data=dups;
run;
title1 Student school combinations;
proc print data=each;
run;
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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.