Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:42:04+00:00 2026-06-06T18:42:04+00:00

I have a matrix in SAS/IML: x = {7 6 3 3 8, 2

  • 0

I have a matrix in SAS/IML:

x = {7 6 3 3 8,
     2 3 5 2 5,
     2 6 4 3 8,
     7 4 8 1 3,
     8 8 6 8 7,
     3 2 6 1 5 };

I want to create a new matrix that contains the highest k values of each column in x. For example, if k=3, I want the result matrix to contain:

8  8  8  8  8 
7  6  6  3  8
7  6  6  3  7

because, for instance, the largest 3 numbers in the first column of x are 8, 7, and 7.

I’ve unsuccessfully tried to figure out how to do this using the rank function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:42:05+00:00Added an answer on June 6, 2026 at 6:42 pm

    Your code looks fine. Here’s a minor revision:

    do c=1 to ncol(x);   
        r = rank(x[,c]);
        y = x[loc(r>=nrow(x)-k+1), c];
        call sort(y);
        tops[,c] = y;
    end;
    

    As to avoiding the loop to make it faster, it’s not necessary. Even with 10,000 columns, this code runs in a fraction of a second. Try running the following timing code:

    x = j(500, 10000);
    call randgen(x,"normal");
    k = 3;
    t0=time();
    tops = j(k,ncol(x),0);
    do c=1 to ncol(x);   
        r = rank(x[,c]);
        y = x[loc(r>=nrow(x)-k+1), c];
        call sort(y);
        tops[,c] = y;
    end;
    t=time()-t0;
    print t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix in SAS IML. For each pair of rows (say vectors
I have a matrix and i want to create a new matrix which will
I have a matrix of objects that contains data in this form: name A,2,name
I have a matrix (5x5) with values in them for example: Matrix (1,1) Value:
I have a matrix M(x,y). I want to apply a threshold in all values
I have a matrix that stores x, y and z values as so: {x1,
I have a matrix called (b2) that contains 3565 rows and 125 columns with
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix A which I want to convert into a data.frame of
I have a matrix that is about 11,000 x 1,000, saved as a csv.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.