BookmarkSubscribeRSS Feed
dmluery
Calcite | Level 5

I want to sort a vector in a subroutine that I've written using Proc FCMP. The length of the vector might several thousand. I've tried various permutations of call sortn(...) but I receive syntax errors each time. I've written a routine that will do a quick sort but would prefer to use a SAS sort. While I am working on the rest of my program, I am doing something very crude: use write_array to write the data to a file, use run_macro to proc sort the file, and use read_array to read it back in. Since I will be calling the subroutine 10's of thousands of times, this stopgap is going to be too slow.

4 REPLIES 4
JacobSimonsen
Barite | Level 11

Hi dmluery,

I just had the same problem. Found a good paper by Paul Dorfman (http://www2.sas.com/proceedings/sugi26/p096-26.pdf), and I was able to recode his quicksort implementation into a fcmp-function.

The function in the attached file will sort a matrix on the first collumn, and the rest of the collumn will follow the order of the first collumn. It is some time since you posted your question, so if you have found a better solution than mine, then I will be happy to know.

The sort-function in the attached file can be tested with this small program, which create a matrix, call the sort-function, and verify that the result is correct;

Data _Null_;

  Array A{1:12345,3} _Temporary_; *Array To Sort;

  Do J=Lbound(A) To Hbound(A);

  a[j,1] = -2e5 + Ceil(Ranuni(1)*4e5);

  *here i just put in some numbers in second and third collumn;

  a[j,2]=a[j,1];a[j,3]=a[j,1]+1;

  End;

  Lb = Lbound(A,1);

  Hb = Hbound(A,1);

  Array F{-200000:200000} _Temporary_; *Frequency Array;

  Do J=Lb To Hb;

    F[a[j,1]] ++ 1;

  End;

  call quicksort(a);

  *verify;

    Do J=Lb To Hb-1 Until(a[j,1] &Seq A[J+1,1]); End;

  If J = Hb Then Put 'Array Is Sorted.';

  Do J=Lb To Hb; F[a[j,1]] +- 1; End;

  Do J=Lbound(F) To Hbound(F) Until (F) ; End;

  If J = Hbound(F)+1 Then Put 'Data Are Valid.';

* call show(a);

  Stop;

run;

FriedEgg
SAS Employee

How about providing an example of what you are trying to do.  There shouldn't be anything inherently preventing you from using sortn inside of a FCMP function.  Here is a useless example that show's it working....

proc fcmp outlib=work.func.foo;

function myfunc(a

  • );
  • call sortn(a);

    return (a[dim(a)]);

    endsub;

    *test inside fcmp;

    array a[5] (1 1 1 1 5000);

    x=myfunc(a);

    put x=;

    run;

    *test inside datastep;

    options cmplib=work.func;

    data _null_;

    array a[5] _temporary_ (4*1 5000);

    x=myfunc(a);

    put x=;

    run;

    JacobSimonsen
    Barite | Level 11

    The problem is that the rows in the matrix should be sorted on just one of the collumns:

    for example the matrix

    1,3,4

    5,1,1

    2,3,4

    4,2,5

    sorted on the first collumn becomes

    1,3,4

    2,3,4

    4,2,5

    5,1,1

    As far as I know (but maybe Im wrong) the sortn function will sort all entries in the matrix.

    FriedEgg
    SAS Employee

    Yes, well that would be the issue for a matrix, but the OP stated they were trying to sort a vector.  To sort each independent vector of a matrix would be possible using multiple calls to sortn, but really, it's not the question at hand.

    sas-innovate-2024.png

    Available on demand!

    Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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