Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Sorting within a subroutine created with PROC FCMP

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-13-2014 02:49 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-08-2014 05:38 PM

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

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

* call show(a);

Stop;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-08-2014 06:11 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-08-2014 06:34 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-08-2014 06:43 PM

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.