Macro to loop over files

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

Macro to loop over files

William Shakespeare
I need to use the matrix-endmatrix command to perform matrix operations on
150 files.  Not sure if I have Python with my implementation of SPSS 19,
so I think a small macro will be my best bet.  Some questions I have not
been able to resolve:

1) I need to subscript my filenames with i, but I'm not sure I have the
correct syntax.
2) Not sure how to have two files open at one time in order to carry out
the product of x inverse and y.
3) Do I have the save coded correctly to save the value of t1 to a file?
4) If I save this file to a .sps file, can I include it to run the
commands?

Sorry if this is stupid.  I can do this in about 10 minutes in SAS, but I
don't have PROC IML.  Spent most of my day trying to find something that
would tell me how to do this.

compute1 t=0.

!do !i=1 !to 150.
matrix.

get x1_!i
 /file="C:\data\".
 /variables=col1 col2 col3 col4 col5 col6 col6 col7.
 /names=varnames.

save x1_!i_inv=inv(x1_i).

save x_inv_y1_!i=x1_!i_inv*y1_!i.

save trace1_!i=trace(x_inv_y1_!i).

t1=t1+trace1_!i.

SAVE OUTFILE='c:\data\t1' .
EXECUTE.

endmatrix.
!doend.

=====================
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: Macro to loop over files

David Marso
Administrator
MACRO is
DEFINE !ENDDEFINE
within that to iterate is !DO !DOEND
1) look at !CONCAT
2) MATRIX can open as many files as needed
GET matrixname /FILE filereference/VARIABLES blah blah2 blaaaaaaaaaaahhhhh........
GET another/blah blah blah.......
3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX language
documentation before asking questions about it. Same with Macro!!!!!
Not sure what you are doing with those save commands, perhaps you mean COMPUTE?
COMPUTE blah=TRACE(INV(X)*Y)...
4. YES.
BTW: EXECUTE is *NOT* a valid MATRIX command.

William Shakespeare wrote
I need to use the matrix-endmatrix command to perform matrix operations on
150 files.  Not sure if I have Python with my implementation of SPSS 19,
so I think a small macro will be my best bet.  Some questions I have not
been able to resolve:

1) I need to subscript my filenames with i, but I'm not sure I have the
correct syntax.
2) Not sure how to have two files open at one time in order to carry out
the product of x inverse and y.
3) Do I have the save coded correctly to save the value of t1 to a file?
4) If I save this file to a .sps file, can I include it to run the
commands?

Sorry if this is stupid.  I can do this in about 10 minutes in SAS, but I
don't have PROC IML.  Spent most of my day trying to find something that
would tell me how to do this.

compute1 t=0.

!do !i=1 !to 150.
matrix.

get x1_!i
 /file="C:\data\".
 /variables=col1 col2 col3 col4 col5 col6 col6 col7.
 /names=varnames.

save x1_!i_inv=inv(x1_i).

save x_inv_y1_!i=x1_!i_inv*y1_!i.

save trace1_!i=trace(x_inv_y1_!i).

t1=t1+trace1_!i.

SAVE OUTFILE='c:\data\t1' .
EXECUTE.

endmatrix.
!doend.

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Macro to loop over files

Art Kendall
In reply to this post by William Shakespeare
Just curious.  Did you check whether what you want to do with MATRIX is not already some existing procedure in SPSS?

Perhaps if you describe what you are doing there may be other ways to reach your goal.
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Macro to loop over files

William Shakespeare
In reply to this post by William Shakespeare
Thanks for your feedback.  As I said, I have no documentation, so I have
to rely on what I can piece together from what I can find on the
internet.  I saw the save syntax somewhere on the internet as an
alternative to compute.  I'd like to save my intermediate files for now so
I can make spot checks of the computations.  Here's what I've been able to
put together from what I've read, and the suggestions here.  I don't think
I need a period after on the !do line.  Not sure I need the get statement
after the matrix product or if I need to add a get statement after taking
the trace.  Feedback appreciated.

define blocktrace
compute1 t1=0.

 !do !i=1 !to 150
 matrix.

 get !concat(x1_,!i).
  /file="C:\data\".
  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
  /names=varnames.

 compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
 save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).

 get !concat(invx1_,!i).
   /file="C:\data\".
   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
   /names=varnames.
 get !concat(y1_,!i).
   /file="C:\data\".
   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
   /names=varnames.

 compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
 save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).

 get !concat("x_inv_y1_",!i).
   /file="C:\data\".
   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
   /names=varnames.

 compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
 save outfile !quote(!concat("c:\data\","trace1_",!i)).

 compute t1=t1+trace(!concat(1_,!i)).

 SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).


 endmatrix.
 !doend.
!enddefine.

On Sat, 7 Jun 2014 06:18:44 -0700, David Marso <[hidden email]>
wrote:

>MACRO is
>DEFINE !ENDDEFINE
>within that to iterate is !DO !DOEND
>1) look at !CONCAT
>2) MATRIX can open as many files as needed
>GET matrixname /FILE filereference/VARIABLES blah blah2
>blaaaaaaaaaaahhhhh........
>GET another/blah blah blah.......
>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>language
>documentation before asking questions about it. Same with Macro!!!!!
>Not sure what you are doing with those save commands, perhaps you mean
>COMPUTE?
>COMPUTE blah=TRACE(INV(X)*Y)...
>4. YES.
>BTW: EXECUTE is *NOT* a valid MATRIX command.
>
>
>William Shakespeare wrote
>> I need to use the matrix-endmatrix command to perform matrix operations
on

>> 150 files.  Not sure if I have Python with my implementation of SPSS 19,
>> so I think a small macro will be my best bet.  Some questions I have not
>> been able to resolve:
>>
>> 1) I need to subscript my filenames with i, but I'm not sure I have the
>> correct syntax.
>> 2) Not sure how to have two files open at one time in order to carry out
>> the product of x inverse and y.
>> 3) Do I have the save coded correctly to save the value of t1 to a file?
>> 4) If I save this file to a .sps file, can I include it to run the
>> commands?
>>
>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS, but
I

>> don't have PROC IML.  Spent most of my day trying to find something that
>> would tell me how to do this.
>>
>> compute1 t=0.
>>
>> !do !i=1 !to 150.
>> matrix.
>>
>> get x1_!i
>>  /file="C:\data\".
>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>  /names=varnames.
>>
>> save x1_!i_inv=inv(x1_i).
>>
>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>
>> save trace1_!i=trace(x_inv_y1_!i).
>>
>> t1=t1+trace1_!i.
>>
>> SAVE OUTFILE='c:\data\t1' .
>> EXECUTE.
>>
>> endmatrix.
>> !doend.
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context: http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726389.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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: Macro to loop over files

Rick Oliver-3
It would be difficult to have a version of the product with "no documentation". You don't a version of the product with a user interface with a Help menu?

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        William Shakespeare <[hidden email]>
To:        [hidden email],
Date:        06/12/2014 01:07 PM
Subject:        Re: Macro to loop over files
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Thanks for your feedback.  As I said, I have no documentation, so I have
to rely on what I can piece together from what I can find on the
internet.  I saw the save syntax somewhere on the internet as an
alternative to compute.  I'd like to save my intermediate files for now so
I can make spot checks of the computations.  Here's what I've been able to
put together from what I've read, and the suggestions here.  I don't think
I need a period after on the !do line.  Not sure I need the get statement
after the matrix product or if I need to add a get statement after taking
the trace.  Feedback appreciated.

define blocktrace
compute1 t1=0.

!do !i=1 !to 150
matrix.

get !concat(x1_,!i).
 /file="C:\data\".
 /variables=col1 col2 col3 col4 col5 col6 col6 col7.
 /names=varnames.

compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).

get !concat(invx1_,!i).
  /file="C:\data\".
  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
  /names=varnames.
get !concat(y1_,!i).
  /file="C:\data\".
  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
  /names=varnames.

compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).

get !concat("x_inv_y1_",!i).
  /file="C:\data\".
  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
  /names=varnames.

compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
save outfile !quote(!concat("c:\data\","trace1_",!i)).

compute t1=t1+trace(!concat(1_,!i)).

SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).


endmatrix.
!doend.
!enddefine.

On Sat, 7 Jun 2014 06:18:44 -0700, David Marso <[hidden email]>
wrote:

>MACRO is
>DEFINE !ENDDEFINE
>within that to iterate is !DO !DOEND
>1) look at !CONCAT
>2) MATRIX can open as many files as needed
>GET matrixname /FILE filereference/VARIABLES blah blah2
>blaaaaaaaaaaahhhhh........
>GET another/blah blah blah.......
>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>language
>documentation before asking questions about it. Same with Macro!!!!!
>Not sure what you are doing with those save commands, perhaps you mean
>COMPUTE?
>COMPUTE blah=TRACE(INV(X)*Y)...
>4. YES.
>BTW: EXECUTE is *NOT* a valid MATRIX command.
>
>
>William Shakespeare wrote
>> I need to use the matrix-endmatrix command to perform matrix operations
on
>> 150 files.  Not sure if I have Python with my implementation of SPSS 19,
>> so I think a small macro will be my best bet.  Some questions I have not
>> been able to resolve:
>>
>> 1) I need to subscript my filenames with i, but I'm not sure I have the
>> correct syntax.
>> 2) Not sure how to have two files open at one time in order to carry out
>> the product of x inverse and y.
>> 3) Do I have the save coded correctly to save the value of t1 to a file?
>> 4) If I save this file to a .sps file, can I include it to run the
>> commands?
>>
>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS, but
I
>> don't have PROC IML.  Spent most of my day trying to find something that
>> would tell me how to do this.
>>
>> compute1 t=0.
>>
>> !do !i=1 !to 150.
>> matrix.
>>
>> get x1_!i
>>  /file="C:\data\".
>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>  /names=varnames.
>>
>> save x1_!i_inv=inv(x1_i).
>>
>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>
>> save trace1_!i=trace(x_inv_y1_!i).
>>
>> t1=t1+trace1_!i.
>>
>> SAVE OUTFILE='c:\data\t1' .
>> EXECUTE.
>>
>> endmatrix.
>> !doend.
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context:
http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726389.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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: Macro to loop over files

David Marso
Administrator
In reply to this post by William Shakespeare
You would do yourself and us a big favor if you describe WHAT you are trying to calculate rather than expect anyone to correct your MATRIX program/macro.
I'm not going to tell you what you need to change because (as written) it is a complete mess.
As Rick stated, hard to believe you DON'T have documentation.
If you click on the icon with the question mark it will load the syntax reference.
It is also available online and downloadable PDF on the IBM site.
A few things to note! (IN PARTICULAR the correct syntax for SAVE in MATRIX).
AFAICT, The following is likely to be exactly  what you are attempting to achieve (after appropriate edits).
Note also the defensive programming you need to apply when using MATRIX.
It is for the most part an unforgiving B!#<# and any computational error will result in a FUBAR state of affairs!
You really don't want to save 300 files!!! use PRINT.
HTH...
 
DEFINE blah().
MATRIX.
COMPUTE t1=0.
!DO !I = 1 !TO ??.
GET x /FILE=!QUOTE(!CONCAT(<filepath>,x,!I)).
GET y/FILE=!QUOTE(!CONCAT(<filepath>,y,!I)).
DO IF NCOL(X)=NROW(Y).
+  DO IF DET(X) GT 0.

/* COMPUTE Xinv=GINV(x).
/* SAVE XINV / OUTFILE !QUOTE(!CONCAT(<filepath>,xInv,!I)).
/* PRINT !I .
/* PRINT XInv.
/* ................ */.

+    COMPUTE T1=T1+TRACE(GINV(X)*Y).
+  ELSE.
+    PRINT !I / TITLE "Determinant is 0".
+  END IF.
ELSE.
PRINT !i /TITLE "Row and column dimension mismatch".
END IF.
!DOEND.
SAVE T1 / OUTFILE somefilename.
!ENDDEFINE.

William Shakespeare wrote
Thanks for your feedback.  As I said, I have no documentation, so I have
to rely on what I can piece together from what I can find on the
internet.  I saw the save syntax somewhere on the internet as an
alternative to compute.  I'd like to save my intermediate files for now so
I can make spot checks of the computations.  Here's what I've been able to
put together from what I've read, and the suggestions here.  I don't think
I need a period after on the !do line.  Not sure I need the get statement
after the matrix product or if I need to add a get statement after taking
the trace.  Feedback appreciated.

define blocktrace
compute1 t1=0.

 !do !i=1 !to 150
 matrix.

 get !concat(x1_,!i).
  /file="C:\data\".
  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
  /names=varnames.

 compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
 save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).

 get !concat(invx1_,!i).
   /file="C:\data\".
   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
   /names=varnames.
 get !concat(y1_,!i).
   /file="C:\data\".
   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
   /names=varnames.

 compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
 save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).

 get !concat("x_inv_y1_",!i).
   /file="C:\data\".
   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
   /names=varnames.

 compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
 save outfile !quote(!concat("c:\data\","trace1_",!i)).

 compute t1=t1+trace(!concat(1_,!i)).

 SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).


 endmatrix.
 !doend.
!enddefine.

On Sat, 7 Jun 2014 06:18:44 -0700, David Marso <[hidden email]>
wrote:

>MACRO is
>DEFINE !ENDDEFINE
>within that to iterate is !DO !DOEND
>1) look at !CONCAT
>2) MATRIX can open as many files as needed
>GET matrixname /FILE filereference/VARIABLES blah blah2
>blaaaaaaaaaaahhhhh........
>GET another/blah blah blah.......
>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>language
>documentation before asking questions about it. Same with Macro!!!!!
>Not sure what you are doing with those save commands, perhaps you mean
>COMPUTE?
>COMPUTE blah=TRACE(INV(X)*Y)...
>4. YES.
>BTW: EXECUTE is *NOT* a valid MATRIX command.
>
>
>William Shakespeare wrote
>> I need to use the matrix-endmatrix command to perform matrix operations
on
>> 150 files.  Not sure if I have Python with my implementation of SPSS 19,
>> so I think a small macro will be my best bet.  Some questions I have not
>> been able to resolve:
>>
>> 1) I need to subscript my filenames with i, but I'm not sure I have the
>> correct syntax.
>> 2) Not sure how to have two files open at one time in order to carry out
>> the product of x inverse and y.
>> 3) Do I have the save coded correctly to save the value of t1 to a file?
>> 4) If I save this file to a .sps file, can I include it to run the
>> commands?
>>
>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS, but
I
>> don't have PROC IML.  Spent most of my day trying to find something that
>> would tell me how to do this.
>>
>> compute1 t=0.
>>
>> !do !i=1 !to 150.
>> matrix.
>>
>> get x1_!i
>>  /file="C:\data\".
>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>  /names=varnames.
>>
>> save x1_!i_inv=inv(x1_i).
>>
>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>
>> save trace1_!i=trace(x_inv_y1_!i).
>>
>> t1=t1+trace1_!i.
>>
>> SAVE OUTFILE='c:\data\t1' .
>> EXECUTE.
>>
>> endmatrix.
>> !doend.
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context: http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726389.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Macro to loop over files

William Shakespeare
In reply to this post by William Shakespeare
Seems that way doesn't it?  I think we had a firewall issue here since I
can't get the help to open when I click on it.  Perhaps my IT department
can help.

On Thu, 12 Jun 2014 13:12:38 -0500, Rick Oliver <[hidden email]> wrote:

