Help using Base SAS procedures

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

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

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

V1V2V3V4V5
21463
4521
782
39
5

and Data Y

A1A2A3A4A5
21356

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

V1V2V3V4V5V_New
2146318
4521530
782630180
39927135810
5101030150900

Thank you in advance.

Nik


Accepted Solutions
Solution
‎04-02-2015 01:42 PM
Respected Advisor
Posts: 3,799

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;
    4-2-2015 12-38-27 PM.png

    View solution in original post


    All Replies
    Solution
    ‎04-02-2015 01:42 PM
    Respected Advisor
    Posts: 3,799

    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;
    4-2-2015 12-38-27 PM.png
    Super User
    Posts: 10,035

    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
    • 211 views
    • 5 likes
    • 3 in conversation