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

Dear All,

 

I want to calculate ratio matrix (column values divide by row values) as showing in following table,

 

Is it possible, if yes please guide me do calculation using SAS code.

 

RatioR1R2R3R4R5R6
T1T1/R1T1/R2T1/R3T1/R4T1/R5T1/R6
T2T2/R1T2/R2T2/R3T2/R4T2/R5T2/R6
T3T3/R1T3/R2T3/R3T3/R4T3/R5T3/R6
T4T4/R1T4/R2T4/R3T4/R4T4/R5T4/R6
T5T5/R1T5/R2T5/R3T5/R4T5/R5T5/R6
T6T6/R1T6/R2T6/R3T6/R4T6/R5T6/R6

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @jitendrakoli,

 

You can use PROC SQL to compute the ratios:

/* Create test data for demonstration */

data have;
input Product $ Value;
cards;
T1 88.9
T2 102.3
T3 85
T4 110.5
T5 104.5
T6 98.5
R1 87.8
R2 117.3
R3 104.5
R4 112.3
R5 98.5
R6 109.5
;

/* Compute ratios */

proc sql;
create table want as
select divide(t.value,r.value) as Ratio
from have(where=(product=:'T')) t,
     have(where=(product=:'R')) r
order by r.product, t.product;
quit;

Feel free to add another SELECT item that identifies the value in variable Ratio, e.g.

select catx('/',t.product,r.product) as Formula length=7, divide(t.value,r.value) as Ratio

 

Please note how convenient it is (for volunteers answering your questions) to have input datasets available in the form of a DATA step as shown above. Many people are not willing or allowed to download MS Office files because of security concerns.

 

Edit: Replaced t.value/r.value by divide(t.value,r.value) for more robustness.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Do you have access to PROC IML?

 

Can you show us the original data?


Do you want the result to be a SAS dataset, or a matrix within IML, or a report?

--
Paige Miller
jitendrakoli
Fluorite | Level 6

Thanks for prompt reply.

 

Not having PROC IML.

Please find attached data.

 

Output not wants in matrix, but it should be in one column as below,

Ratio
T1/R1
T2/R1
T3/R1
T4/R1
T5/R1
T6/R1
T1/R2
T2/R2
T3/R2
T4/R2
T5/R2
T6/R2
T1/R3
T2/R3
T3/R3
T4/R3
T5/R3
T6/R3
T1/R4
T2/R4
T3/R4
T4/R4
T5/R4
T6/R4
T1/R5
T2/R5
T3/R5
T4/R5
T5/R5
T6/R5
T1/R6
T2/R6
T3/R6
T4/R6
T5/R6
T6/R6

 

Thanks,

FreelanceReinh
Jade | Level 19

Hello @jitendrakoli,

 

You can use PROC SQL to compute the ratios:

/* Create test data for demonstration */

data have;
input Product $ Value;
cards;
T1 88.9
T2 102.3
T3 85
T4 110.5
T5 104.5
T6 98.5
R1 87.8
R2 117.3
R3 104.5
R4 112.3
R5 98.5
R6 109.5
;

/* Compute ratios */

proc sql;
create table want as
select divide(t.value,r.value) as Ratio
from have(where=(product=:'T')) t,
     have(where=(product=:'R')) r
order by r.product, t.product;
quit;

Feel free to add another SELECT item that identifies the value in variable Ratio, e.g.

select catx('/',t.product,r.product) as Formula length=7, divide(t.value,r.value) as Ratio

 

Please note how convenient it is (for volunteers answering your questions) to have input datasets available in the form of a DATA step as shown above. Many people are not willing or allowed to download MS Office files because of security concerns.

 

Edit: Replaced t.value/r.value by divide(t.value,r.value) for more robustness.

jitendrakoli
Fluorite | Level 6

Thank you very.

 

It working fine

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1732 views
  • 0 likes
  • 3 in conversation