>It would be difficult to have a version of the product with "no
>documentation". You don't a version of the product with a user interface
>with a Help menu?
>
>Rick Oliver
>Senior Information Developer
>IBM Business Analytics (SPSS)
>E-mail: [hidden email]
>
>
>
>From:   William Shakespeare <[hidden email]>
>To:     [hidden email],
>Date:   06/12/2014 01:07 PM
>Subject:        Re: Macro to loop over files
>Sent by:        "SPSSX(r) Discussion" <[hidden email]>
>
>
>
>Thanks for your feedback.  As I said, I have no documentation, so I have
>to rely on what I can piece together from what I can find on the
>internet.  I saw the save syntax somewhere on the internet as an
>alternative to compute.  I'd like to save my intermediate files for now so
>I can make spot checks of the computations.  Here's what I've been able to
>put together from what I've read, and the suggestions here.  I don't think
>I need a period after on the !do line.  Not sure I need the get statement
>after the matrix product or if I need to add a get statement after taking
>the trace.  Feedback appreciated.
>
>define blocktrace
>compute1 t1=0.
>
> !do !i=1 !to 150
> matrix.
>
> get !concat(x1_,!i).
>  /file="C:\data\".
>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>  /names=varnames.
>
> compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
> save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).
>
> get !concat(invx1_,!i).
>   /file="C:\data\".
>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>   /names=varnames.
> get !concat(y1_,!i).
>   /file="C:\data\".
>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>   /names=varnames.
>
> compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
> save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).
>
> get !concat("x_inv_y1_",!i).
>   /file="C:\data\".
>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>   /names=varnames.
>
> compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
> save outfile !quote(!concat("c:\data\","trace1_",!i)).
>
> compute t1=t1+trace(!concat(1_,!i)).
>
> SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).
>
>
> endmatrix.
> !doend.
>!enddefine.
>
>On Sat, 7 Jun 2014 06:18:44 -0700, David Marso <[hidden email]>
>wrote:
>
>>MACRO is
>>DEFINE !ENDDEFINE
>>within that to iterate is !DO !DOEND
>>1) look at !CONCAT
>>2) MATRIX can open as many files as needed
>>GET matrixname /FILE filereference/VARIABLES blah blah2
>>blaaaaaaaaaaahhhhh........
>>GET another/blah blah blah.......
>>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>>language
>>documentation before asking questions about it. Same with Macro!!!!!
>>Not sure what you are doing with those save commands, perhaps you mean
>>COMPUTE?
>>COMPUTE blah=TRACE(INV(X)*Y)...
>>4. YES.
>>BTW: EXECUTE is *NOT* a valid MATRIX command.
>>
>>
>>William Shakespeare wrote
>>> I need to use the matrix-endmatrix command to perform matrix operations
>on
>>> 150 files.  Not sure if I have Python with my implementation of SPSS
>19,
>>> so I think a small macro will be my best bet.  Some questions I have
>not
>>> been able to resolve:
>>>
>>> 1) I need to subscript my filenames with i, but I'm not sure I have the
>>> correct syntax.
>>> 2) Not sure how to have two files open at one time in order to carry
>out
>>> the product of x inverse and y.
>>> 3) Do I have the save coded correctly to save the value of t1 to a
>file?
>>> 4) If I save this file to a .sps file, can I include it to run the
>>> commands?
>>>
>>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS, but
>I
>>> don't have PROC IML.  Spent most of my day trying to find something
>that
>>> would tell me how to do this.
>>>
>>> compute1 t=0.
>>>
>>> !do !i=1 !to 150.
>>> matrix.
>>>
>>> get x1_!i
>>>  /file="C:\data\".
>>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>  /names=varnames.
>>>
>>> save x1_!i_inv=inv(x1_i).
>>>
>>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>>
>>> save trace1_!i=trace(x_inv_y1_!i).
>>>
>>> t1=t1+trace1_!i.
>>>
>>> SAVE OUTFILE='c:\data\t1' .
>>> EXECUTE.
>>>
>>> endmatrix.
>>> !doend.
>>>
>>> =====================
>>> 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
>>
>>
>>
>>
>>
>>-----
>>Please reply to the list and not to my personal email.
>>Those desiring my consulting or training services please feel free to
>email me.
>>---
>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>porcos ne forte conculcent eas pedibus suis."
>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>in abyssum?"
>>--
>>View this message in context: http://spssx-
>discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>tp5726385p5726389.html
>>Sent from the SPSSX Discussion mailing list archive at 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
>
>=====================
>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: Macro to loop over files

William Shakespeare
In reply to this post by William Shakespeare
Thanks for the feedback.  My algorithm is very simple:
For all files (do 150 times):
1) Invert X and save.
2) Multiply X and Y and save.
3) Find the trace of X*Y and save.
Final step (I have this accumulating over the iterations of the loop):
4) Add up all of the computed traces in step 3 and save.

As I said, I prefer to save the computations in 1-3 to make to checks.
It's really more than 300 files since each step 1-3 generates 150 files.
No worries, however, as drive space is not an issue.  I have other sets of
data to which I'll apply this algorithm.  Once I'm satisfied that things
perform as expected on the first set, then I'll resort to the more compact
code shown below.  Don't even need to print at that point.  Definitely a
good point about trapping computational errors.

Documentation on the IBM website seems to start with v. 20.  I have v. 19,
so I don't know how much commonality there is between the two.  Perhaps
there's a firewall issue that's preventing me from directly accessing the
docs from SPSS.  Will talk to my IT department.

On Thu, 12 Jun 2014 12:32:35 -0700, David Marso <[hidden email]>
wrote:

>You would do yourself and us a big favor if you describe WHAT you are
trying
>to calculate rather than expect anyone to correct your MATRIX
program/macro.
>I'm not going to tell you what you need to change because (as written) it
is
>a complete mess.
>As Rick stated, hard to believe you DON'T have documentation.
>If you click on the icon with the question mark it will load the syntax
>reference.
>It is also available online and downloadable PDF on the IBM site.
>A few things to note! (IN PARTICULAR the correct syntax for SAVE in
MATRIX).

>AFAICT, The following is likely to be exactly  what you are attempting to
>achieve (after appropriate edits).
>Note also the defensive programming you need to apply when using MATRIX.
>It is for the most part an unforgiving B!#<# and any computational error
>will result in a FUBAR state of affairs!
>You really don't want to save 300 files!!! use PRINT.
>HTH...
>
>DEFINE blah().
>MATRIX.
>COMPUTE t1=0.
>!DO !I = 1 !TO ??.
>GET x /FILE=!QUOTE(!CONCAT(<filepath>,x,!I)).
>GET y/FILE=!QUOTE(!CONCAT(<filepath>,y,!I)).
>DO IF NCOL(X)=NROW(Y).
>+  DO IF DET(X) GT 0.
>
>/* COMPUTE Xinv=GINV(x).
>/* SAVE XINV / OUTFILE !QUOTE(!CONCAT(<filepath>,xInv,!I)).
>/* PRINT !I .
>/* PRINT XInv.
>/* ................ */.
>
>+    COMPUTE T1=T1+TRACE(GINV(X)*Y).
>+  ELSE.
>+    PRINT !I / TITLE "Determinant is 0".
>+  END IF.
>ELSE.
>PRINT !i /TITLE "Row and column dimension mismatch".
>END IF.
>!DOEND.
>SAVE T1 / OUTFILE somefilename.
>!ENDDEFINE.
>
>
>William Shakespeare wrote
>> Thanks for your feedback.  As I said, I have no documentation, so I have
>> to rely on what I can piece together from what I can find on the
>> internet.  I saw the save syntax somewhere on the internet as an
>> alternative to compute.  I'd like to save my intermediate files for now
so
>> I can make spot checks of the computations.  Here's what I've been able
to
>> put together from what I've read, and the suggestions here.  I don't
think
>> I need a period after on the !do line.  Not sure I need the get
statement
>> after the matrix product or if I need to add a get statement after
taking

