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

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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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