Statistical programming, matrix languages, and more

How to get the median and other quantiles of data in the same row of several vectors?

Reply
Contributor
Posts: 22

How to get the median and other quantiles of data in the same row of several vectors?

How to get the median and other quantiles of data in the same row of several vectors using IML functions?

SAS Super FREQ
Posts: 3,221

Re: How to get the median and other quantiles of data in the same row of several vectors?

Look at the QNTL subroutine. The QNTL function operates on columns of a matrix, so if you have several vectors you can concatenate them together (use the || operator)  and compute all the quantiles at once.

Contributor
Posts: 22

Re: How to get the median and other quantiles of data in the same row of several vectors?

How to do it using SAS 9.2IML? Thanks!

Frequent Contributor
Posts: 122

Re: How to get the median and other quantiles of data in the same row of several vectors?

You could adapt my code here which uses the SAS pctl function:

http://listserv.uga.edu/cgi-bin/wa?A2=ind0812A&L=sas-l&D=0&P=35674

But this is not a workable solution unless your matrices will always be small.  As Dale said in the same thread:

http://listserv.uga.edu/cgi-bin/wa?A2=ind0812A&L=sas-l&D=0&P=37690

you will probably be better off building your own percentile function.

SAS Super FREQ
Posts: 3,221

Re: How to get the median and other quantiles of data in the same row of several vectors?

   proc iml;

   start myQntl(q, x, p);         /* definition 5 from UNIVARIATE doc */

      y = colvec(x);

      call sort(y,1);

      n = nrow(y);                   /* assume nonmissing data */

      q = j(ncol(p),1);

      do i = 1 to ncol(p);

         j  = (n+1)*p;            /* position in ordered data */

         j1 = floor(j); j2 = ceil(j);/* indices into ordered data */

         print j j1 j2;

         if j1=j2 then               /* return a datum */

            q = y[j1];

         else                        /* interpolate between data */

            q = (y[j2]+y[j1])/2;

      end;

   finish;

   /* test it */

   x = ranuni(j(100,1));

   p = {0.1 0.25 0.5 0.75 0.9};

   call MyQntl(q, x, p);

   print q;

Post a Question
Discussion Stats
  • 4 replies
  • 401 views
  • 0 likes
  • 3 in conversation