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

I'm perhaps over-complicating this problem, and for various reasons I'm keen to keep this in IML as it is part of an iterative process which requires matrix calculations later on.  I wondered if there was some way I could use the LOC function but thinking probably not.

 

High level description of problem

I have a list of percentages.  The percentages determine the rate you receive.  I want to re-code the original percentages in one vector into the target rates in another vector.  

 

Detail 

I wish to re-code each element of Vector A into a value in Vector C.  

Vector B lets us determine the column of Vector C we want to return. So, any element with value less than or equal to 0.5 falls into category 1 (i.e. that is returns 0.02 from Vector C); between 0.51 and 0.55 (inclusive) would be category 2, and so on.  

 

Example

So, in this example, element 1 of Vector A is 0.78 - it falls into category 7 (determined by vector B).  Therefore, element 1 of the resulting vector will be 0.08 (which matches column 7 of vector C). 

 

Vector A contains the percentages which I want to re-code.

  

M1M2M3M4
0.780.750.720.69
 

 

Vector B contains contains the categories (these allow me to determine which column of vector C i need).

 

LookupVector         
0.50.550.60.650.70.750.80.850.90.951

 

Vector C contains the target rates

 

Match50Match55Match60Match65Match70Match75Match80Match85Match90Match95Match100
0.020.030.040.050.060.070.080.090.10.110.12

 

The output would look like this :

0.080.070.070.06


I have solved this using two DO loops (see attached), but I really believe this won't be the most efficient way to do it.  This will be repeated many times so removing any loops will be important.  

 

Thanks in advance for any help.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Sure, you need to use the BIN function, which enables you to specify cut points. The values of A are binned into integer indices that correspond to the cut points in B.  You can then use those indices to extract the target rates in the C vector.

For reference, you can read about the BIN function in the article "Bin observations by using custom cut points".

 

proc iml;
A = {0.78	0.75	0.72	0.69};  /* values */
B = 0 || do(0.5, 1, 0.05);      /* cutpoints */
binIdx = bin(A, B);
C = do(0.02, 0.12, 0.01);
RatesForA = C[,binIdx];
print RatesForA;

View solution in original post

4 REPLIES 4
Rick_SAS
SAS Super FREQ

Sure, you need to use the BIN function, which enables you to specify cut points. The values of A are binned into integer indices that correspond to the cut points in B.  You can then use those indices to extract the target rates in the C vector.

For reference, you can read about the BIN function in the article "Bin observations by using custom cut points".

 

proc iml;
A = {0.78	0.75	0.72	0.69};  /* values */
B = 0 || do(0.5, 1, 0.05);      /* cutpoints */
binIdx = bin(A, B);
C = do(0.02, 0.12, 0.01);
RatesForA = C[,binIdx];
print RatesForA;
Andrew_
Fluorite | Level 6

Thanks Rick, this solution looks good, it was exactly what I was looking for.

 

I unfortunately only have SAS 9.2 so I am unable to invoke the BIN function Man Sad However, I'll retain this for future use.

 

Much appreciated,

Andrew

Rick_SAS
SAS Super FREQ

Please engage management and IT. SAS 9.2 is more than 8 years old and there have been seven releases of SAS since then.  In SAS/IML alone there have been about 50 new functions added.

 

You can use one DO loop and the LOC function t0 write your own BIN function. Then use the previous solution.

 

 

Andrew_
Fluorite | Level 6

Hi Rick,

 

Unfortunately I am working a contractor so my weight is pretty low.  I've been informed plans for an upgrade are afoot, but it may be after I leave.

 

As always thanks very much for the support.

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
  • 4 replies
  • 1275 views
  • 2 likes
  • 2 in conversation