BookmarkSubscribeRSS Feed
SannaSanna
Quartz | Level 8


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;

3 REPLIES 3
art297
Opal | Level 21

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;

ArtC
Rhodochrosite | Level 12

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;

Reeza
Super User

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.


sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 748 views
  • 8 likes
  • 4 in conversation