BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sri1
Obsidian | Level 7

Hi all,

 

I have a data set with following records where I have to flag the first occurrence of maximum score.I have used the following code

 

proc sort data=ex;

by subj score;

run;

 

data ex_t;

set ex;

by subj score;

if last.subj then flag=1;

else flag=0;

run;

 

With the above code , the following records(highlighted) gets flagged

 

SUBJ

Score

Date

004

1

01FEB2018

004

1

09MAR2018

005

1

13APR17

005

1

14JUN2017

005

2

04APR17

006

1

17 SEP2018

006

3

12OCT2017

 

But when the score is same for all records for a subject I need to get first occurrence  

i.e  in the above table , for subj 004 I need to get the first record flagged instead of second record.Is there any way I can do this.

 

Your replies are highly appreciated.

 

Thanks

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
proc sort data=ex;
by subj descending score;
run;



data ex_t;
set ex;
by subj ;
if first.subj then flag=1;
else flag=0;
run;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
if first.subj then flag=1;
else flag=0;
--
Paige Miller
sri1
Obsidian | Level 7
Then for subjects 005 and 006, will not get the records with maximum score
Ksharp
Super User
proc sort data=ex;
by subj descending score;
run;



data ex_t;
set ex;
by subj ;
if first.subj then flag=1;
else flag=0;
run;
PGStats
Opal | Level 21

Just add date to your sorting order if what you want is the first date with maximum score:

 

proc sort data=ex;
by subj descending score date;
run;

data ex_t;
set ex;
by subj ;
flag = first.subj;
run; 
PG
Astounding
PROC Star

Here's a somewhat more complex solution.  Why go with complexity?  Well, it may be that you don't really want to sort your data in SCORE order.  Perhaps you want to keep it in order by SUBJ DATE the whole time:

 

proc sort data=have;
   by subj date;
run;

data want;
   do until (last.subj);
      set have;
      by subj;
      max_score = max(score, max_score);
   end;
   found_max='N';
   do until (last.subj);
      set have;
      by subj;
      if found_max='N' and score=max_score then do;
         flag=1;
         found_max='Y';
      end;
else flag=0; output; end; drop max_score found_max; run;

The top and bottom loops both read all observations for the same SUBJ.  The top loop finds the maximum SCORE for that SUBJ, and the bottom loop calculates FLAG and outputs observations.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1287 views
  • 0 likes
  • 5 in conversation