Solved
Contributor
Posts: 68

# Successively multiply values of lower part of a matrix with an array

Hi all,

I would appreciate your help on the following.

I have two data sets (arrays)

Data X

 V1 V2 V3 V4 V5 2 1 4 6 3 4 5 2 1 7 8 2 3 9 5

and Data Y

 A1 A2 A3 A4 A5 2 1 3 5 6

I would like to fill the missing lower part of the matrix by successively multiplying the previous non-missing value of Data X with the elements of the Data Y

i.e. for the last observation  10= 5*2 (A1),  10= 10 * 1 (A2), 30=10* 3 (A3) , 150 = 30 * 5 (A4) and 900 = 150 * 6(A5)

Consequently column V_New will be created due to final multiplication.

So  Data Z is the desired one

 V1 V2 V3 V4 V5 V_New 2 1 4 6 3 18 4 5 2 1 5 30 7 8 2 6 30 180 3 9 9 27 135 810 5 10 10 30 150 900

Thank you in advance.

Nik

Accepted Solutions
Solution
‎04-02-2015 01:42 PM
Posts: 3,852

## Re: Successively multiply values of lower part of a matrix with an array

data v;
infile cards expandtabs missover;

input V1-V5;
cards;
2  1  4  6  3
4  5  2  1
7  8  2
3  9
5
;;;;
run;
proc print;

run;
Data a;
infile cards expandtabs;

input A1-A5;
cards;
2  1  3  5  6
;;;;
run;
proc print;

run;
data mult;
if _n_ eq 1 then set a;
set v;
array v
• v: Vx;
•    array a
• a:;
•    do i = n(of v
• );
•       do j = i to dim(a);
v[j+1] = a*v;
end;

end;

drop i j;
run;
proc print;

run;

All Replies
Solution
‎04-02-2015 01:42 PM
Posts: 3,852

## Re: Successively multiply values of lower part of a matrix with an array

data v;
infile cards expandtabs missover;

input V1-V5;
cards;
2  1  4  6  3
4  5  2  1
7  8  2
3  9
5
;;;;
run;
proc print;

run;
Data a;
infile cards expandtabs;

input A1-A5;
cards;
2  1  3  5  6
;;;;
run;
proc print;

run;
data mult;
if _n_ eq 1 then set a;
set v;
array v
• v: Vx;
•    array a
• a:;
•    do i = n(of v
• );
•       do j = i to dim(a);
v[j+1] = a*v;
end;

end;

drop i j;
run;
proc print;

run;
Super User
Posts: 10,770

## Re: Successively multiply values of lower part of a matrix with an array

I like such kind of questions.

```data v;
infile cards expandtabs missover;
input V1-V5;
cards;
2  1  4  6  3
4  5  2  1
7  8  2
3  9
5
;;;;
run;
Data a;
infile cards expandtabs;
input A1-A5;
cards;
2  1  3  5  6
;;;;
run;
data want;
if _n_ eq 1 then set a;
set v;
array x{*} v1-v5 v_new;
array y{*} a1-a5;
do i=1 to dim(x)-1;
temp=x{i}*y{i};
if missing(x{i+1}) then x{i+1}=temp;
end;
drop temp i a:;
run;

```

Xia Keshan

🔒 This topic is solved and locked.

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

Discussion stats
• 2 replies
• 238 views
• 5 likes
• 3 in conversation