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.
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.
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.