>> the trace.  Feedback appreciated.
>>
>> define blocktrace
>> compute1 t1=0.
>>
>>  !do !i=1 !to 150
>>  matrix.
>>
>>  get !concat(x1_,!i).
>>   /file="C:\data\".
>>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>   /names=varnames.
>>
>>  compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
>>  save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).
>>
>>  get !concat(invx1_,!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>  get !concat(y1_,!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>
>>  compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
>>  save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).
>>
>>  get !concat("x_inv_y1_",!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>
>>  compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
>>  save outfile !quote(!concat("c:\data\","trace1_",!i)).
>>
>>  compute t1=t1+trace(!concat(1_,!i)).
>>
>>  SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).
>>
>>
>>  endmatrix.
>>  !doend.
>> !enddefine.
>>
>> On Sat, 7 Jun 2014 06:18:44 -0700, David Marso &lt;
>
>> david.marso@
>
>> &gt;
>> wrote:
>>
>>>MACRO is
>>>DEFINE !ENDDEFINE
>>>within that to iterate is !DO !DOEND
>>>1) look at !CONCAT
>>>2) MATRIX can open as many files as needed
>>>GET matrixname /FILE filereference/VARIABLES blah blah2
>>>blaaaaaaaaaaahhhhh........
>>>GET another/blah blah blah.......
>>>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>>>language
>>>documentation before asking questions about it. Same with Macro!!!!!
>>>Not sure what you are doing with those save commands, perhaps you mean
>>>COMPUTE?
>>>COMPUTE blah=TRACE(INV(X)*Y)...
>>>4. YES.
>>>BTW: EXECUTE is *NOT* a valid MATRIX command.
>>>
>>>
>>>William Shakespeare wrote
>>>> I need to use the matrix-endmatrix command to perform matrix
operations
>> on
>>>> 150 files.  Not sure if I have Python with my implementation of SPSS
19,
>>>> so I think a small macro will be my best bet.  Some questions I have
not
>>>> been able to resolve:
>>>>
>>>> 1) I need to subscript my filenames with i, but I'm not sure I have
the
>>>> correct syntax.
>>>> 2) Not sure how to have two files open at one time in order to carry
out
>>>> the product of x inverse and y.
>>>> 3) Do I have the save coded correctly to save the value of t1 to a
file?
>>>> 4) If I save this file to a .sps file, can I include it to run the
>>>> commands?
>>>>
>>>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS,
but
>> I
>>>> don't have PROC IML.  Spent most of my day trying to find something
that

>>>> would tell me how to do this.
>>>>
>>>> compute1 t=0.
>>>>
>>>> !do !i=1 !to 150.
>>>> matrix.
>>>>
>>>> get x1_!i
>>>>  /file="C:\data\".
>>>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>  /names=varnames.
>>>>
>>>> save x1_!i_inv=inv(x1_i).
>>>>
>>>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>>>
>>>> save trace1_!i=trace(x_inv_y1_!i).
>>>>
>>>> t1=t1+trace1_!i.
>>>>
>>>> SAVE OUTFILE='c:\data\t1' .
>>>> EXECUTE.
>>>>
>>>> endmatrix.
>>>> !doend.
>>>>
>>>> =====================
>>>> 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
>>>
>>>
>>>
>>>
>>>
>>>-----
>>>Please reply to the list and not to my personal email.
>>>Those desiring my consulting or training services please feel free to
>> email me.
>>>---
>>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos ne forte conculcent eas pedibus suis."
>>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in abyssum?"
>>>--
>>>View this message in context: http://spssx-
>> discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>> tp5726385p5726389.html
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>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
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context: http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726451.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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: Macro to loop over files

Rick Oliver-3
In reply to this post by William Shakespeare
Help for commands (including the macro facility) is provided in two forms: as part of the overall help system (Topics on the Help menu) and in PDF form (Command Syntax Reference on the Help menu). If the Help menu doesn't work at all (and I can't imagine any firewall issues that would cause that), you can find the PDF in the installation directory: syntaxreference.pdf.

Note that the help system is installed locally. It is not trying to access some external website.

If all else fails, although you can't access the help for Statistic 19 on the IBM website, you can access the entire help system for Statistics 20:

www.ibm.com/support/knowledgecenter/SSLVMB_20.0.0/com.ibm.spss.statistics_20.kc.doc/pv_welcome.html

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        William Shakespeare <[hidden email]>
To:        [hidden email],
Date:        06/13/2014 08:39 AM
Subject:        Re: Macro to loop over files
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Seems that way doesn't it?  I think we had a firewall issue here since I
can't get the help to open when I click on it.  Perhaps my IT department
can help.

On Thu, 12 Jun 2014 13:12:38 -0500, Rick Oliver <[hidden email]> wrote:

>It would be difficult to have a version of the product with "no
>documentation". You don't a version of the product with a user interface
>with a Help menu?
>
>Rick Oliver
>Senior Information Developer
>IBM Business Analytics (SPSS)
>E-mail: [hidden email]
>
>
>
>From:   William Shakespeare <[hidden email]>
>To:     [hidden email],
>Date:   06/12/2014 01:07 PM
>Subject:        Re: Macro to loop over files
>Sent by:        "SPSSX(r) Discussion" <[hidden email]>
>
>
>
>Thanks for your feedback.  As I said, I have no documentation, so I have
>to rely on what I can piece together from what I can find on the
>internet.  I saw the save syntax somewhere on the internet as an
>alternative to compute.  I'd like to save my intermediate files for now so
>I can make spot checks of the computations.  Here's what I've been able to
>put together from what I've read, and the suggestions here.  I don't think
>I need a period after on the !do line.  Not sure I need the get statement
>after the matrix product or if I need to add a get statement after taking
>the trace.  Feedback appreciated.
>
>define blocktrace
>compute1 t1=0.
>
> !do !i=1 !to 150
> matrix.
>
> get !concat(x1_,!i).
>  /file="C:\data\".
>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>  /names=varnames.
>
> compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
> save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).
>
> get !concat(invx1_,!i).
>   /file="C:\data\".
>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>   /names=varnames.
> get !concat(y1_,!i).
>   /file="C:\data\".
>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>   /names=varnames.
>
> compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
> save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).
>
> get !concat("x_inv_y1_",!i).
>   /file="C:\data\".
>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>   /names=varnames.
>
> compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
> save outfile !quote(!concat("c:\data\","trace1_",!i)).
>
> compute t1=t1+trace(!concat(1_,!i)).
>
> SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).
>
>
> endmatrix.
> !doend.
>!enddefine.
>
>On Sat, 7 Jun 2014 06:18:44 -0700, David Marso <[hidden email]>
>wrote:
>
>>MACRO is
>>DEFINE !ENDDEFINE
>>within that to iterate is !DO !DOEND
>>1) look at !CONCAT
>>2) MATRIX can open as many files as needed
>>GET matrixname /FILE filereference/VARIABLES blah blah2
>>blaaaaaaaaaaahhhhh........
>>GET another/blah blah blah.......
>>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>>language
>>documentation before asking questions about it. Same with Macro!!!!!
>>Not sure what you are doing with those save commands, perhaps you mean
>>COMPUTE?
>>COMPUTE blah=TRACE(INV(X)*Y)...
>>4. YES.
>>BTW: EXECUTE is *NOT* a valid MATRIX command.
>>
>>
>>William Shakespeare wrote
>>> I need to use the matrix-endmatrix command to perform matrix operations
>on
>>> 150 files.  Not sure if I have Python with my implementation of SPSS
>19,
>>> so I think a small macro will be my best bet.  Some questions I have
>not
>>> been able to resolve:
>>>
>>> 1) I need to subscript my filenames with i, but I'm not sure I have the
>>> correct syntax.
>>> 2) Not sure how to have two files open at one time in order to carry
>out
>>> the product of x inverse and y.
>>> 3) Do I have the save coded correctly to save the value of t1 to a
>file?
>>> 4) If I save this file to a .sps file, can I include it to run the
>>> commands?
>>>
>>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS, but
>I
>>> don't have PROC IML.  Spent most of my day trying to find something
>that
>>> would tell me how to do this.
>>>
>>> compute1 t=0.
>>>
>>> !do !i=1 !to 150.
>>> matrix.
>>>
>>> get x1_!i
>>>  /file="C:\data\".
>>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>  /names=varnames.
>>>
>>> save x1_!i_inv=inv(x1_i).
>>>
>>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>>
>>> save trace1_!i=trace(x_inv_y1_!i).
>>>
>>> t1=t1+trace1_!i.
>>>
>>> SAVE OUTFILE='c:\data\t1' .
>>> EXECUTE.
>>>
>>> endmatrix.
>>> !doend.
>>>
>>> =====================
>>> 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
>>
>>
>>
>>
>>
>>-----
>>Please reply to the list and not to my personal email.
>>Those desiring my consulting or training services please feel free to
>email me.
>>---
>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>porcos ne forte conculcent eas pedibus suis."
>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>in abyssum?"
>>--
>>View this message in context:
http://spssx-
>discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>tp5726385p5726389.html
>>Sent from the SPSSX Discussion mailing list archive at 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
>
>=====================
>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: Macro to loop over files

