Statistical programming, matrix languages, and more

Re-code each element of a vector efficiently

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Re-code each element of a vector efficiently

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.


Accepted Solutions
Solution
‎07-12-2016 04:57 AM
SAS Super FREQ
Posts: 3,624

Re: Re-code each element of a vector efficiently

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


All Replies
Solution
‎07-12-2016 04:57 AM
SAS Super FREQ
Posts: 3,624

Re: Re-code each element of a vector efficiently

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;
Occasional Contributor
Posts: 7

Re: Re-code each element of a vector efficiently

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

SAS Super FREQ
Posts: 3,624

Re: Re-code each element of a vector efficiently

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.

 

 

Occasional Contributor
Posts: 7

Re: Re-code each element of a vector efficiently

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 407 views
  • 2 likes
  • 2 in conversation