Macro Modification

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

Macro Modification

Ujjawal
Hi SPSS Gurus,

I would like to thank each and every one of you, especially David Marso, for your expert answers. I am learning SPSS everyday by reading your comments.

I have a query about macros. Recently i created a macro which generates correlation matrix by several data cuts. Lets say, a variable named Age has 5 options. In this macro, i have to assign all the 5 values. I want to get rid of this step. That means a macro which counts the number of options that Age contains and run the correlation matrix by each options.

DEFINE !Var (Demogs=!CMDEND)
!DO !D !IN (!demogs)
GET File = 'C:\Documents and Settings\Ujjawal\My Documents\Downloads\examples\Datafile.sav'.
!LET !fname=!QUOTE(!CONCAT('C:\Documents and Settings\Ujjawal\My Documents\Downloads\examples\','Corr','-',!D,'.SAV'))

Select If Age = !D.
Execute.

CORRELATIONS
/VARIABLES= FinalScore Product Brand
/MISSING=LISTWISE
/MATRIX = OUT (!fname).
!DOEND
!ENDDEFINE.

SET MPRINT ON.
!Var Demogs=1 2 3 4 5.

Instead of typing *1 2 3 4 5* i want macro to be called by typing AGE only. Is it possible ?
Any help would be greatly appreciated !
Reply | Threaded
Open this post in threaded view
|

Re: Macro Modification

Bruce Weaver
Administrator
Have you considered using SPLIT FILE?  E.g.,

SORT CASES BY age.
SPLIT FILE by age.
CORRELATIONS
/VARIABLES= FinalScore Product Brand
/MISSING=LISTWISE
/MATRIX = OUT (filename).

Then open filename, and use a series of XSAVE OUTFILE commands if you really need separate files.

HTH.


Ujjawal wrote
Hi SPSS Gurus,

I would like to thank each and every one of you, especially David Marso, for your expert answers. I am learning SPSS everyday by reading your comments.

I have a query about macros. Recently i created a macro which generates correlation matrix by several data cuts. Lets say, a variable named Age has 5 options. In this macro, i have to assign all the 5 values. I want to get rid of this step. That means a macro which counts the number of options that Age contains and run the correlation matrix by each options.

DEFINE !Var (Demogs=!CMDEND)
!DO !D !IN (!demogs)
GET File = 'C:\Documents and Settings\Ujjawal\My Documents\Downloads\examples\Datafile.sav'.
!LET !fname=!QUOTE(!CONCAT('C:\Documents and Settings\Ujjawal\My Documents\Downloads\examples\','Corr','-',!D,'.SAV'))

Select If Age = !D.
Execute.

CORRELATIONS
/VARIABLES= FinalScore Product Brand
/MISSING=LISTWISE
/MATRIX = OUT (!fname).
!DOEND
!ENDDEFINE.

SET MPRINT ON.
!Var Demogs=1 2 3 4 5.

Instead of typing *1 2 3 4 5* i want macro to be called by typing AGE only. Is it possible ?
Any help would be greatly appreciated !
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Macro Modification

David Marso
Administrator
Indeed! SPLIT FILE...(modify the macro to accept the requested variable as a parameter) and do the SORT/SPLIT /CORR etc inside.
If I may also assume this query is somewhat related to the recent MATRIX question:
http://spssx-discussion.1045642.n5.nabble.com/Save-Matrix-Expressions-to-SPSS-Data-File-td5718335.html

Anticipating that this is indeed the case...(If not, IGNORE).
----
I will edify you with the following:
MATRIX supports SPLIT file but only within a "batch" environment (ie INCLUDE or INSERT with appropriate switches).
SO!  
You will need to disable SPLIT FILE processing prior to entering the MATRIX subsystem and handle anything internally by enumerating the differences in the code by comparing values of the split across adjacent rows.
*OR*
Place the MATRIX program into an external file and use INCLUDE or INSERT.
---

__
Note that either of MATRIX or MACRO are a TPITA to implement in a robust manner and you will want to CYA for all sorts of possible disasters (division by 0,improper dimensions or arguments) and there is absolutely no error handling.  As a program becomes larger it becomes a beast to localize FUBARified code.
I have adopted a strategy of adding lines such as:

DO IF (!debug GT 0).
PRINT / TITLE "Signpost 42: you are here and all is well".
END IF.

Also

DO IF (mydenom GT 0).
COMPUTE x= top/mydenom.
ELSE.
COMPUTE divby0=1.
PRINT /TITLE "Something aint right with the world ;-(((".
END IF.
---
Yeah, I am in the midst of a 2000 line beats at the moment and I am somehow keeping the hairline recession minimized by such.
Hope this postpones a few cycles of cognitive meltdown ;-)
---
Have fun.
David
-
Bruce Weaver wrote
Have you considered using SPLIT FILE?  E.g.,

SORT CASES BY age.
SPLIT FILE by age.
CORRELATIONS
/VARIABLES= FinalScore Product Brand
/MISSING=LISTWISE
/MATRIX = OUT (filename).

Then open filename, and use a series of XSAVE OUTFILE commands if you really need separate files.

HTH.


Ujjawal wrote
Hi SPSS Gurus,

I would like to thank each and every one of you, especially David Marso, for your expert answers. I am learning SPSS everyday by reading your comments.

I have a query about macros. Recently i created a macro which generates correlation matrix by several data cuts. Lets say, a variable named Age has 5 options. In this macro, i have to assign all the 5 values. I want to get rid of this step. That means a macro which counts the number of options that Age contains and run the correlation matrix by each options.

DEFINE !Var (Demogs=!CMDEND)
!DO !D !IN (!demogs)
GET File = 'C:\Documents and Settings\Ujjawal\My Documents\Downloads\examples\Datafile.sav'.
!LET !fname=!QUOTE(!CONCAT('C:\Documents and Settings\Ujjawal\My Documents\Downloads\examples\','Corr','-',!D,'.SAV'))

Select If Age = !D.
Execute.

CORRELATIONS
/VARIABLES= FinalScore Product Brand
/MISSING=LISTWISE
/MATRIX = OUT (!fname).
!DOEND
!ENDDEFINE.

SET MPRINT ON.
!Var Demogs=1 2 3 4 5.

Instead of typing *1 2 3 4 5* i want macro to be called by typing AGE only. Is it possible ?
Any help would be greatly appreciated !
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 Modification

Albert-Jan Roskam
In reply to this post by Bruce Weaver
'Layered by' usually does what I want:
SPLIT FILE LAYERED BY age.

(isn't the default called 'separated by'?).

Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


----- Original Message -----

> From: Bruce Weaver <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Thursday, March 7, 2013 9:07 PM
> Subject: Re: [SPSSX-L] Macro Modification
>
> Have you considered using SPLIT FILE?  E.g.,
>
> SORT CASES BY age.
> SPLIT FILE by age.
> CORRELATIONS
> /VARIABLES= FinalScore Product Brand
> /MISSING=LISTWISE
> /MATRIX = OUT (filename).
>
> Then open filename, and use a series of XSAVE OUTFILE commands if you really
> need separate files.
>
> HTH.
>
>
>
> Ujjawal wrote
>>  Hi SPSS Gurus,
>>
>>  I would like to thank each and every one of you, especially
> *
>>  David Marso
> *
>>  , for your expert answers. I am learning SPSS everyday by reading your
>>  comments.
>>
>>  I have a query about macros. Recently i created a macro which generates
>>  correlation matrix by several data cuts. Lets say, a variable named Age
>>  has 5 options. In this macro, i have to assign all the 5 values. I want to
>>  get rid of this step. That means a macro which counts the number of
>>  options that Age contains and run the correlation matrix by each options.
>>
>>  DEFINE !Var (Demogs=!CMDEND)
>>  !DO !D !IN (!demogs)
>>  GET File = 'C:\Documents and Settings\Ujjawal\My
>>  Documents\Downloads\examples\Datafile.sav'.
>>  !LET !fname=!QUOTE(!CONCAT('C:\Documents and
> Settings\Ujjawal\My
>>
> Documents\Downloads\examples\','Corr','-',!D,'.SAV'))
>>
>>  Select If Age = !D.
>>  Execute.
>>
>>  CORRELATIONS
>>  /VARIABLES= FinalScore Product Brand
>>  /MISSING=LISTWISE
>>  /MATRIX = OUT (!fname).
>>  !DOEND
>>  !ENDDEFINE.
>>
>>  SET MPRINT ON.
>>  !Var Demogs=1 2 3 4 5.
>>
>>  Instead of typing *1 2 3 4 5* i want macro to be called by typing AGE
>>  only. Is it possible ?
>>  Any help would be greatly appreciated !
>
>
>
>
>
> -----
> --
> Bruce Weaver
> [hidden email]
> http://sites.google.com/a/lakeheadu.ca/bweaver/
>
> "When all else fails, RTFM."
>
> NOTE: My Hotmail account is not monitored regularly.
> To send me an e-mail, please use the address shown above.
>
> --
> View this message in context:
> http://spssx-discussion.1045642.n5.nabble.com/Macro-Modification-tp5718464p5718465.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 Modification

Ujjawal
This post was updated on .
Thank you so much Bruce and David for showing me the way. Yes David, your assumption is absolutely correct. This query is related to the matrix question i posted few days ago.
 
I considered using SPLIT FILE. As you suggested, i disabled SPLIT FILE processing prior to entering the MATRIX subsystem ( SPLIT FILE OFF). MGET created 5 matrices CRS1 , CRS2 , CRS3, CRS4 and CRS5. After creating the matrices, SPSS returned this error :
Error # 12492
>An attempt has been made to use previously undefined matrix (or scalar).
>This command not executed.
Matrix - 'CR' is undefined


I guess this is because of the following commands : (  Source link )
COMPUTE R = CR.                   /* CR is default name of matrix */
COMPUTE N = NCOL(R).              /* Calculate number of variables */
COMPUTE RXX = R(2:N,2:N).         /* Correlations between the X's */


Altenatively, i command SPSS to split the file prior to correlation matrix syntax and then open the file and save separate files for each variable options. To accomplish the task of saving separate files i have to mention all the options (1 2 3 4 5) in loop mentioned in the macro. I guess i am making this process complicated :(
Reply | Threaded
Open this post in threaded view
|

Re: Macro Modification

kwame woei
In reply to this post by Ujjawal
Try the WRITE OUTFILE function in combination with the INSERT function.

WRITE OUTFILE = "....."
/"define Macro ()"' varname'" enddefine.

INSERT "generated outfile".




Op 8 mrt. 2013 om 15:28 heeft "Ujjawal" <[hidden email]> het volgende geschreven:

> Hi SPSS Gurus,
>
> I would like to thank each and every one of you, especially *David Marso*,
> for your expert answers. I am learning SPSS everyday by reading your
> comments.
>
> I have a query about macros. Recently i created a macro which generates
> correlation matrix by several data cuts. Lets say, a variable named Age has
> 5 options. In this macro, i have to assign all the 5 values. I want to get
> rid of this step. That means a macro which counts the number of options that
> Age contains and run the correlation matrix by each options.
>
> DEFINE !Var (Demogs=!CMDEND)
> !DO !D !IN (!demogs)
> GET File = 'C:\Documents and Settings\Ujjawal\My
> Documents\Downloads\examples\Datafile.sav'.
> !LET !fname=!QUOTE(!CONCAT('C:\Documents and Settings\Ujjawal\My
> Documents\Downloads\examples\','Corr','-',!D,'.SAV'))
>
> Select If Age = !D.
> Execute.
>
> CORRELATIONS
> /VARIABLES= FinalScore Product Brand
> /MISSING=LISTWISE
> /MATRIX = OUT (!fname).
> !DOEND
> !ENDDEFINE.
>
> SET MPRINT ON.
> !Var Demogs=1 2 3 4 5.
>
> Instead of typing *1 2 3 4 5* i want macro to be called by typing AGE only.
> Is it possible ?
> Any help would be greatly appreciated !
>
>
>
>
> --
> View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Macro-Modification-tp5718464.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