## How to creat N*M matrice from N*1 and 1*M

Hi, I created the matrix SurvivalM and the matrix InverseC. I want to the product of the matrix SurvivalM and

t(InverseC)[loc(test^=1)]

Note I also want the to delete the elements  that are 1's

SurvivalM=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then SurvivalM=survival1; else if censored=0 then SurvivalM=survival1/*****Create a 1 by N -matrix of Survival ****/;

end;

print SurvivalM;

InverseC=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then InverseC=1/survival1; else if censored=0 then InverseC=1/*****survivalC is the imputed censored survival and set the real Survivalval to 1**/;

end;

print InverseC;

m=frequency;

Big=J(nrow(n),m,1);

Big=SurvivalM*t(InverseC)[loc(test^=1)] ;

print Big;

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: How to creat N*M matrice from N*1 and 1*M

This statement;

SurvivalM=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then SurvivalM=survival1; else if censored=0 then SurvivalM=survival1/*****Create a 1 by N -matrix of Survival ****/;

end;

print SurvivalM;

InverseC=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then InverseC=1/survival1; else if censored=0 then InverseC=0/*****survivalC is the imputed censored survival and set the real Survivalval to 1**/;

*InverseCC=J(nrow(survival1),1,1);

end;

print InverseC;

m=frequency;

*Big=J(nrow(DM),m,value);

t=(loc(InverseC^=0)); print t;

tt=t(t); print tt;

reset print;

Big=SurvivalM*t(tt);

print Big;

It multiply SurvivalM by the location not the elements in the location.

I need to complete this by Sunday and gor issues

14 REPLIES 14

## Re: How to creat N*M matrice from N*1 and 1*M

It looks like both of your vectors are column vectors, so you want to transpose the second vector and multiply them together, like so:

x = {1, 2, 3};  /* 3 x 1 */

y = {4,5,6,7};  /* 4 x 1 */

m = x * t(y);   /* 3 x 4 */

print m;

You are already using the LOC function in order to locate the observations to be kept, as described in this article: Beware the naked LOC - The DO Loop   The expression

idx = loc( test^=1);

finds obs that are not 1, and the expression

v[idx]

returns a COLUMN VECTOR of the corresponding observations in v.  Thus I suspect the expression

t(InverseC)[loc(test^=1)] should be

t( InverseC[loc(test^=1)] )

if you want to have a row vector for the second factor in the product.

## Re: How to creat N*M matrice from N*1 and 1*M

It does not work, this is the log file. The matrices SurvivalM and InverseC are created and they are both correct but the matrix BIG is not, it state

ERROR: (execution) Matrix has not been set to a value.

Despite I defined it.

Also, I want the name of the Matrix to be M1-Mn when created. How do I do that?

1948 SurvivalM=J(nrow(survival1),1,1);

1949 do i = 1 to n;

1950 if censored=1 then SurvivalM=survival1;

1950! else if censored=0 then

1950! SurvivalM=survival1; /*****Create a 1 by N -matrix of Survival ****/;

1951 end;

1952

1953 print SurvivalM;

1954

1955 InverseC=J(nrow(survival1),1,1);

1956 do i = 1 to n;

1957 if censored=1 then InverseC=1/survival1;

1957! else if censored=0 then

1957! InverseC=1; /*****survivalC is the imputed censored survival and set the real

1957! /*****survivalC is the imputed censored survival and set the real Survivalval to 1**/;

1958

1959 end;

1960 print InverseC;

1961 m=frequency;

1962 Big=J(nrow(DM),m,value);

ERROR: (execution) Matrix has not been set to a value.

operation : J at line 1962 column 9

operands : _TEM1001, m, value

_TEM1001 1 row 1 col (numeric)

20

m 1 row 1 col (numeric)

4

value 0 row 0 col (type ?, size 0)

statement : ASSIGN at line 1962 column 4

1963 Big=SurvivalM*t( InverseC[loc(test^=1)] );

ERROR: (execution) Matrix has not been set to a value.

operation : ^= at line 1963 column 41

operands : test, *LIT1059

test 0 row 0 col (type ?, size 0)

*LIT1059 1 row 1 col (numeric)

1

statement : ASSIGN at line 1963 column 7

1964 print Big;

1965 U=ranuni(-1);

## Re: How to creat N*M matrice from N*1 and 1*M

There are two errors:

1) The VALUE matrix is undefined.

Since BIG is being assigned in the line after the error, you do not need to use the J function to allocate BIG. Just delete the line BIG=J(...);

2) The TEST matrix is undefined.

Probably you need to include TEST on the list of variables that you are reading from a SAS data set.

## Re: How to creat N*M matrice from N*1 and 1*M

Test as variable with values all equal to 1 that is test=1 as a variable that is

test

1

1

1

1?

## Re: How to creat N*M matrice from N*1 and 1*M

Sir, Please THis is the entiredcode, w here do I have to define test:

proc iml;

use KMM;

read all var{time censored  survival1 frequency } into DM;

close;

