Help using Base SAS procedures

If first statements

Reply
Contributor
Posts: 61

If first statements


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;

PROC Star
Posts: 7,490

Re: If first statements

Posted in reply to Suzanne_Ed

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;

Valued Guide
Posts: 634

Re: If first statements

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;

Super User
Posts: 19,860

Re: If first statements

Posted in reply to Suzanne_Ed

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.


Ask a Question
Discussion stats
  • 3 replies
  • 205 views
  • 8 likes
  • 4 in conversation