William Shakespeare
In reply to this post by William Shakespeare
Is there a missing endmatrix in this code block?  I think it hshould be
before the !endefine, corrrect?

On Thu, 12 Jun 2014 12:32:35 -0700, David Marso <[hidden email]>
wrote:

>You would do yourself and us a big favor if you describe WHAT you are
trying
>to calculate rather than expect anyone to correct your MATRIX
program/macro.
>I'm not going to tell you what you need to change because (as written) it
is
>a complete mess.
>As Rick stated, hard to believe you DON'T have documentation.
>If you click on the icon with the question mark it will load the syntax
>reference.
>It is also available online and downloadable PDF on the IBM site.
>A few things to note! (IN PARTICULAR the correct syntax for SAVE in
MATRIX).

>AFAICT, The following is likely to be exactly  what you are attempting to
>achieve (after appropriate edits).
>Note also the defensive programming you need to apply when using MATRIX.
>It is for the most part an unforgiving B!#<# and any computational error
>will result in a FUBAR state of affairs!
>You really don't want to save 300 files!!! use PRINT.
>HTH...
>
>DEFINE blah().
>MATRIX.
>COMPUTE t1=0.
>!DO !I = 1 !TO ??.
>GET x /FILE=!QUOTE(!CONCAT(<filepath>,x,!I)).
>GET y/FILE=!QUOTE(!CONCAT(<filepath>,y,!I)).
>DO IF NCOL(X)=NROW(Y).
>+  DO IF DET(X) GT 0.
>
>/* COMPUTE Xinv=GINV(x).
>/* SAVE XINV / OUTFILE !QUOTE(!CONCAT(<filepath>,xInv,!I)).
>/* PRINT !I .
>/* PRINT XInv.
>/* ................ */.
>
>+    COMPUTE T1=T1+TRACE(GINV(X)*Y).
>+  ELSE.
>+    PRINT !I / TITLE "Determinant is 0".
>+  END IF.
>ELSE.
>PRINT !i /TITLE "Row and column dimension mismatch".
>END IF.
>!DOEND.
>SAVE T1 / OUTFILE somefilename.
>!ENDDEFINE.
>
>
>William Shakespeare wrote
>> Thanks for your feedback.  As I said, I have no documentation, so I have
>> to rely on what I can piece together from what I can find on the
>> internet.  I saw the save syntax somewhere on the internet as an
>> alternative to compute.  I'd like to save my intermediate files for now
so
>> I can make spot checks of the computations.  Here's what I've been able
to
>> put together from what I've read, and the suggestions here.  I don't
think
>> I need a period after on the !do line.  Not sure I need the get
statement
>> after the matrix product or if I need to add a get statement after
taking

>> the trace.  Feedback appreciated.
>>
>> define blocktrace
>> compute1 t1=0.
>>
>>  !do !i=1 !to 150
>>  matrix.
>>
>>  get !concat(x1_,!i).
>>   /file="C:\data\".
>>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>   /names=varnames.
>>
>>  compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
>>  save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).
>>
>>  get !concat(invx1_,!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>  get !concat(y1_,!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>
>>  compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
>>  save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).
>>
>>  get !concat("x_inv_y1_",!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>
>>  compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
>>  save outfile !quote(!concat("c:\data\","trace1_",!i)).
>>
>>  compute t1=t1+trace(!concat(1_,!i)).
>>
>>  SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).
>>
>>
>>  endmatrix.
>>  !doend.
>> !enddefine.
>>
>> On Sat, 7 Jun 2014 06:18:44 -0700, David Marso &lt;
>
>> david.marso@
>
>> &gt;
>> wrote:
>>
>>>MACRO is
>>>DEFINE !ENDDEFINE
>>>within that to iterate is !DO !DOEND
>>>1) look at !CONCAT
>>>2) MATRIX can open as many files as needed
>>>GET matrixname /FILE filereference/VARIABLES blah blah2
>>>blaaaaaaaaaaahhhhh........
>>>GET another/blah blah blah.......
>>>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>>>language
>>>documentation before asking questions about it. Same with Macro!!!!!
>>>Not sure what you are doing with those save commands, perhaps you mean
>>>COMPUTE?
>>>COMPUTE blah=TRACE(INV(X)*Y)...
>>>4. YES.
>>>BTW: EXECUTE is *NOT* a valid MATRIX command.
>>>
>>>
>>>William Shakespeare wrote
>>>> I need to use the matrix-endmatrix command to perform matrix
operations
>> on
>>>> 150 files.  Not sure if I have Python with my implementation of SPSS
19,
>>>> so I think a small macro will be my best bet.  Some questions I have
not
>>>> been able to resolve:
>>>>
>>>> 1) I need to subscript my filenames with i, but I'm not sure I have
the
>>>> correct syntax.
>>>> 2) Not sure how to have two files open at one time in order to carry
out
>>>> the product of x inverse and y.
>>>> 3) Do I have the save coded correctly to save the value of t1 to a
file?
>>>> 4) If I save this file to a .sps file, can I include it to run the
>>>> commands?
>>>>
>>>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS,
but
>> I
>>>> don't have PROC IML.  Spent most of my day trying to find something
that

>>>> would tell me how to do this.
>>>>
>>>> compute1 t=0.
>>>>
>>>> !do !i=1 !to 150.
>>>> matrix.
>>>>
>>>> get x1_!i
>>>>  /file="C:\data\".
>>>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>  /names=varnames.
>>>>
>>>> save x1_!i_inv=inv(x1_i).
>>>>
>>>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>>>
>>>> save trace1_!i=trace(x_inv_y1_!i).
>>>>
>>>> t1=t1+trace1_!i.
>>>>
>>>> SAVE OUTFILE='c:\data\t1' .
>>>> EXECUTE.
>>>>
>>>> endmatrix.
>>>> !doend.
>>>>
>>>> =====================
>>>> 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
>>>
>>>
>>>
>>>
>>>
>>>-----
>>>Please reply to the list and not to my personal email.
>>>Those desiring my consulting or training services please feel free to
>> email me.
>>>---
>>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos ne forte conculcent eas pedibus suis."
>>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in abyssum?"
>>>--
>>>View this message in context: http://spssx-
>> discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>> tp5726385p5726389.html
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>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
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context: http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726451.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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: Macro to loop over files

David Marso
Administrator

True that.
END MATRIX right before the !ENDDEFINE.

William Shakespeare wrote
Is there a missing endmatrix in this code block?  I think it hshould be
before the !endefine, corrrect?

On Thu, 12 Jun 2014 12:32:35 -0700, David Marso <[hidden email]>
wrote:

