🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 05-30-2020 10:47 PM
(2014 views)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
5 REPLIES 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Do you need the intermediate steps or just the final result?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Intermediate steps too please
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;