BookmarkSubscribeRSS Feed
kpdoe
Calcite | Level 5

Hello I need help with assigning tag for high score by variable "A" taking a priority regardless of the high score in var "B". With below code, I am not getting the correct output.

 

I have a data: testa

IDsecnd_IDABScore
1234534557 10.00514
12345234563 0.00449
1234534545 10.00384

Output with code below:

IDsecnd_IDABScoreCntTagCode
1234534557 10.005141  
12345234563 0.004492DDelete
1234534545 10.003843DDelete

 

Desired output:

IDsecnd_IDABScoreCntTagCode
1234534557 10.005142DDelete
12345234563 0.004491  
1234534545 10.003843DDelete

 

Code:

proc sort data=testa;
by ID descending score descending A;
run;


data test_keep
 set testa;
by id;
retain cnt;
if first.id = 1 then cnt = 0;
cnt + 1;
if cnt > 1 then tag = 'D'
if tag = 'D' then code = 'Delete';
run;

 

6 REPLIES 6
ballardw
Super User

@kpdoe wrote:

Hello I need help with assigning tag for high score by variable "A" taking a priority regardless of the high score in var "B". With below code, I am not getting the correct output.

 

 


Your code does nothing with the value of variable A so how is a "high score by variable A" determined or evaluated?

 

What exactly does "taking a priority" mean in this context?

 

kpdoe
Calcite | Level 5
Meaning if there's a value in var "A" there should be no tag and code assigned.
Astounding
PROC Star

It sounds like you're close.  Try replacing this statement:

set testa;

Instead, use this and see if it does what you need:

set testa (where=(A > .)) testa (where=(A=.));
kpdoe
Calcite | Level 5

@Astoundingthat worked but, I still want a record with the highest score. For the above scenario the code worked but did not satisfy this one.

Output with your code below:

IDsecnd_IDABScoreCntTagCode
6789035456001 0.015671  
6789037459004 0.042372DDelete
6789075456 0010.009583DDelete

 

Desired output: I need the output with the higher score

IDsecnd_IDABScoreCntTagCode
6789037459004 0.042371  
6789035456001 0.015672DDelete
6789075456 0010.009583DDelete
Astounding
PROC Star

The code looks OK.  Show the log of the program that produced the incorrect results.

Tom
Super User Tom
Super User

This works with your example:

data have;
  input ID secnd_ID A B Score;
cards;
12345 34557 . 1 0.00514
12345 23456 3 . 0.00449
12345 34545 . 1 0.00384
;

proc sort data=have ;
  by id descending score ;
run;

data want;
  set have(where=(not missing(a))) have(where=(missing(a)));
  by id;
  cnt+1;
  if first.id then cnt=1;
  else do;
    tag='D'; code='Delete';
  end;
run;

If you want it to print in the same order then re-sort it.

proc sort data=want ;
  by id descending score ;
run;
Obs      ID     secnd_ID    A    B     Score    cnt    tag     code

 1     12345      34557     .    1    .00514     2      D     Delete
 2     12345      23456     3    .    .00449     1
 3     12345      34545     .    1    .00384     3      D     Delete

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1025 views
  • 0 likes
  • 4 in conversation