>You would do yourself and us a big favor if you describe WHAT you are
trying
>to calculate rather than expect anyone to correct your MATRIX
program/macro.
>I'm not going to tell you what you need to change because (as written) it
is
>a complete mess.
>As Rick stated, hard to believe you DON'T have documentation.
>If you click on the icon with the question mark it will load the syntax
>reference.
>It is also available online and downloadable PDF on the IBM site.
>A few things to note! (IN PARTICULAR the correct syntax for SAVE in
MATRIX).
>AFAICT, The following is likely to be exactly  what you are attempting to
>achieve (after appropriate edits).
>Note also the defensive programming you need to apply when using MATRIX.
>It is for the most part an unforgiving B!#<# and any computational error
>will result in a FUBAR state of affairs!
>You really don't want to save 300 files!!! use PRINT.
>HTH...
>
>DEFINE blah().
>MATRIX.
>COMPUTE t1=0.
>!DO !I = 1 !TO ??.
>GET x /FILE=!QUOTE(!CONCAT(<filepath>,x,!I)).
>GET y/FILE=!QUOTE(!CONCAT(<filepath>,y,!I)).
>DO IF NCOL(X)=NROW(Y).
>+  DO IF DET(X) GT 0.
>
>/* COMPUTE Xinv=GINV(x).
>/* SAVE XINV / OUTFILE !QUOTE(!CONCAT(<filepath>,xInv,!I)).
>/* PRINT !I .
>/* PRINT XInv.
>/* ................ */.
>
>+    COMPUTE T1=T1+TRACE(GINV(X)*Y).
>+  ELSE.
>+    PRINT !I / TITLE "Determinant is 0".
>+  END IF.
>ELSE.
>PRINT !i /TITLE "Row and column dimension mismatch".
>END IF.
>!DOEND.
>SAVE T1 / OUTFILE somefilename.
>!ENDDEFINE.
>
>
>William Shakespeare wrote
>> Thanks for your feedback.  As I said, I have no documentation, so I have
>> to rely on what I can piece together from what I can find on the
>> internet.  I saw the save syntax somewhere on the internet as an
>> alternative to compute.  I'd like to save my intermediate files for now
so
>> I can make spot checks of the computations.  Here's what I've been able
to
>> put together from what I've read, and the suggestions here.  I don't
think
>> I need a period after on the !do line.  Not sure I need the get
statement
>> after the matrix product or if I need to add a get statement after
taking
>> the trace.  Feedback appreciated.
>>
>> define blocktrace
>> compute1 t1=0.
>>
>>  !do !i=1 !to 150
>>  matrix.
>>
>>  get !concat(x1_,!i).
>>   /file="C:\data\".
>>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>   /names=varnames.
>>
>>  compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
>>  save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).
>>
>>  get !concat(invx1_,!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>  get !concat(y1_,!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>
>>  compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
>>  save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).
>>
>>  get !concat("x_inv_y1_",!i).
>>    /file="C:\data\".
>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>    /names=varnames.
>>
>>  compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
>>  save outfile !quote(!concat("c:\data\","trace1_",!i)).
>>
>>  compute t1=t1+trace(!concat(1_,!i)).
>>
>>  SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).
>>
>>
>>  endmatrix.
>>  !doend.
>> !enddefine.
>>
>> On Sat, 7 Jun 2014 06:18:44 -0700, David Marso <
>
>> david.marso@
>
>> >
>> wrote:
>>
>>>MACRO is
>>>DEFINE !ENDDEFINE
>>>within that to iterate is !DO !DOEND
>>>1) look at !CONCAT
>>>2) MATRIX can open as many files as needed
>>>GET matrixname /FILE filereference/VARIABLES blah blah2
>>>blaaaaaaaaaaahhhhh........
>>>GET another/blah blah blah.......
>>>3. NO, RTFM! Syntax is simple, but you should carefully read the MATRIX
>>>language
>>>documentation before asking questions about it. Same with Macro!!!!!
>>>Not sure what you are doing with those save commands, perhaps you mean
>>>COMPUTE?
>>>COMPUTE blah=TRACE(INV(X)*Y)...
>>>4. YES.
>>>BTW: EXECUTE is *NOT* a valid MATRIX command.
>>>
>>>
>>>William Shakespeare wrote
>>>> I need to use the matrix-endmatrix command to perform matrix
operations
>> on
>>>> 150 files.  Not sure if I have Python with my implementation of SPSS
19,
>>>> so I think a small macro will be my best bet.  Some questions I have
not
>>>> been able to resolve:
>>>>
>>>> 1) I need to subscript my filenames with i, but I'm not sure I have
the
>>>> correct syntax.
>>>> 2) Not sure how to have two files open at one time in order to carry
out
>>>> the product of x inverse and y.
>>>> 3) Do I have the save coded correctly to save the value of t1 to a
file?
>>>> 4) If I save this file to a .sps file, can I include it to run the
>>>> commands?
>>>>
>>>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS,
but
>> I
>>>> don't have PROC IML.  Spent most of my day trying to find something
that
>>>> would tell me how to do this.
>>>>
>>>> compute1 t=0.
>>>>
>>>> !do !i=1 !to 150.
>>>> matrix.
>>>>
>>>> get x1_!i
>>>>  /file="C:\data\".
>>>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>  /names=varnames.
>>>>
>>>> save x1_!i_inv=inv(x1_i).
>>>>
>>>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>>>
>>>> save trace1_!i=trace(x_inv_y1_!i).
>>>>
>>>> t1=t1+trace1_!i.
>>>>
>>>> SAVE OUTFILE='c:\data\t1' .
>>>> EXECUTE.
>>>>
>>>> endmatrix.
>>>> !doend.
>>>>
>>>> =====================
>>>> 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
>>>
>>>
>>>
>>>
>>>
>>>-----
>>>Please reply to the list and not to my personal email.
>>>Those desiring my consulting or training services please feel free to
>> email me.
>>>---
>>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos ne forte conculcent eas pedibus suis."
>>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in abyssum?"
>>>--
>>>View this message in context: http://spssx-
>> discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>> tp5726385p5726389.html
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>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
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context: http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726451.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Macro to loop over files

William Shakespeare
In reply to this post by William Shakespeare
Ok. I rewrote this macro, and it runs, but I'm not seeing any printed
output. It prints the log, but not the matrices.  What's going on here?

define blocktrace ().
matrix.
compute1 t1=0.

!do !i=1 !to 2.

