Matrix Macro Syntax Problem

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix Macro Syntax Problem

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

define seo (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=(!gr=!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 .
end matrix .
!enddefine.
restore .
sep vars=dv1 to dv3/gr=y/max='2' .

Thanks in advance.

Brian
===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Macro Syntax Problem

David Marso-2
Perhaps comment each matrix line with your intention. Would be happy to review after that.

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Macro Syntax Problem

spss.giesel@yahoo.de
Maybe you start with calling the correct macro name:
"seo" or "sep"?

Mario Giesel
Munich, Germany


Am Dienstag, 17. Dezember 2019, 11:15:54 MEZ hat David Marso <[hidden email]> Folgendes geschrieben:


Perhaps comment each matrix line with your intention. Would be happy to review after that.

=====================
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
===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Macro Syntax Problem

bdates
Thanks, Mario,  but that's just a typo. The macro works fine until the syntax that splits the macro, starting with '!do !i=1 !to !unquote(!max).' I'll take David's advice and comment each line.

Brian

From: SPSSX(r) Discussion <[hidden email]> on behalf of Mario Giesel <[hidden email]>
Sent: Tuesday, December 17, 2019 5:25 AM
To: [hidden email] <[hidden email]>
Subject: Re: Matrix Macro Syntax Problem
 
Maybe you start with calling the correct macro name:
"seo" or "sep"?

Mario Giesel
Munich, Germany


Am Dienstag, 17. Dezember 2019, 11:15:54 MEZ hat David Marso <[hidden email]> Folgendes geschrieben:


Perhaps comment each matrix line with your intention. Would be happy to review after that.

=====================
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
===================== 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
===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Macro Syntax Problem

PRogman
In reply to this post by bdates
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: http://spssx-discussion.1045642.n5.nabble.com/

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Macro Syntax Problem

bdates
Perfect! Thank-you so much.

Brian

From: SPSSX(r) Discussion <[hidden email]> on behalf of PRogman <[hidden email]>
Sent: Thursday, December 19, 2019 3:43 PM
To: [hidden email] <[hidden email]>
Subject: Re: Matrix Macro Syntax Problem
 
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&amp;data=02%7C01%7Cbdates%40SWSOL.ORG%7C7e6094053143483e096b08d784c1b953%7Cecdd61640dbd4227b0986de8e52525ca%7C0%7C0%7C637123839899062043&amp;sdata=qFk3HYxBLFIp9PSfdUEe0I9VLX7WbMFlbxpxydTJANw%3D&amp;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
===================== 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