Hi all. I would like to transform table 1 to table 2.
Table 2 means the bigger value has to times all the previous values. How do I write the code to show such increment? Thanks!
Table 1:
Value1 Value2 Value3
5 6 7
Table 2:
Value1 Value2 Value3
5 5*6 5*6*7
Use an array:
data want;
set have;
array v {*} value:;
do i = 2 to dim(v);
v{i} = v{i} * v{i-1};
end;
drop i;
run;
Looks like you are drip feeding us part of an unspecified bigger problem. With only 2 variables needing calculation just hard-code the result:
data Have;
Value1 = 5;
Value2 = 6;
Value3 = 7;
run;
data Want;
set Have;
Value2 = Value2 * Value1;
Value3 = Value3 * Value2;
put _all_;
run;
Or if this example is going to grow then here is a more flexible way:
data Want;
set Have;
array values (3) Value1 - Value3;
do i = 1 to 2;
Values(i+1) = Values(i+1) * Values(i);
end;
put _all_;
run;
Use an array:
data want;
set have;
array v {*} value:;
do i = 2 to dim(v);
v{i} = v{i} * v{i-1};
end;
drop i;
run;
Hi all. I would like to write codes to transform table from Table 1 to Table 2. Thank you!
Table 1:
ID Value1 Value2 Value3
1 1 2 3
2 4 5 6
3 7 8 9
Table 2:
ID Value4 Value5 Value6
1 1-(1-1) 1-(1-1)(1-2) 1-(1-1)(1-2)(1-3)
2 1-(1-4) 1-(1-4)(1-5) 1-(1-4)(1-5)(1-6)
3 1-(1-7) 1-(1-7)(1-8) 1-(1-7)(1-8)(1-9)
Are the (1-1)(1-2) supposed to be multiplication? And follow normal rules of arithmetic order such as the result of 1-(1-1)(1-2) is 1 - (result of (1-1) times (1-2) )?
I ask because we get inconsistent usages.
What metric are you using to define "efficient"? Fewer lines of code? Execution time? Memory use? Some combination of these?
If this is a subset of a larger problem you should say so because your number of variables is low enough that you don't gain much in different approaches that might appear if there were 50 values per observation.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.