Variable names in the dataset are not directly accessed within the

matrix-endmatrix command.

If you add a 'SET MPRINT ON.' you will see the generated macro code.

The line

get y /var = !gr.

reads the *dataset* variable y into the *matrix* variable y. They are not

identical!

Later, in the !do loop you have the line

compute flag = (!gr = !i) &* ind .

which will substitute your call with 'gr=y' to be

compute flag = (y = !i) &* ind .

This will work as the names are identical.

When called with another variable, ie 'gr=z', the substitution generates

compute flag = (z = !i) &* ind .

This fails as z is here an unknown matrix variable.

The correct line in the macro should be

compute flag = (y = !i) &* ind .

as whatever !gr is read into *matrix* y

HTH, PR (My additions in capital letters)

*Always helpful with demo data*.

DATA LIST LIST /

y z dv1 dv2 dv3 (5F8.0).

BEGIN DATA

1 1 1 1 1

1 1 3 3 3

2 2 2 2 2

2 2 4 4 4

END DATA.

DATASET NAME TwoGroupMatrix.

SET MPRINT ON.

define sep (vars = !charend('/')

/gr = !charend('/')

/max = !charend('/')

) .

set mxloops = 100000000 .

/* Separate the two groups. */

compute srt = !gr .

sort cases by srt(A) .

matrix .

get x /var = !vars .

get y /var = !gr .

compute ind = t(1:nrow(x)) .

compute lst = 0 .

!do !i = 1 !to !unquote(!max) .

compute flag = (y = !i)&*ind .

compute fst = lst+1 .

compute last = cmax(flag) .

do if last .

compute lst = last .

compute !conc(x,!i) = x(fst:lst,:) .

end if .

!doend .

print fst .

print lst .

print flag .

PRINT X.

PRINT X1.

PRINT X2.

DISPLAY.

end matrix .

!enddefine.

COMMENT : following line not preceeded by preserve

restore .

sep vars = dv1 to dv3

/gr = y

/max = '2'

.

sep vars = dv1 to dv3

/gr = z

/max = '2'

.

bdates wrote

> I'm missing something, but I'm not sure what. This is a snippet of syntax

> based on Kirill's macro for separating a matrix horizontally. I have data

> with two groups, coded 1 and 2. That separates the matrix into two

> separate matrices. 'Get x' produces a matrix of dependent variables. 'Get

> y' extracts the vector of 1's and 2's presorted. The syntax only works if

> !gr labeled the same as 'y'. The glitch is in the division of matrix 'x'.

> Prior to that, 'y' can refer to any !gr. However, in order for the

> division to work, !gr must be coded the same as the second 'get' variable

> (see blue highlighted). In this case it's 'y', but it could be any name

> for the vector used to split the 'x' matrix. !max refers to the maximum

> value by which to split matrix 'x'. I'm assuming others using matrix will

> be able to spot the error in my logic.

>

> ...

>

> Thanks in advance.

>

> Brian

>

> =====================

> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

> (not to SPSSX-L), with no body text except the

> command. To leave the list, send the command

> SIGNOFF SPSSX-L

> For a list of commands to manage subscriptions, send the command

> INFO REFCARD

--

Sent from:

https://nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fspssx-discussion.1045642.n5.nabble.com%2F&data=02%7C01%7Cbdates%40SWSOL.ORG%7C7e6094053143483e096b08d784c1b953%7Cecdd61640dbd4227b0986de8e52525ca%7C0%7C0%7C637123839899062043&sdata=qFk3HYxBLFIp9PSfdUEe0I9VLX7WbMFlbxpxydTJANw%3D&reserved=0
=====================

To manage your subscription to SPSSX-L, send a message to

[hidden email] (not to SPSSX-L), with no body text except the

command. To leave the list, send the command

SIGNOFF SPSSX-L

For a list of commands to manage subscriptions, send the command

INFO REFCARD