get x / file=!quote(!concat("C:\data\",x1_,!i)).
get y / file=!quote(!concat("C:\data\",y1_,!i)).

do if ncol(x)=nrow(y).
+do if det(x) gt 0.
+ compute x_inv=ginv(x).
+ print !i.
+ print x_inv.

+ compute x_inv_y=x_inv*y.
+ print !i.
+ print x_inv_y.

+ compute tr_x_inv=trace(x_inv_y).
+ print !i.
+ print tr_x_inv.

+ compute t1=t1+tr_x_inv.
+ print !i.
+ print t1.
+ELSE.
+ print !i/title "Determinant is 0".
+ end if.
ELSE.
+print !i/title "No. row ne no. cols."
endif.
!doend.

endmatrix.
!enddefine.





On Wed, 18 Jun 2014 17:47:37 -0700, David Marso <[hidden email]>
wrote:

>True that.
>END MATRIX right before the !ENDDEFINE.
>
>
>William Shakespeare wrote
>> Is there a missing endmatrix in this code block?  I think it hshould be
>> before the !endefine, corrrect?
>>
>> On Thu, 12 Jun 2014 12:32:35 -0700, David Marso &lt;
>
>> david.marso@
>
>> &gt;
>> wrote:
>>
>>>You would do yourself and us a big favor if you describe WHAT you are
>> trying
>>>to calculate rather than expect anyone to correct your MATRIX
>> program/macro.
>>>I'm not going to tell you what you need to change because (as written)
it
>> is
>>>a complete mess.
>>>As Rick stated, hard to believe you DON'T have documentation.
>>>If you click on the icon with the question mark it will load the syntax
>>>reference.
>>>It is also available online and downloadable PDF on the IBM site.
>>>A few things to note! (IN PARTICULAR the correct syntax for SAVE in
>> MATRIX).
>>>AFAICT, The following is likely to be exactly  what you are attempting
to

>>>achieve (after appropriate edits).
>>>Note also the defensive programming you need to apply when using MATRIX.
>>>It is for the most part an unforgiving B!#<# and any computational error
>>>will result in a FUBAR state of affairs!
>>>You really don't want to save 300 files!!! use PRINT.
>>>HTH...
>>>
>>>DEFINE blah().
>>>MATRIX.
>>>COMPUTE t1=0.
>>>!DO !I = 1 !TO ??.
>>>GET x /FILE=!QUOTE(!CONCAT(
>> <filepath>
>> ,x,!I)).
>>>GET y/FILE=!QUOTE(!CONCAT(
>> <filepath>
>> ,y,!I)).
>>>DO IF NCOL(X)=NROW(Y).
>>>+  DO IF DET(X) GT 0.
>>>
>>>/* COMPUTE Xinv=GINV(x).
>>>/* SAVE XINV / OUTFILE !QUOTE(!CONCAT(
>> <filepath>
>> ,xInv,!I)).
>>>/* PRINT !I .
>>>/* PRINT XInv.
>>>/* ................ */.
>>>
>>>+    COMPUTE T1=T1+TRACE(GINV(X)*Y).
>>>+  ELSE.
>>>+    PRINT !I / TITLE "Determinant is 0".
>>>+  END IF.
>>>ELSE.
>>>PRINT !i /TITLE "Row and column dimension mismatch".
>>>END IF.
>>>!DOEND.
>>>SAVE T1 / OUTFILE somefilename.
>>>!ENDDEFINE.
>>>
>>>
>>>William Shakespeare wrote
>>>> Thanks for your feedback.  As I said, I have no documentation, so I
have
>>>> to rely on what I can piece together from what I can find on the
>>>> internet.  I saw the save syntax somewhere on the internet as an
>>>> alternative to compute.  I'd like to save my intermediate files for
now
>> so
>>>> I can make spot checks of the computations.  Here's what I've been
able

>> to
>>>> put together from what I've read, and the suggestions here.  I don't
>> think
>>>> I need a period after on the !do line.  Not sure I need the get
>> statement
>>>> after the matrix product or if I need to add a get statement after
>> taking
>>>> the trace.  Feedback appreciated.
>>>>
>>>> define blocktrace
>>>> compute1 t1=0.
>>>>
>>>>  !do !i=1 !to 150
>>>>  matrix.
>>>>
>>>>  get !concat(x1_,!i).
>>>>   /file="C:\data\".
>>>>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>   /names=varnames.
>>>>
>>>>  compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
>>>>  save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).
>>>>
>>>>  get !concat(invx1_,!i).
>>>>    /file="C:\data\".
>>>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>    /names=varnames.
>>>>  get !concat(y1_,!i).
>>>>    /file="C:\data\".
>>>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>    /names=varnames.
>>>>
>>>>  compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
>>>>  save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).
>>>>
>>>>  get !concat("x_inv_y1_",!i).
>>>>    /file="C:\data\".
>>>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>    /names=varnames.
>>>>
>>>>  compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
>>>>  save outfile !quote(!concat("c:\data\","trace1_",!i)).
>>>>
>>>>  compute t1=t1+trace(!concat(1_,!i)).
>>>>
>>>>  SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).
>>>>
>>>>
>>>>  endmatrix.
>>>>  !doend.
>>>> !enddefine.
>>>>
>>>> On Sat, 7 Jun 2014 06:18:44 -0700, David Marso &lt;
>>>
>>>> david.marso@
>>>
>>>> &gt;
>>>> wrote:
>>>>
>>>>>MACRO is
>>>>>DEFINE !ENDDEFINE
>>>>>within that to iterate is !DO !DOEND
>>>>>1) look at !CONCAT
>>>>>2) MATRIX can open as many files as needed
>>>>>GET matrixname /FILE filereference/VARIABLES blah blah2
>>>>>blaaaaaaaaaaahhhhh........
>>>>>GET another/blah blah blah.......
>>>>>3. NO, RTFM! Syntax is simple, but you should carefully read the
MATRIX

>>>>>language
>>>>>documentation before asking questions about it. Same with Macro!!!!!
>>>>>Not sure what you are doing with those save commands, perhaps you mean
>>>>>COMPUTE?
>>>>>COMPUTE blah=TRACE(INV(X)*Y)...
>>>>>4. YES.
>>>>>BTW: EXECUTE is *NOT* a valid MATRIX command.
>>>>>
>>>>>
>>>>>William Shakespeare wrote
>>>>>> I need to use the matrix-endmatrix command to perform matrix
>> operations
>>>> on
>>>>>> 150 files.  Not sure if I have Python with my implementation of SPSS
>> 19,
>>>>>> so I think a small macro will be my best bet.  Some questions I have
>> not
>>>>>> been able to resolve:
>>>>>>
>>>>>> 1) I need to subscript my filenames with i, but I'm not sure I have
>> the
>>>>>> correct syntax.
>>>>>> 2) Not sure how to have two files open at one time in order to carry
>> out
>>>>>> the product of x inverse and y.
>>>>>> 3) Do I have the save coded correctly to save the value of t1 to a
>> file?
>>>>>> 4) If I save this file to a .sps file, can I include it to run the
>>>>>> commands?
>>>>>>
>>>>>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS,
>> but
>>>> I
>>>>>> don't have PROC IML.  Spent most of my day trying to find something
>> that
>>>>>> would tell me how to do this.
>>>>>>
>>>>>> compute1 t=0.
>>>>>>
>>>>>> !do !i=1 !to 150.
>>>>>> matrix.
>>>>>>
>>>>>> get x1_!i
>>>>>>  /file="C:\data\".
>>>>>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>>>  /names=varnames.
>>>>>>
>>>>>> save x1_!i_inv=inv(x1_i).
>>>>>>
>>>>>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>>>>>
>>>>>> save trace1_!i=trace(x_inv_y1_!i).
>>>>>>
>>>>>> t1=t1+trace1_!i.
>>>>>>
>>>>>> SAVE OUTFILE='c:\data\t1' .
>>>>>> EXECUTE.
>>>>>>
>>>>>> endmatrix.
>>>>>> !doend.
>>>>>>
>>>>>> =====================
>>>>>> 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
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>-----
>>>>>Please reply to the list and not to my personal email.
>>>>>Those desiring my consulting or training services please feel free to
>>>> email me.
>>>>>---
>>>>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>>>> porcos ne forte conculcent eas pedibus suis."
>>>>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum
cliff

>>>> in abyssum?"
>>>>>--
>>>>>View this message in context: http://spssx-
>>>> discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>>>> tp5726385p5726389.html
>>>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>>>
>>>>>=====================
>>>>>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
>>>>
>>>> =====================
>>>> 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
>>>
>>>
>>>
>>>
>>>
>>>-----
>>>Please reply to the list and not to my personal email.
>>>Those desiring my consulting or training services please feel free to
>> email me.
>>>---
>>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos ne forte conculcent eas pedibus suis."
>>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in abyssum?"
>>>--
>>>View this message in context: http://spssx-
>> discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>> tp5726385p5726451.html
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>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
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context: http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726534.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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: Macro to loop over files

David Marso
Administrator
You probably aren't calling the macro and you have at least three errors!
compute1?
Rather than endif and endmatrix.
It should be:
END IF.
and
END MATRIX.

William Shakespeare wrote
Ok. I rewrote this macro, and it runs, but I'm not seeing any printed
output. It prints the log, but not the matrices.  What's going on here?

define blocktrace ().
matrix.
compute1 t1=0.

!do !i=1 !to 2.