frequency =DM[,4]; survival1 =DM[,3]; Censored= DM[,2]; time = DM[,1];

n = nrow(DM);

segment = J(nrow(DM),1,1); print segment;

do i = 2 to nrow(DM);

if ((Censored=1)=(Censored[i-1]=1)) then segment=segment[i-1];  /**/        /* coming up with unique segments for censored*****/;

else if ((Censored=1)=1-(Censored[i-1]=1)) then segment=segment[i-1]+1;

print time survival1 censored frequency segment;

print segment;

end;

print censored time Censored survival1 frequency segment;

SurvivalM=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then SurvivalM=survival1; else if censored=0 then SurvivalM=survival1/*****Create a 1 by N -matrix of Survival ****/;

end;

print SurvivalM;

InverseC=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then InverseC=1/survival1; else if censored=0 then InverseC=1/*****survivalC is the imputed censored survival and set the real Survivalval to 1**/;

end;

print InverseC;

*m=frequency;

*Big=J(nrow(DM),m,value);

Big=SurvivalM*t( InverseC[loc(test^=1)]);

print Big;

## Re: How to creat N*M matrice from N*1 and 1*M

I don't understand what the variable is supposed to do, but if you just want to define it so that the program runs, put

test = j(nrow(survival1),1,0);

after the CLOSE statement.

[EDIT: The next statement is wrong. Sorry! -Rick]

It looks to me that your program won't work unless TEST is all zeros. If there is a 1, then the SurvivalM and the InverseC [loc(..)] vectors are different sizes and the inner product will fail.

## Re: How to creat N*M matrice from N*1 and 1*M

It is suppose to work because  SurvivalM is Nx1 and t(InverseC [loc(..)]) suppose to be 1Xm to give and MXm matrix. THis runs but it does not delete the rows with 1's in  InverseC.

Thanks

## Re: How to creat N*M matrice from N*1 and 1*M

Sorry. I was thinking "inner product" when this is an outer product. Ignore my last remark.

## Re: How to creat N*M matrice from N*1 and 1*M

I think my question might not be clear, I want to delete all the elements in InverseC that are 1s, this way I will have a reduce matrix of N-deleted X1, next I transpose it to to 1 X N-deleted.

Since SurvivalM is N X1 and the transpose of the reduced Inverse is 1 X N-deleted. I will end up with a N X N-deleted matrix. THis is not doing that;

test = j(nrow(InverseC),1,0);

Big=SurvivalM*t( InverseC[loc(test^=1)]);

print Big;

## Re: How to creat N*M matrice from N*1 and 1*M

use loc(InverseC^=1)

## Re: How to creat N*M matrice from N*1 and 1*M

It does not work, this are my two matrices. The fisrt is 20X1 and the second is 20X1;. Once I delete all 1s in the second, suppose to be 3X1, then transpose to have 1X3. THus 20X1 multiply 1X3=20X3

 0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5 0.45 0.4 0.35 0.320833 0.291667 0.255208 0.21875 0.145833 0.0729167 0.0729167

 1 1 1 1 1 1 1 1 1 1 1 1 1 3.11688 1 3.91837 1 1 1 13.7143

## Re: How to creat N*M matrice from N*1 and 1*M

THis is the error message;

print InverseC ;

3218 m=frequency;

3219 t = j(nrow(InverseC),1,1);

3220 t=loc(InverseC^=1);

3221 Big=SurvivalM*t(t)

ERROR: (execution) Matrices do not conform to the operation.

operation : *` at line 3221 column 20

operands : SurvivalM, t

SurvivalM 20 rows 1 col (numeric)

t 1 row 3 cols (numeric)

14 16 20

statement : ASSIGN at line 3221 column 7

3222 print Big;

It clearly state

SurvivalM 20 rows 1 col (numeric)

and

t 1 row 3 cols (numeric)

How comes it does not give me a 20X3 matrix. ???

## Re: How to creat N*M matrice from N*1 and 1*M

This statement;

SurvivalM=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then SurvivalM=survival1; else if censored=0 then SurvivalM=survival1/*****Create a 1 by N -matrix of Survival ****/;

end;

print SurvivalM;

InverseC=J(nrow(survival1),1,1);

do i = 1 to n;

if censored=1 then InverseC=1/survival1; else if censored=0 then InverseC=0/*****survivalC is the imputed censored survival and set the real Survivalval to 1**/;

*InverseCC=J(nrow(survival1),1,1);

end;

print InverseC;

m=frequency;

*Big=J(nrow(DM),m,value);

t=(loc(InverseC^=0)); print t;

tt=t(t); print tt;

reset print;

Big=SurvivalM*t(tt);

print Big;

It multiply SurvivalM by the location not the elements in the location.

I need to complete this by Sunday and gor issues

## Re: How to creat N*M matrice from N*1 and 1*M

I finally figure it out, this is what I wrote;

t=InverseC[(loc(InverseC^=0))]; print t;

tt=t(t); print tt;

Big=SurvivalM*(tt);

print Big;

Putting everything on the same lile does not work

From The DO Loop