From: Ajay Ohri on
I agree with Phil

Matrix multiplication is very useful in R and you can use the Bridge to R if you don't have a recurring need for IML

Here's a question R is bad with big datasets so for big matrices is it netter to use sas iml or just add more ram or are there any ways to do it in SAS base

Sent from my iPhone

On Oct 10, 2009, at 6:50 PM, Philip Rack <PhilRack(a)MINEQUEST.COM> wrote:

This is an ideal application for R. I'm no expert on R but I'm starting to
believe that R can replace a lot of SAS/IML functionality. I wrote a Bridge
to R for both SAS and WPS and you can find it on the MineQuest website at:
http://www.minequest.com/BridgetoR.html

Here's some code that demonstrates using R and how to perform scalar and
matrix multiplication. Using your sample data, the code you would use is
below. I tried to comment the code as much as reasonably possible.

I've also put this program inside the samples folder of the zip file for the
Bridge to R.


*--------------- Matrix Multiplication ------------------;

data a;
input a b;
cards;
1 2
3 4
;
run;


data b;
input c d;
cards;
4 5
6 7
;
run;

/* Output the two data sets as CSV files for R */
Proc Export data=a outfile="c:\temp\a.csv"
dbms=csv replace;
run;

Proc Export data=b outfile="c:\temp\b.csv"
dbms=csv replace;
run;



/* Start the Bridge to R */

%Rstart(man,,nographwindow);
datalines4;

# Read in the csv files created in SAS/WPS
a <- read.csv(
file="C:\\temp\\a.csv",head=TRUE,sep=",")

b <- read.csv(
file="C:\\temp\\b.csv",head=TRUE,sep=",")

amat = as.matrix(a) # Create matrix amat
bmat = as.matrix(b) # Create matrix bmat

amat # Print amat
bmat # Print bmat

# Scalar multiply
c <- 3 # assign c the value of 3
c*amat # multiply it out
c # Print c

# Matrix multiply
dmat <- amat %*% bmat # Multiply the matrices
dmat # Print dmat

# Write matrix dmat to a csv file
write.table(dmat,
file = "c:\\temp\\foo.csv",
sep = ",", col.names = NA, qmethod = "double")

;;;;
%Rstop;



proc import datafile = "c:\temp\foo.csv"
out=mydata dbms=csv replace;
getnames=yes;
run;

*----------------- End Code -------------------;





Philip Rack
MineQuest, LLC
SAS & WPS Consulting and WPS Reseller
Tel: (614) 457-3714
Web: www.MineQuest.com
Blog: www.MineQuest.com/WordPress


-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L(a)LISTSERV.UGA.EDU] On Behalf Of gupt
Sent: Friday, October 09, 2009 2:15 AM
To: SAS-L(a)LISTSERV.UGA.EDU
Subject: matrix mulitplication...................

Hi,
I am having one doubt can we do matrix multiplication by using sas. I
think we have to follow arrays concept. suppose I am having the data
sets like the following.

data set name: first
column names : A B

A B
1 2
3 4

another data set name : second
column names : C D

C D
4 5
6 7

I want to get the output like the following
data set name : Mulitiply_first_second
column names : E F
E F
16 19
36 43

In the final data set i need to get the matrix mulitplication.........
If anybody help me to get the above code it will be very useful to
me......
From: oloolo on
for all the nice suggestions, here is mine, without resorting to IML or R
or write your own DATA STEP
*******************************;
data a;
A=1; B=2; output;
A=3; B=4; output;
run;
data b;
C=4; D=5; output;
C=6; D=7; output;
run;
proc transpose data=b out=b_t; run;
data b_t;
set b_t;
rename Col1=A Col2=B;
_TYPE_='PARMS' ;
run;
proc score data=a score=b_t out=C type=parms;
var A B;
run;
*******************************;


On Sat, 10 Oct 2009 11:12:18 -0400, Arthur Tabachneck <art297(a)NETSCAPE.NET>
wrote:

