BookmarkSubscribeRSS Feed
LimM
Calcite | Level 5

Hi all,

 

I would like to know what SAS codes I can use to decide the values of three ranking variables (Top1 to Top3) based on multiple existing variables (Score_A to F) as below.

 

Score_A to F are the scores for A, B, C, D, F for each observation. Top1 to 3 are the new variables that I want to create to show among ABCDF, which would go to top1 to top 3 for each observation. Case 1 assigns only one to each of the new variables and Case 2 would assign two if they have the same scores. Thank you in advanced!

 

Case1

 Top1Top2Top3Score_AScore_BScore_CScore_DScore_F
obs1ABD10.900.60.6
obs2DFC00.20.611

 

Case 2

 

 Top1Top2Top3Score_AScore_BScore_CScore_DScore_F
obs1ABD F10.900.60.6
obs2D FCB00.20.611
3 REPLIES 3
novinosrin
Tourmaline | Level 20

32 Bit machine

 

data have;
input Score_A	Score_B	Score_C	Score_D	Score_F;
cards;
1	0.9	0	0.6	0.6
0	0.2	0.6	1	1
;

data want;
set have;
array s(*)score_a--score_f;
array top(5) ;
array r[*] top5-top1;
call poke(put(peekc(addr(s(1)),40),40.),addr(top[1]), 40);
call sortn(of r[*]);
/*drop top5 top6;*/
run;

64 Bit Machine

 

data have;
input Score_A	Score_B	Score_C	Score_D	Score_F;
cards;
1	0.9	0	0.6	0.6
0	0.2	0.6	1	1
;

data want;
set have;
array s(*)score_a--score_f;
array top(5) ;
array r[*] top5-top1;
call pokelong(put(peekclong(addrlong(s(1)),40),40.),addrlong(top[1]), 40);
call sortn(of r[*]);
/*drop top5 top6;*/
run;
novinosrin
Tourmaline | Level 20

HI @LimM Welcome to SAS forum. The above is a little incomplete for the reason you need whichn/vname. Me being too lazy didn't do that part although you will get that from someone. Nonetheless, i encourage you to try the remaining 

PGStats
Opal | Level 21

Much easier if you transpose first

 

data have;
input Obs Score_A	Score_B	Score_C	Score_D	Score_F;
cards;
1 1	0.9	0	0.6	0.6
2 0	0.2	0.6	1	1
;

proc transpose data=have out=scores name=score;
by obs;
var Score_A -- Score_F;
run;

proc sort data=scores; by obs descending col1 score;

data want;
id = 0;
do until(last.obs);
    set scores; by obs descending col1;
    if first.col1 then Id = Id + 1;
    array top{5} $10;
    top{Id} = catx(" ", top{Id}, substr(score, 7, 1));
    end;
keep obs top1-top3;
run;

proc print data=want noobs; run;
PG

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1016 views
  • 0 likes
  • 3 in conversation