BookmarkSubscribeRSS Feed
kiddcao
Calcite | Level 5

Hey guys,

What I am trying to do are following: I want to find out if a observation (A) is top 3 across others.

For example,

     A    B    C    D    E    F    G    H   TOP3-A

1  20   30  40   50  60  70   80  90       N

2  80   90  70   80   0    0     0    0        Y

3  70   0     0    80   90  0    0     0        Y

4  60   70   80  90   0    0     0    0         N

I am thinking transpose + rank + transpose + if <4 then Y else N, however it seems too cumbersome and to be honest as a newbie I do not how to code all these steps correctly...

Thanks!

2 REPLIES 2
KachiM
Rhodochrosite | Level 12

Take the 3-rd largest value. Compare it with A and decide the top3_A flag.

data want;

   set have;

   top3_A = 'N';

   array k

  • A -- H;
  •    top = largest(3, of k

  • );
  •    if A >= top then top3_A = 'Y';

    drop top;

    run;

    Loko
    Barite | Level 11

    Hello,

    data have;
    input  A    B    C    D    E    F    G    H ;
    datalines;
    1  20   30  40   50  60  70   80  90
    2  80   90  70   80   0    0     0    0
    3  70   0     0    80   90  0    0     0
    4  60   70   80  90   0    0     0    0
    ;


    data want;
    set have;

    TOP3_A="N";
    do i=1 to 3;
    if B=largest(i, B,C,D,E,F,G,H) then do;
      TOP3_A="Y";
      go to finish;
    end;
    end;

    finish: ;

    drop i;
    run;

    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!

    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
    • 2 replies
    • 707 views
    • 0 likes
    • 3 in conversation