>Gupta,
>
>And, if you license it, there is SAS/IML. See, e.g.,
>
>http://www.google.ca/url?
>sa=t&source=web&ct=res&cd=2&ved=0CA0QFjAB&url=http%3A%2F%2Fsupport.sas.com%
2
>Fdocumentation%2Fonlinedoc%2F91pdf%2Fsasdoc_91%
2Fiml_ug_7306.pdf&ei=fqLQSobe
>JYqOlQfMh9WSAw&usg=AFQjCNFSU6CB-
>9QxhcGq_40DC6sTahPclw&sig2=Fm8EENnC03qKJddjf2vDXg
>
>or, in short form: http://xrl.us/bfrf9q
>
>HTH,
>Art
>--------
>On Sat, 10 Oct 2009 10:55:03 -0400, Muthia Kachirayan
><muthia.kachirayan(a)GMAIL.COM> wrote:
>
>>On Fri, Oct 9, 2009 at 2:15 AM, gupt <pvsgupta(a)gmail.com> wrote:
>>
>>> Hi,
>>> I am having one doubt can we do matrix multiplication by using sas. I
>>> think we have to follow arrays concept. suppose I am having the data
>>> sets like the following.
>>>
>>> data set name: first
>>> column names : A B
>>>
>>> A B
>>> 1 2
>>> 3 4
>>>
>>> another data set name : second
>>> column names : C D
>>>
>>> C D
>>> 4 5
>>> 6 7
>>>
>>> I want to get the output like the following
>>> data set name : Mulitiply_first_second
>>> column names : E F
>>> E F
>>> 16 19
>>> 36 43
>>>
>>> In the final data set i need to get the matrix mulitplication.........
>>> If anybody help me to get the above code it will be very useful to
>>> me......
>>>
>>
>>Gupta,
>>
>>Matrix Multiplication through SAS datastep can be done - only we need
>>imagination. Let us take a very general situation of two matrices with
>>sizes 2 by 3 and 3 by 3 to yield a product matrix of 2 by 3. Dataset ONE
>has
>>2 rows and 3 columns and TWO has 3 rows and 3 columns.
>>
>>data one;
>>input a b c;
>>cards;
>>1 0 3
>>2 -1 -2
>>;
>>run;
>>data two;
>>input d e f;
>>cards;
>>-2 4 2
>> 1 0 0
>>-1 1 -1
>>;
>>run;
>>
>>The datastep takes one row of ONE and builds up sums of products by
cycling
>>through all rows of TWO using relevant elements. The cycling of TWO
>requires
>>the use of POINT option in SET statement.
>>
>>data mult;
>>array prod[*] p1 p2 p3;
>>set one;
>>array k[*] a b c; *** Vars of dataset ONE;
>>i = 0;
>>do row = 1 to n; *** Loop through all rows of TWO;
>> set two nobs = n point = row;
>> array m[*] d e f; *** Vars of dataset TWO;
>> i + 1;
>> do j = 1 to dim(m);
>> prod[j] = sum(prod[j], k[i] * m[j]);
>> end;
>>end;
>>output;
>>call missing(of prod[*]);
>>keep p:;
>>run;
>>
>>It will far easy to do this or any complicated matrix operations by the
use
>>of user- definable Function Compiler facility in SAS 9. Here goes.
>>
>>proc fcmp;
>> array X[2,3]/nosymbols;
>> array Y[3,3]/nosymbols;
>> array result[2,3];
>> rc = read_array('one',X);
>> rc = read_array('two',Y);
>> call mult(X, Y, result);
>> rc = write_array('mult', result);
>>quit;
>>
>>Enough temporary array space is declared to save the datasets. ONE goes to
>>array X and TWO goes to Y.
>>
>>READ_ARRAY() translates the datasets to arrays and similarly WRITE_ARRAY()
>>does the reverse into MULT from RESULT array.
>>
>>Call mult() is a SAS built-in matrix multiplication function.
>>
>>The output of RESULT :
>>
>> Obs result1 result2 result3
>> 1 -5 7 -1
>> 2 -3 6 6
>>
>>Do you find this useful?
>>
>>Kind regards.
>>
>>Muthia Kachirayan