Hello Community :),
I am trying to do the following multiplication in SAS and I am getting in trouble. I want to mention that what I call as vector is indeed a dataset with one column in SAS:
I would like to do it with base SAS, but I am getting problems with indices and colnames
DATA WORK.dataset; INPUT X Y; datalines; 1 4 2 3 3 2 ; run; DATA WORK.vector; INPUT vector; datalines; 2 3 ; run;
Thank you for the help in advanced,
DATA WORK.dataset;
INPUT X Y;
datalines;
1 4
2 3
3 2
;
run;
DATA WORK.vector;
INPUT vector;
datalines;
2
3
;
run;
proc transpose data=dataset(obs=0) out=temp;
var _all_;
run;
data key;
merge temp vector;
key=cats(_name_,'=',_name_,'*',vector,';');
run;
data want;
set key end=last;
if _n_=1 then call execute('data want;set dataset;');
call execute(key);
if last then call execute('run;');
run;
First of all, why don't you want to so this in IML, when this clearly is a Matrix/Vector problem?
I do not have access to the iml package. That is why, else it would be quite straightforward.
Without access to IML, you can do this type of vector multiplication using PROC SCORE (but you'd have to have a license for SAS/STAT). Here is an example: https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=statug&docsetTarget=statug_sc...
Do you have SAS/STAT licensed? There are many clever ways to use the scoring functionality to implement essentially matrix multiplication.
Pictures are REALLY hard to code from.
Are you sure that is the output you want?
If so then explain what type of multiplication you are doing.
Here is what we get if we do matrix multiplication in IML.
proc iml;
x={2 3};
y={1 4,2 3,3 2};
print x;
print y;
z=y*x;
print z;
quit;
z 14 13 12
Which you can mimic in data step.
data want;
set dataset;
array row x y ;
Z=.;
do i=1 to dim(row);
set vector point=i;
Z=sum(Z,row[i]*vector);
end;
keep Z ;
run;
proc print data=want; run;
Obs Z 1 14 2 13 3 12
To mimic this in a DATA step, I think it's fastest to in effect transpose the vector:
data want;
array v {99} _temporary_;
if _n_=1 then do until (done);
set vector end=done;
n_terms + 1;
v{n_terms} = vector;
end;
drop n_terms k vector;
set dataset;
array vals {2} X Y;
total = 0;
do k=1 to n_terms;
total + vals{k} * v{k};
end;
run;
It's not 100% clear whether you actually want something slightly different inside the loop, such as forgetting about TOTAL and instead calculating:
vals{k} = vals{k} * v{k};
DATA WORK.dataset;
INPUT X Y;
datalines;
1 4
2 3
3 2
;
run;
DATA WORK.vector;
INPUT vector;
datalines;
2
3
;
run;
proc transpose data=dataset(obs=0) out=temp;
var _all_;
run;
data key;
merge temp vector;
key=cats(_name_,'=',_name_,'*',vector,';');
run;
data want;
set key end=last;
if _n_=1 then call execute('data want;set dataset;');
call execute(key);
if last then call execute('run;');
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.