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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

3 REPLIES 3
LinusH
Tourmaline | Level 20
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
DavidLie
Obsidian | Level 7

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

Patrick
Opal | Level 21

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;

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
  • 3 replies
  • 862 views
  • 0 likes
  • 3 in conversation