BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
smurray
Calcite | Level 5
I want to write a code to multiply vector 1 starting at the beginning by vector 2, only vector 2 will move down one at every iteration while vector one will always begin at the beginning. The example given is on a much smaller scale, use lags would become much too large (400 periods / 200 variables ).
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Is below doing what you're after?

data have;
  do x=1 to 5;
    y=x+2;
    output;
  end;
  stop;
run;

/* get number of rows used as number of required array elements */
data _null_;
  call symputx('nobs',nobs);
  stop;
  set have nobs=nobs;
run;
%put &=nobs;

data want(drop=_: vx_: vy_:);
  if 0 then set have(keep=x y);
  length result 8;
  
  array vx_ {&nobs} 8;
  array vy_ {&nobs} 8;
  array step_ {&nobs} 8;

  do while(not last);
    set have end=last;
    _i+1;
    vx_[_i]=x;
    vy_[_i]=y;
  end;

  do _i=1 to &nobs;
    _sX=1;
    _startY+1;
    call missing(result, of step_{*});
    do _sy=_startY to dim(vy_);
      step_[_sx]=vx_[_sx]*vy_[_sy];
      _sX+1;
    end;
    x=vx_{_i};
    y=vy_{_i};
    result=sum(of step_[*]);
    output;
  end;
  stop;
run;

proc print data=want;
run;

Patrick_0-1590898965060.png

View solution in original post

5 REPLIES 5
Reeza
Super User
Do you need the intermediate steps or just the final result?
smurray
Calcite | Level 5
Intermediate steps too please
Patrick
Opal | Level 21

Is below doing what you're after?

data have;
  do x=1 to 5;
    y=x+2;
    output;
  end;
  stop;
run;

/* get number of rows used as number of required array elements */
data _null_;
  call symputx('nobs',nobs);
  stop;
  set have nobs=nobs;
run;
%put &=nobs;

data want(drop=_: vx_: vy_:);
  if 0 then set have(keep=x y);
  length result 8;
  
  array vx_ {&nobs} 8;
  array vy_ {&nobs} 8;
  array step_ {&nobs} 8;

  do while(not last);
    set have end=last;
    _i+1;
    vx_[_i]=x;
    vy_[_i]=y;
  end;

  do _i=1 to &nobs;
    _sX=1;
    _startY+1;
    call missing(result, of step_{*});
    do _sy=_startY to dim(vy_);
      step_[_sx]=vx_[_sx]*vy_[_sy];
      _sX+1;
    end;
    x=vx_{_i};
    y=vy_{_i};
    result=sum(of step_[*]);
    output;
  end;
  stop;
run;

proc print data=want;
run;

Patrick_0-1590898965060.png

smurray
Calcite | Level 5
It is- thank you! Is it possible to run this by group (if first start calculation over) - would that be a change to the do while section?
Patrick
Opal | Level 21

@smurray Below one way how to go for processing by group.

data have;
  do group=1 to 3;
    do x=1 to 5;
/*      y=x+1+group;*/
      y=x+2;
      output;
      if group=2 and x=3 then leave;
    end;
  end;
  stop;
run;

/* get max number of rows in a group to define required array elements */
proc sql noprint;
  select max(cnt) into :max_grp trimmed
  from
    (
      select count(*) as cnt 
      from have
      group by group
    )
  ;
quit;
%put &=max_grp;

data want(keep=group x y result step_:);
  if 0 then set have;
  length result 8;
  
  array vx_ {&max_grp} 8;
  array vy_ {&max_grp} 8;
  array step_ {&max_grp} 8;

  do while(not last);
    set have end=last;
    by group;
    _stop+1;
    vx_[_stop]=x;
    vy_[_stop]=y;

    if last.group then
      do;
        do _i=1 to _stop;
          _sX=1;
          _startY+1;
          call missing(result, of step_[*]);
          do _sy=_startY to dim(vy_);
            step_[_sx]=vx_[_sx]*vy_[_sy];
            _sX+1;
          end;
          x=vx_{_i};
          y=vy_{_i};
          result=sum(of step_[*]);
          output;
        end;
        call missing(of _all_);
      end;
  end;
  stop;
run;

proc print data=want;
run;

Patrick_0-1591059952869.png

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1174 views
  • 4 likes
  • 3 in conversation