BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hamed_jabarian
Calcite | Level 5

I am striving to calculate Value at risk in financial market, in order to produce 10% VaR in excel it is easy to use Norm.inv(probability,mean,std). what is the same code in IML?

Furthermore in excel we have " small(array,3) " to calculate 3rd smallest value. how is it done in IML?

Please help me in this topic. thanks in advance..

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

The inverse CDF function is called the QUANTILE function in SAS. To compute the inverse CDF of the normal distribution, use the following:

proc iml;

prob=0.5; mean=0; std=1;

q = quantile("Normal", prob, mean, std);

For computing the kth smallest, I'd use the RANK function., which returns the order of each element in a sorted list.  You can use the LOC function to find the index that contains the kth smallest, and then extract the value, as follows:

x = 100:91;

r = rank(x);

idx = loc(r=3);   /* location of the 3rd smallest */

small3 = x[idx];  /* value of the 3rd smallest */

View solution in original post

7 REPLIES 7
Rick_SAS
SAS Super FREQ

The inverse CDF function is called the QUANTILE function in SAS. To compute the inverse CDF of the normal distribution, use the following:

proc iml;

prob=0.5; mean=0; std=1;

q = quantile("Normal", prob, mean, std);

For computing the kth smallest, I'd use the RANK function., which returns the order of each element in a sorted list.  You can use the LOC function to find the index that contains the kth smallest, and then extract the value, as follows:

x = 100:91;

r = rank(x);

idx = loc(r=3);   /* location of the 3rd smallest */

small3 = x[idx];  /* value of the 3rd smallest */

Ksharp
Super User

Rick,

What if there are some ties ? How to get rid of them ?

proc iml;
m = { 1 2 0,
      2 4 0,
      10 11 12,
      2 2 2};
r=rank(m);
x=loc(r=3); 
smallest3=m;
print smallest3;
quit;

Xia Keshan

Rick_SAS
SAS Super FREQ

Why would I want to get rid of them? The algorithm returns the observations with the k_th smallest values.  If you just want one, you can use

smallest3=m[1];

Ksharp
Super User

As you can see the code return 1,but the smallest 3 is 2 . so do I get that 2 ?

Rick_SAS
SAS Super FREQ

Sorry, I didn't understand your question previously.  In your example you have two 0s. Your code returns 1 because that is the third value in a ranked list of the values: 0, 0, 1, 2, 2, 2,....

If you want to return all values that have the third largest UNIQUE value (which is 2), then you can use the UNIQUE-LOC technique: The UNIQUE-LOC trick: A real treat! - The DO Loop

The code would look like this:

m = { 1 2 0,

      2 4 0,

      10 11 12,

      2 2 2};

u = unique(m);     /* unique values */

k = 3;             /* look for the k_th smallest */

idx = loc(m=u); /* find observations */

print (idx`)[L="Obs Num"] (m[idx])[L="Value"];

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 2002 views
  • 6 likes
  • 3 in conversation