Hi everyone,
I have a question. I have the following tables:
Price | _1 | _2 | _3 | _4 |
1 | 0.605938 | 0.698997 | 0.631967 | 0.768982 |
1 | 0.979747 | 0.730603 | 0.651564 | 0.755674 |
2 | 0.854955 | 0.524984 | 0.393906 | 0.084612 |
4.5 | 0.214883 | 0.218393 | 0.448404 | 0.011109 |
5.5 | 0.840106 | 0.717766 | 0.078595 | 0.636096 |
I wanted to get product of price and each of the column from column 2 to 5. I did this SAS macros:
%let n_RMA=4;
%macro Crossprice(n_RMA);
%do i=1 %to &n_RMA;
data data.whatever; set data.whatever;
result&i=price*_&i;
run;
%end;
%mend;
%Crossprice(&n_RMA);
I wanted to achieve four separate columns for this but I found out that I only got one column (which is the last calculation of product of price and _4). It seems that result 1 is being replaced by 2 and so on if I use the above code.
I hope you can help me with the coding. Thanks
Code based on my interpretation of your narrative and not the "want to achieve" data you've posted.
data have;
input Price _1 _2 _3 _4;
datalines;
1 2 1 1 8
1 5 10 7 1
2 10 6 2 8
4.5 3 10 4 2
5.5 3 8 6 3
;
run;
data want(drop=_: Price);
set have;
array products {4} Product_1 - Product_4;
array values {*} _1 - _4;
do _i=1 to dim(products);
products[_i]=price*values[_i];
end;
run;
Hi,
Thank you for replying. I am sorry that I did not make myself clear. Let me start over.
Currently I have:
Price | _1 | _2 | _3 | _4 |
1 | 2 | 1 | 1 | 8 |
1 | 5 | 10 | 7 | 1 |
2 | 10 | 6 | 2 | 8 |
4.5 | 3 | 10 | 4 | 2 |
5.5 | 3 | 8 | 6 | 3 |
What do I want to achieve?
Product 1 | Product 2 | Product 3 | Product 4 |
2 | 1 | 1 | 8 |
5 | 10 | 7 | 1 |
10 | 6 | 2 | 8 |
3 | 10 | 4 | 2 |
3 | 8 | 6 | 3 |
Product 1 is achieved through column "Price" x "_1"
Product 2 is achieved through column "Price" x "_2" and so on. This is what I want.
Best,
David
Code based on my interpretation of your narrative and not the "want to achieve" data you've posted.
data have;
input Price _1 _2 _3 _4;
datalines;
1 2 1 1 8
1 5 10 7 1
2 10 6 2 8
4.5 3 10 4 2
5.5 3 8 6 3
;
run;
data want(drop=_: Price);
set have;
array products {4} Product_1 - Product_4;
array values {*} _1 - _4;
do _i=1 to dim(products);
products[_i]=price*values[_i];
end;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.