DATA Step, Macro, Functions and more

SAS Macro for products of column

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

SAS Macro for products of column

[ Edited ]

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


Accepted Solutions
Solution
‎10-07-2015 06:10 AM
Respected Advisor
Posts: 4,173

Re: SAS Macro for products of column

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;

View solution in original post


All Replies
Super User
Posts: 5,424

Re: SAS Macro for products of column

Not obvious for what you are trying to do. Providing corresponding output data sample would help.
Your data is replaces because that's how the data step behaves. Use options mprint; to audit your macro code generation.
Data never sleeps
Contributor
Posts: 21

Re: SAS Macro for products of column

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

Solution
‎10-07-2015 06:10 AM
Respected Advisor
Posts: 4,173

Re: SAS Macro for products of column

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;
🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 197 views
  • 0 likes
  • 3 in conversation