get x / file=!quote(!concat("C:\data\",x1_,!i)).
get y / file=!quote(!concat("C:\data\",y1_,!i)).

do if ncol(x)=nrow(y).
+do if det(x) gt 0.
+ compute x_inv=ginv(x).
+ print !i.
+ print x_inv.

+ compute x_inv_y=x_inv*y.
+ print !i.
+ print x_inv_y.

+ compute tr_x_inv=trace(x_inv_y).
+ print !i.
+ print tr_x_inv.

+ compute t1=t1+tr_x_inv.
+ print !i.
+ print t1.
+ELSE.
+ print !i/title "Determinant is 0".
+ end if.
ELSE.
+print !i/title "No. row ne no. cols."
endif.
!doend.

endmatrix.
!enddefine.





On Wed, 18 Jun 2014 17:47:37 -0700, David Marso <[hidden email]>
wrote:

>True that.
>END MATRIX right before the !ENDDEFINE.
>
>
>William Shakespeare wrote
>> Is there a missing endmatrix in this code block?  I think it hshould be
>> before the !endefine, corrrect?
>>
>> On Thu, 12 Jun 2014 12:32:35 -0700, David Marso <
>
>> david.marso@
>
>> >
>> wrote:
>>
>>>You would do yourself and us a big favor if you describe WHAT you are
>> trying
>>>to calculate rather than expect anyone to correct your MATRIX
>> program/macro.
>>>I'm not going to tell you what you need to change because (as written)
it
>> is
>>>a complete mess.
>>>As Rick stated, hard to believe you DON'T have documentation.
>>>If you click on the icon with the question mark it will load the syntax
>>>reference.
>>>It is also available online and downloadable PDF on the IBM site.
>>>A few things to note! (IN PARTICULAR the correct syntax for SAVE in
>> MATRIX).
>>>AFAICT, The following is likely to be exactly  what you are attempting
to
>>>achieve (after appropriate edits).
>>>Note also the defensive programming you need to apply when using MATRIX.
>>>It is for the most part an unforgiving B!#<# and any computational error
>>>will result in a FUBAR state of affairs!
>>>You really don't want to save 300 files!!! use PRINT.
>>>HTH...
>>>
>>>DEFINE blah().
>>>MATRIX.
>>>COMPUTE t1=0.
>>>!DO !I = 1 !TO ??.
>>>GET x /FILE=!QUOTE(!CONCAT(
>> <filepath>
>> ,x,!I)).
>>>GET y/FILE=!QUOTE(!CONCAT(
>> <filepath>
>> ,y,!I)).
>>>DO IF NCOL(X)=NROW(Y).
>>>+  DO IF DET(X) GT 0.
>>>
>>>/* COMPUTE Xinv=GINV(x).
>>>/* SAVE XINV / OUTFILE !QUOTE(!CONCAT(
>> <filepath>
>> ,xInv,!I)).
>>>/* PRINT !I .
>>>/* PRINT XInv.
>>>/* ................ */.
>>>
>>>+    COMPUTE T1=T1+TRACE(GINV(X)*Y).
>>>+  ELSE.
>>>+    PRINT !I / TITLE "Determinant is 0".
>>>+  END IF.
>>>ELSE.
>>>PRINT !i /TITLE "Row and column dimension mismatch".
>>>END IF.
>>>!DOEND.
>>>SAVE T1 / OUTFILE somefilename.
>>>!ENDDEFINE.
>>>
>>>
>>>William Shakespeare wrote
>>>> Thanks for your feedback.  As I said, I have no documentation, so I
have
>>>> to rely on what I can piece together from what I can find on the
>>>> internet.  I saw the save syntax somewhere on the internet as an
>>>> alternative to compute.  I'd like to save my intermediate files for
now
>> so
>>>> I can make spot checks of the computations.  Here's what I've been
able
>> to
>>>> put together from what I've read, and the suggestions here.  I don't
>> think
>>>> I need a period after on the !do line.  Not sure I need the get
>> statement
>>>> after the matrix product or if I need to add a get statement after
>> taking
>>>> the trace.  Feedback appreciated.
>>>>
>>>> define blocktrace
>>>> compute1 t1=0.
>>>>
>>>>  !do !i=1 !to 150
>>>>  matrix.
>>>>
>>>>  get !concat(x1_,!i).
>>>>   /file="C:\data\".
>>>>   /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>   /names=varnames.
>>>>
>>>>  compute=!concat(x1_,!i,_inv)=inv(!concat(x1,_i)).
>>>>  save outfile=!quote(!concat("c:\data\","invx1_",!i,".sav")).
>>>>
>>>>  get !concat(invx1_,!i).
>>>>    /file="C:\data\".
>>>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>    /names=varnames.
>>>>  get !concat(y1_,!i).
>>>>    /file="C:\data\".
>>>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>    /names=varnames.
>>>>
>>>>  compute=!concat(x_inv_y1_,!i)=!concat(x1_,!i,_inv)*!concat(y1_,!i).
>>>>  save outfile !quote(!concat("c:\data\","x_inv_y1_",!i,".sav")).
>>>>
>>>>  get !concat("x_inv_y1_",!i).
>>>>    /file="C:\data\".
>>>>    /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>    /names=varnames.
>>>>
>>>>  compute !concat(trace1_,!i)=trace(!concat(x_inv_y1_,!i)).
>>>>  save outfile !quote(!concat("c:\data\","trace1_",!i)).
>>>>
>>>>  compute t1=t1+trace(!concat(1_,!i)).
>>>>
>>>>  SAVE OUTFILE=!QUOTE(!CONCAT("c:\data\",!t1,".sav")).
>>>>
>>>>
>>>>  endmatrix.
>>>>  !doend.
>>>> !enddefine.
>>>>
>>>> On Sat, 7 Jun 2014 06:18:44 -0700, David Marso <
>>>
>>>> david.marso@
>>>
>>>> >
>>>> wrote:
>>>>
>>>>>MACRO is
>>>>>DEFINE !ENDDEFINE
>>>>>within that to iterate is !DO !DOEND
>>>>>1) look at !CONCAT
>>>>>2) MATRIX can open as many files as needed
>>>>>GET matrixname /FILE filereference/VARIABLES blah blah2
>>>>>blaaaaaaaaaaahhhhh........
>>>>>GET another/blah blah blah.......
>>>>>3. NO, RTFM! Syntax is simple, but you should carefully read the
MATRIX
>>>>>language
>>>>>documentation before asking questions about it. Same with Macro!!!!!
>>>>>Not sure what you are doing with those save commands, perhaps you mean
>>>>>COMPUTE?
>>>>>COMPUTE blah=TRACE(INV(X)*Y)...
>>>>>4. YES.
>>>>>BTW: EXECUTE is *NOT* a valid MATRIX command.
>>>>>
>>>>>
>>>>>William Shakespeare wrote
>>>>>> I need to use the matrix-endmatrix command to perform matrix
>> operations
>>>> on
>>>>>> 150 files.  Not sure if I have Python with my implementation of SPSS
>> 19,
>>>>>> so I think a small macro will be my best bet.  Some questions I have
>> not
>>>>>> been able to resolve:
>>>>>>
>>>>>> 1) I need to subscript my filenames with i, but I'm not sure I have
>> the
>>>>>> correct syntax.
>>>>>> 2) Not sure how to have two files open at one time in order to carry
>> out
>>>>>> the product of x inverse and y.
>>>>>> 3) Do I have the save coded correctly to save the value of t1 to a
>> file?
>>>>>> 4) If I save this file to a .sps file, can I include it to run the
>>>>>> commands?
>>>>>>
>>>>>> Sorry if this is stupid.  I can do this in about 10 minutes in SAS,
>> but
>>>> I
>>>>>> don't have PROC IML.  Spent most of my day trying to find something
>> that
>>>>>> would tell me how to do this.
>>>>>>
>>>>>> compute1 t=0.
>>>>>>
>>>>>> !do !i=1 !to 150.
>>>>>> matrix.
>>>>>>
>>>>>> get x1_!i
>>>>>>  /file="C:\data\".
>>>>>>  /variables=col1 col2 col3 col4 col5 col6 col6 col7.
>>>>>>  /names=varnames.
>>>>>>
>>>>>> save x1_!i_inv=inv(x1_i).
>>>>>>
>>>>>> save x_inv_y1_!i=x1_!i_inv*y1_!i.
>>>>>>
>>>>>> save trace1_!i=trace(x_inv_y1_!i).
>>>>>>
>>>>>> t1=t1+trace1_!i.
>>>>>>
>>>>>> SAVE OUTFILE='c:\data\t1' .
>>>>>> EXECUTE.
>>>>>>
>>>>>> endmatrix.
>>>>>> !doend.
>>>>>>
>>>>>> =====================
>>>>>> 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
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>-----
>>>>>Please reply to the list and not to my personal email.
>>>>>Those desiring my consulting or training services please feel free to
>>>> email me.
>>>>>---
>>>>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>>>> porcos ne forte conculcent eas pedibus suis."
>>>>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum
cliff
>>>> in abyssum?"
>>>>>--
>>>>>View this message in context: http://spssx-
>>>> discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>>>> tp5726385p5726389.html
>>>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>>>
>>>>>=====================
>>>>>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
>>>>
>>>> =====================
>>>> 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
>>>
>>>
>>>
>>>
>>>
>>>-----
>>>Please reply to the list and not to my personal email.
>>>Those desiring my consulting or training services please feel free to
>> email me.
>>>---
>>>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos ne forte conculcent eas pedibus suis."
>>>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in abyssum?"
>>>--
>>>View this message in context: http://spssx-
>> discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
>> tp5726385p5726451.html
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>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
>>
>> =====================
>> 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
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to
email me.
>---
>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante
porcos ne forte conculcent eas pedibus suis."
>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
in abyssum?"
>--
>View this message in context: http://spssx-
discussion.1045642.n5.nabble.com/Macro-to-loop-over-files-
tp5726385p5726534.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"