Obsidian | Level 7

## Write codes to show increment of a sequence

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

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Write codes to show increment of a sequence

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;``````
7 REPLIES 7
Opal | Level 21

## Re: Write codes to show increment of a sequence

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;`

Super User

## Re: Write codes to show increment of a sequence

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;``````
Obsidian | Level 7

## Re: Write codes to show increment of a sequence

What if I have many values? How to expand v{i} = v{i} * v{i-1}; ?
Obsidian | Level 7

## Re: Write codes to show increment of a sequence

Got it. Thanks!
Obsidian | Level 7

## Write efficient code for a sequence

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)

Super User

## Re: Write efficient code for a sequence

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.

Obsidian | Level 7

## Re: Write efficient code for a sequence

Discussion stats
• 7 replies
• 257 views
• 0 likes
• 4 in conversation