DATA Step, Macro, Functions and more

find the top n biger value in each obsevation

Reply
Occasional Contributor
Posts: 10

find the top n biger value in each obsevation

I have a data set that have over thousands observations and  over ten variables, and all values are numeric.

what I need to do is find out the top 6 bigger values for each observation.

Any feedback would be greatly appreciatedSmiley Happy

Super User
Super User
Posts: 7,080

Re: find the top n biger value in each obsevation

Posted in reply to LillianLee

Did you mean the top 6 values for each variable?  Then why not use PROC UNIVARIATE.

Occasional Contributor
Posts: 10

Re: find the top n biger value in each obsevation

no, the top 6 values in on observation.

Super User
Posts: 19,878

Re: find the top n biger value in each obsevation

Posted in reply to LillianLee

Sample data and output are always helpful in determining requirements.

Respected Advisor
Posts: 3,799

Re: find the top n biger value in each obsevation

Posted in reply to LillianLee

Perhaps LARGEST function.

data have1;
   array x[10];
   do row=1 to 5;
     
do i=1 to dim(x);
         x=ranuni(1);
         end;
     
output;
     
end;
  
drop row i;
   run;

data largest6;
   set have1;
   array l[6];
   do i=1 to dim(l);
      l=largest(i,of xSmiley Happy;
     
end;
  
run;
proc print;
  
run;
Valued Guide
Posts: 2,177

Re: find the top n biger value in each obsevation

Posted in reply to data_null__

It might be that I'm easily impressed, but continue to be amazed at our contributor who comes up (I expect, first) with these novel functions like

     LARGEST(i, array )

Much appreciation for contributor

Respected Advisor
Posts: 3,799

Re: find the top n biger value in each obsevation

Thank you .  The last few versions of SAS have intruduced some really nice functions.

Respected Advisor
Posts: 3,156

Re: find the top n biger value in each obsevation

Posted in reply to LillianLee

I am not clear on your final output, so this is just a idea that you may start upon, after conversion, n5-n10 will be the top 6 bigger value for each row.

%macro have;

data have;

do row=1 to 5;

%do i=1 %to 10;

      n&i=ranuni(0);

     %end;

output;

end;

run;

%mend;

%have;

data want;

set have;

       array n(10) n:;

      call sortn(of n(*));

/*       put n5-n10;*/

run;

Haikuo

Respected Advisor
Posts: 3,799

Re: find the top n biger value in each obsevation

For a descending sort you can use a descending enumerated range.

data want;
   set have;
   set have(rename=(x1-x10=n1-n10));
   call sortn(of n10-n1);
   drop n7-n10;
   run;
Respected Advisor
Posts: 3,156

Re: find the top n biger value in each obsevation

Posted in reply to data_null__

Thanks, Null. Did not think of that.

Haikuo

Ask a Question
Discussion stats
  • 9 replies
  • 456 views
  • 1 like
  • 6 in conversation