BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
carles
Fluorite | Level 6

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:

Vectormultmatrix.PNG

 

 

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

First of all, why don't you want to so this in IML, when this clearly is a Matrix/Vector problem?

carles
Fluorite | Level 6

I do not have access to the iml package. That is why, else it would be quite straightforward.

PaigeMiller
Diamond | Level 26

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...

--
Paige Miller
Tom
Super User Tom
Super User

Do you have SAS/STAT licensed?  There are many clever ways to use the scoring functionality to implement essentially matrix multiplication.

Tom
Super User Tom
Super User

Pictures are REALLY hard to code from.

Tom
Super User Tom
Super User

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
Astounding
PROC Star

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}; 

Ksharp
Super User
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;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1546 views
  • 1 like
  • 6 in conversation