BookmarkSubscribeRSS Feed
ShufeGuoding
Obsidian | Level 7

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

4 REPLIES 4
Rick_SAS
SAS Super FREQ

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.

ShufeGuoding
Obsidian | Level 7

How to do it using SAS 9.2IML? Thanks!

IanWakeling
Barite | Level 11

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.

Rick_SAS
SAS Super FREQ

   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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 1088 views
  • 0 likes
  • 3 in conversation