velvetrock
velvetrock

Reputation: 585

SAS how to get the first several rows by criteria

In SAS, I want to obtain the first several rows by two variables class and student.

Here is my data:

class   student          course           note
 1       A                 001             10
 1       A                 002             14
 1       B                 001             12
 1       B                 002             8
 2       C                 001             6
 2       C                 002             17
 2       D                 003             9
 3       E                 003             13

I want to get the data of the first student of each class, that means the data of student A, C, E:

class   student          course           note
 1       A                 001             10
 1       A                 002             14
 2       C                 001             6
 2       C                 002             17
 3       E                 003             13

I have tried

if first.class then flag_first=1; else flag_first=0;

but this gets only the first row of each class.

Hope to get your answers.

Upvotes: 0

Views: 126

Answers (1)

Reeza
Reeza

Reputation: 21294

Using first logic is correct.

Data want;
Set have;
By class student;

 Retain flag;

 If first.class then flag =1;
 Else if first.student then flag=0;

  If flag=1 then output;
 Run;

Upvotes: 2

Related Questions