DO REPEAT and shifting dataset names and dates

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

DO REPEAT and shifting dataset names and dates

Krinsky, Alan-2
DO REPEAT and shifting dataset names and dates

Does anyone know if it is possible to place a GET DATA command within a DO REPEAT-END REPEAT loop? I want to run the same set of operations on different data sets, basically ones shifting by quarterly time frames and want to run once instead of redoing code for each dataset. I was also looking at MACROs as a possible method of accomplishing this, but thought the DO REPEAT might be more appropriate. I guess another way of phrasing the question is that I have changing dataset names as well as changing dates and want to be able to code this to loop through the matches, and even produce and save the transformed datasets, each with its own name corresponding to the original one. Any thoughts? Thanks, and be well!

Alan

"If people don't want to come out to the ballpark, how are you going to stop them?"
"I always thought that record would stand until it was broken."
-Yogi Berra

Alan D. Krinsky  PhD, MPH
Medical Management Interventions Manager
UMass Memorial Health Care
Hahnemann Campus
281 Lincoln St.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail: [hidden email]

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
Reply | Threaded
Open this post in threaded view
|

Re: DO REPEAT and shifting dataset names and dates

Richard Ristow
At 11:46 AM 1/29/2010, Krinsky, Alan wrote:

>Does anyone know if it is possible to place a GET DATA command
>within a DO REPEAT-END REPEAT loop? I want to run the same set of
>operations on different data sets

No, that won't work. DO REPEAT works only within a transformation
program or input program; it's part of the code that's executed once
per record.

>I was also looking at MACROs as a possible method of accomplishing
>this, but thought the DO REPEAT might be more appropriate.

There are three ways (that I know of) to write a loop to generate
multiple input operations:

. Use a macro loop, as you were thinking of
. Use Python to generate the code
. Have SPSS read the dataset values, generate the code and write it
to an external file, and INSERT it. (This one isn't currently
popular, but it does work.)

Offhand, I'd consider a macro loop for your case.

-Best of luck,
  Richard

=====================
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: DO REPEAT and shifting dataset names and dates

Jerabek Jindrich
In reply to this post by Krinsky, Alan-2
Hello,

DO REPEAT is not for this purpose and cannot be used (unless the DO REP changed a lot in new versions).

But macros with the loop !DO should be perfect to do it.

Jindra

> ------------ Původní zpráva ------------
> Od: Krinsky, Alan <[hidden email]>
> Předmět: DO REPEAT and shifting dataset names and dates
> Datum: 29.1.2010 19:32:55
> ----------------------------------------
> Does anyone know if it is possible to place a GET DATA command within a
> DO REPEAT-END REPEAT loop? I want to run the same set of operations on
> different data sets, basically ones shifting by quarterly time frames
> and want to run once instead of redoing code for each dataset. I was
> also looking at MACROs as a possible method of accomplishing this, but
> thought the DO REPEAT might be more appropriate. I guess another way of
> phrasing the question is that I have changing dataset names as well as
> changing dates and want to be able to code this to loop through the
> matches, and even produce and save the transformed datasets, each with
> its own name corresponding to the original one. Any thoughts? Thanks,
> and be well!
>
> Alan
>
> "If people don't want to come out to the ballpark, how are you going to
> stop them?"
> "I always thought that record would stand until it was broken."
> -Yogi Berra
>
> Alan D. Krinsky  PhD, MPH
> Medical Management Interventions Manager
> UMass Memorial Health Care
> Hahnemann Campus
> 281 Lincoln St.
> Worcester, MA 01605
> Phone: 508-334-5854
> Fax: 508-793-6086
> E-mail: [hidden email]
>
>
>
> The information transmitted is intended only for the person or
> entity to which it is addressed and may contain confidential and/or
> privileged material.  Any review, transmission, re-transmission,
> dissemination or other use of, or taking of any action in reliance
> upon this information by persons or entities other than the
> intended recipient is prohibited.  If you received this in error,
> please contact the sender and delete the material from any
> computer.
>
>

=====================
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: DO REPEAT and shifting dataset names and dates

Albert-Jan Roskam
In reply to this post by Krinsky, Alan-2
Hi Alan,

Inside DO REPEAT, only xsave, count, select, recode, do if, do repeat, compute, loop (and maybe a couple of other commands) can be used. A macro is one way, but you'd still need to hard-code the file and variable names. A neater (and easier) way would be to use a little Python program. But I'm not sure whether I entirely understand you. You have data files that all reside in one directory, each with the same format, but the only thing that differs is one of the variable names (the date variable). Is that correct? Are they tab-separated files? Or csv? Could you poste the GET DATA syntax and indicate which parts would differ for each file?

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 1/29/10, Krinsky, Alan <[hidden email]> wrote:

From: Krinsky, Alan <[hidden email]>
Subject: [SPSSX-L] DO REPEAT and shifting dataset names and dates
To: [hidden email]
Date: Friday, January 29, 2010, 5:46 PM

DO REPEAT and shifting dataset names and dates

Does anyone know if it is possible to place a GET DATA command within a DO REPEAT-END REPEAT loop? I want to run the same set of operations on different data sets, basically ones shifting by quarterly time frames and want to run once instead of redoing code for each dataset. I was also looking at MACROs as a possible method of accomplishing this, but thought the DO REPEAT might be more appropriate. I guess another way of phrasing the question is that I have changing dataset names as well as changing dates and want to be able to code this to loop through the matches, and even produce and save the transformed datasets, each with its own name corresponding to the original one. Any thoughts? Thanks, and be well!

Alan

"If people don't want to come out to the ballpark, how are you going to stop them?"
"I always thought that record would stand until it was broken."
-Yogi Berra

Alan D. Krinsky  PhD, MPH
Medical Management Interventions Manager
UMass Memorial Health Care
Hahnemann Campus
281 Lincoln St.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail: [hidden email]

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

Reply | Threaded
Open this post in threaded view
|

Re: DO REPEAT and shifting dataset names and dates

Art Kendall
In reply to this post by Krinsky, Alan-2
I'm not sure that I completely understand your question, but see if  this  helps.
For each special run use a few lines of syntax to specify the file and any specific transforms,  Then use the INSERT command for the common operations. Something like this:
* first dataset.
get data...
compute ...
select ...
insert file= 'common.setof.operation.sps'.
* second data set
get data...
compute ...
select ...
insert file= 'common.setof.operation.sps'.
*third data set.
get data...
compute ...
select ...
insert file= 'common.setof.operation.sps'.

Art Kendall
Social Research Consultants

On 1/29/2010 11:46 AM, Krinsky, Alan wrote:
DO REPEAT and shifting dataset names and dates

Does anyone know if it is possible to place a GET DATA command within a DO REPEAT-END REPEAT loop? I want to run the same set of operations on different data sets, basically ones shifting by quarterly time frames and want to run once instead of redoing code for each dataset. I was also looking at MACROs as a possible method of accomplishing this, but thought the DO REPEAT might be more appropriate. I guess another way of phrasing the question is that I have changing dataset names as well as changing dates and want to be able to code this to loop through the matches, and even produce and save the transformed datasets, each with its own name corresponding to the original one. Any thoughts? Thanks, and be well!

Alan

"If people don't want to come out to the ballpark, how are you going to stop them?"
"I always thought that record would stand until it was broken."
-Yogi Berra

Alan D. Krinsky  PhD, MPH
Medical Management Interventions Manager
UMass Memorial Health Care
Hahnemann Campus
281 Lincoln St.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail: [hidden email]

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
===================== 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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: DO REPEAT and shifting dataset names and dates

Richard Ristow
In reply to this post by Albert-Jan Roskam
At 05:26 AM 1/30/2010, Albert-Jan Roskam wrote:

Inside DO REPEAT, only xsave, count, select, recode, do if, do repeat, compute, loop (and maybe a couple of other commands) can be used.

You can use RECODE inside DO REPEAT, but since it accepts variable lists itself, you will rarely need to.

You can't use XSAVE or DO REPEAT inside a DO REPEAT; see below. You can nest LOOP inside DO REPEAT, and DO REPEAT inside LOOP.

You can use declarative SPSS commands,
STRING, NUMERIC, FORMATS, MISSING VALUES,
VAR LABELS, VAL LABELS
inside a DO REPEAT, but using variable lists on the declarative statements is usually preferable.

==============================
Illustrations using DO REPEAT
==============================
|-----------------------------|---------------------------|
|Output Created               |30-JAN-2010 22:23:04       |
|-----------------------------|---------------------------|
[RolledUp]
 
Alphabet Serial1 Serial2 Serial3 Letter1 Letter2 Letter3

Roman        1       2       3   A       B       C
Greek        4       5       6   Gamma   Delta   Epsilon

Number of cases read:  2    Number of cases listed:  2

 
*  XSAVE                          ... .
*  ...   XSAVE inside LOOP        ... .
DATASET ACTIVATE RolledUp WINDOW=ASIS.
DATASET COPY     WithLoop WINDOW=FRONT.
DATASET ACTIVATE WithLoop WINDOW=FRONT.
VECTOR  Nbr=Serial1 TO Serial3
       /Ltr=Letter1 TO Letter3.

NUMERIC Order  (F2).
STRING  Text   (A7).

LOOP  #Idx = 1 TO 3.
.  COMPUTE Order = Nbr(#Idx).
.  COMPUTE Text  = Ltr(#Idx).
.  XSAVE   OUTFILE=File1
   /KEEP=  Alphabet Order Text.
END LOOP.
EXECUTE /* ---  One of the case where it's needed  --- */.

GET FILE=File1.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |30-JAN-2010 22:23:11       |
|-----------------------------|---------------------------|
C:\Documents and Settings\Richard\My Documents
  \Temporary\SPSS\
   2010-01-30 Roskam -
   Re-DO REPEAT and shifting dataset names and dates. - LOOP.SAV
 
Alphabet Order Text

Roman       1  A
Roman       2  B
Roman       3  C
Greek       4  Gamma
Greek       5  Delta
Greek       6  Epsilon


Number of cases read:  6    Number of cases listed:  6

 
*  ...   XSAVE inside DO REPEAT   ... .
DATASET ACTIVATE RolledUp WINDOW=ASIS.
DATASET COPY     WithRpt  WINDOW=FRONT.
DATASET ACTIVATE WithRpt  WINDOW=FRONT.
NUMERIC Order  (F2).
STRING  Text   (A7).

DO REPEAT
        Nbr=Serial1 TO Serial3
       /Ltr=Letter1 TO Letter3.
.  COMPUTE Order = Nbr.
.  COMPUTE Text  = Ltr.
.  XSAVE   OUTFILE=File2

>Error # 4530.  Command name: XSAVE
>This command is not allowed inside the DO REPEAT/ END REPEAT facility.  The
>command will be ignored.
>This command not executed.

   /KEEP=  Alphabet Order Text.
END REPEAT.

EXECUTE /* ---  One of the cases where it's needed  --- */.

GET FILE=File2.

>Error # 31.  Command name: GET FILE
>File not found.
>This command not executed.



*  Nested DO REPEAT                                    ... .

NEW FILE.
INPUT PROGRAM.

NUMERIC A B C D E (F2).
RECODE  A B C D E (ELSE=1).

END CASE.
END FILE.
END INPUT PROGRAM.

DO REPEAT FACTOR = 2 3 5.
.  DO REPEAT NUMBER = A TO E.

>Error # 4530.  Command name: DO REPEAT
>This command is not allowed inside the DO REPEAT/ END REPEAT facility.  The
>command will be ignored.
>This command not executed.

.     COMPUTE NUMBER = NUMBER * FACTOR.
.  END REPEAT.
END REPEAT.

>Error # 4001.  Command name: END REPEAT
>An END REPEAT command has appeared without a previous DO REPEAT command.
>This command not executed.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |30-JAN-2010 22:23:19       |
|-----------------------------|---------------------------|
 A  B  C  D  E   NUMBER

 1  1  1  1  1      .

Number of cases read:  1    Number of cases listed:  1

 
*  DO REPEAT within LOOP is documented to work  --     ... .
*  see Command Syntax Reference article on DO REPEAT   ... .


*  LOOP within DO REPEAT                               ... .

NEW FILE.
INPUT PROGRAM.

NUMERIC A B C D E (F2).
RECODE  A B C D E (ELSE=1).

END CASE.
END FILE.
END INPUT PROGRAM.

VECTOR NUMBER=A TO E.

DO REPEAT FACTOR = 2 3 5.
.  LOOP #IDX = 1 TO 5.
.     COMPUTE NUMBER(#IDX) = NUMBER(#IDX) * FACTOR.
.  END LOOP..
END REPEAT.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |30-JAN-2010 22:23:23       |
|-----------------------------|---------------------------|
 A  B  C  D  E

30 30 30 30 30

Number of cases read:  1    Number of cases listed:  1

 
*  Declarative statements within DO REPEAT             ... ,

NEW FILE.
INPUT PROGRAM.
.  NUMERIC RecdNum (F2).
.  COMPUTE RecdNum=1.
.  END CASE.
END FILE.
END INPUT PROGRAM.

DO REPEAT NewVar = V1 TO V5.
.  NUMERIC NewVar (F2).
END REPEAT.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |30-JAN-2010 22:23:29       |
|-----------------------------|---------------------------|
RecdNum V1 V2 V3 V4 V5

    1    .  .  .  .  .

Number of cases read:  1    Number of cases listed:  1
==============================
APPENDIX:  Test data, and code
==============================
*  C:\Documents and Settings\Richard\My Documents                               .
*    \Technical\spssx-l\Z 2010a                                                 .
*    \2010-01-30 Roskam - Re-DO REPEAT and shifting dataset names and dates.SPS .

*  In response to posting                                            .
*  Date:     Sat, 30 Jan 2010 02:26:43 -0800                         .
*  From:     Albert-Jan Roskam <[hidden email]>                     .
*  Subject:  Re: DO REPEAT and shifting dataset names and dates      .
*  Comments: To: AlanKrinsky <[hidden email]>        .
*  To:       [hidden email]                                .

FILE HANDLE File1
 /NAME='C:\Documents and Settings\Richard\My Documents'              +
         '\Temporary\SPSS\'                                          +
       '2010-01-30 Roskam - '                                        +
       'Re-DO REPEAT and shifting dataset names and dates.'          +
       ' - '                                                         +
       'LOOP.SAV'.                                                 

FILE HANDLE File2
 /NAME='C:\Documents and Settings\Richard\My Documents'              +
         '\Temporary\SPSS\'                                          +
       '2010-01-30 Roskam - '                                        +
       'Re-DO REPEAT and shifting dataset names and dates.'          +
       ' - '                                                         +
       'DO REPEAT.SAV'.                                                 

NEW FILE.
DATA LIST LIST/
   Alphabet Serial1 Serial2 Serial3 Letter1 Letter2 Letter3
   (A5,     F2,     F2,     F2,     A7,     A7,     A7).
BEGIN DATA
   Roman    1       2       3       A       B       C
   Greek    4       5       6       Gamma   Delta   Epsilon
END DATA.
DATASET NAME RolledUp.
LIST.

*  XSAVE                          ... .
*  ...   XSAVE inside LOOP        ... .
DATASET ACTIVATE RolledUp WINDOW=ASIS.
DATASET COPY     WithLoop WINDOW=FRONT.
DATASET ACTIVATE WithLoop WINDOW=FRONT.

VECTOR  Nbr=Serial1 TO Serial3
       /Ltr=Letter1 TO Letter3.

NUMERIC Order  (F2).
STRING  Text   (A7).

LOOP  #Idx = 1 TO 3.
.  COMPUTE Order = Nbr(#Idx).
.  COMPUTE Text  = Ltr(#Idx).
.  XSAVE   OUTFILE=File1
   /KEEP=  Alphabet Order Text.
END LOOP.
EXECUTE /* ---  One of the case where it's needed  --- */.

GET FILE=File1.
LIST.

*  ...   XSAVE inside DO REPEAT   ... .
DATASET ACTIVATE RolledUp WINDOW=ASIS.
DATASET COPY     WithRpt  WINDOW=FRONT.
DATASET ACTIVATE WithRpt  WINDOW=FRONT.

NUMERIC Order  (F2).
STRING  Text   (A7).

DO REPEAT
        Nbr=Serial1 TO Serial3
       /Ltr=Letter1 TO Letter3.
.  COMPUTE Order = Nbr.
.  COMPUTE Text  = Ltr.
.  XSAVE   OUTFILE=File2
   /KEEP=  Alphabet Order Text.
END REPEAT.

EXECUTE /* ---  One of the cases where it's needed  --- */.

GET FILE=File2.
LIST.

*  Nested DO REPEAT                                    ... .

NEW FILE.
INPUT PROGRAM.

NUMERIC A B C D E (F2).
RECODE  A B C D E (ELSE=1).

END CASE.
END FILE.
END INPUT PROGRAM.

DO REPEAT FACTOR = 2 3 5.
.  DO REPEAT NUMBER = A TO E.
.     COMPUTE NUMBER = NUMBER * FACTOR.
.  END REPEAT.
END REPEAT.

LIST.

*  DO REPEAT within LOOP is documented to work  --     ... .
*  see Command Syntax Reference article on DO REPEAT   ... .


*  LOOP within DO REPEAT                               ... .

NEW FILE.
INPUT PROGRAM.

NUMERIC A B C D E (F2).
RECODE  A B C D E (ELSE=1).

END CASE.
END FILE.
END INPUT PROGRAM.

VECTOR NUMBER=A TO E.

DO REPEAT FACTOR = 2 3 5.
.  LOOP #IDX = 1 TO 5.
.     COMPUTE NUMBER(#IDX) = NUMBER(#IDX) * FACTOR.
.  END LOOP..
END REPEAT.

LIST.

*  Declarative statements within DO REPEAT             ... ,

NEW FILE.
INPUT PROGRAM.
.  NUMERIC RecdNum (F2).
.  COMPUTE RecdNum=1.
.  END CASE.
END FILE.
END INPUT PROGRAM.

DO REPEAT NewVar = V1 TO V5.
.  NUMERIC NewVar (F2).
END REPEAT.

LIST.

===================== 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
|

Macros and Variable Values

Krinsky, Alan-2
In reply to this post by Richard Ristow
Would anyone be able to tell me if the following is possible in a macro?
And if so, how? I will simplify what I aim to do. What I am actually
working on is more complicated and would involve additional
substitutions.

I think my key question is this: how do I have a macro loop through
different values of a single variable? I realize this should be
straightforward, but all of the resources I have sought online have not
quite made it clear to me, especially the relationship among macros,
arguments, and tokens. A variable with many values is which of these?

Basically, I am trying to pull data. In this example, I want to create
one file for each membership year-month. That is, I want to know who is
a member in each month. So, let's say I have 6 year-months: 200909
200908 200907 200906 200905 200904. Doesn't even need to be a date
variable.

What I want to be able to do is use something like the following code,
where !yrmonth is my macro (or is it my argument? or token?) for the 6
year-months. As you can see the !yrmonth needs replacing in 2 places, in
the GET DATA command and again in the SAVE OUTFILE command. Can I place
this inside a DEFINE...!ENDDEFINE macro, so that it will produce 6 saved
files, each with a member list for that particular month? Where do I
locate the values for the variable? Inside the macro? After it? Before?



GET DATA
  /TYPE=ODBC
  /CONNECT='DSN=datawarehouse1;Description=datawarehouse1;UID=;APP=SPSS
For
Windows;WSID=GN1MGCW05;DATABASE=datawarehouse1;Trusted_Connection=Yes'
  /SQL="SELECT idnumber, firstname, lastname, gender, dob, membermonth "
           +"FROM dbo.Members WHERE membermonth=!yrmonth"
  /ASSUMEDSTRWIDTH=255.
SAVE OUTFILE='\\database\membership\members in !yrmonth.sav'
  /COMPRESSED.


Thank you for any help, and be well.

Alan



"If people don't want to come out to the ballpark, how are you going to
stop them?"
"I always thought that record would stand until it was broken."
-Yogi Berra

Alan D. Krinsky  PhD, MPH
Medical Management Interventions Manager
UMass Memorial Health Care
Hahnemann Campus
281 Lincoln St.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail: [hidden email]
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential and/or
privileged material.  Any review, transmission, re-transmission,
dissemination or other use of, or taking of any action in reliance
upon this information by persons or entities other than the
intended recipient is prohibited.  If you received this in error,
please contact the sender and delete the material from any
computer.

=====================
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
|

SPSS books

Gary Oliver-3
Hi All

A while back a few list members were soliciting recommendations for SPSS books

Did that end up on a web site anywhere?

Warm regards/gary

=====================
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: Macros and Variable Values

Albert-Jan Roskam
In reply to this post by Krinsky, Alan-2
Hi,

If you want to hard-code the individual values, you could use !do (!value !in !values)- !doend, where !values is the argument (you could use !cmdend). But if you have lots of values or the values change, you could programmatically build a list of the unique values, like the program below does. It's untested as I'm on Linux = no Spss. You only need to change "your by var here" to the name of your grouping variable. If the values contain signs that are not supported in windows file names (slashes, etc.), the program will crash.

import spss, spssdata
def get_distinct_values(by_var):
  data = spssdata.Spssdata(indexes=(by_var))
  distinct_values = []
  for row in data:
    value = eval("row." + by_var)
    if value not in distinct_values:
      distinct_values.append(value)
  return distinct_values

cmd = r"""
GET DATA
  /TYPE=ODBC
  /CONNECT='DSN=datawarehouse1;Description=datawarehouse1;UID=;APP=SPSS
   For Windows;WSID=GN1MGCW05;DATABASE=datawarehouse1;Trusted_Connection=Yes'
  /SQL="SELECT idnumber, firstname, lastname, gender, dob, membermonth "
           +"FROM dbo.Members WHERE membermonth=%s"
  /ASSUMEDSTRWIDTH=255.
SAVE OUTFILE='\\database\membership\members in %s.sav'
  /COMPRESSED.
"""

distinct_values = get_distinct_values(by_var = "your_by_var_here")
for distinct_value in distinct_values:
  spss.Submit(cmd.replace("%s", distinct_value))


 

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 2/11/10, Krinsky, Alan <[hidden email]> wrote:

From: Krinsky, Alan <[hidden email]>
Subject: [SPSSX-L] Macros and Variable Values
To: [hidden email]
Date: Thursday, February 11, 2010, 11:36 PM

Would anyone be able to tell me if the following is possible in a macro?
And if so, how? I will simplify what I aim to do. What I am actually
working on is more complicated and would involve additional
substitutions.

I think my key question is this: how do I have a macro loop through
different values of a single variable? I realize this should be
straightforward, but all of the resources I have sought online have not
quite made it clear to me, especially the relationship among macros,
arguments, and tokens. A variable with many values is which of these?

Basically, I am trying to pull data. In this example, I want to create
one file for each membership year-month. That is, I want to know who is
a member in each month. So, let's say I have 6 year-months: 200909
200908 200907 200906 200905 200904. Doesn't even need to be a date
variable.

What I want to be able to do is use something like the following code,
where !yrmonth is my macro (or is it my argument? or token?) for the 6
year-months. As you can see the !yrmonth needs replacing in 2 places, in
the GET DATA command and again in the SAVE OUTFILE command. Can I place
this inside a DEFINE...!ENDDEFINE macro, so that it will produce 6 saved
files, each with a member list for that particular month? Where do I
locate the values for the variable? Inside the macro? After it? Before?



GET DATA
  /TYPE=ODBC
  /CONNECT='DSN=datawarehouse1;Description=datawarehouse1;UID=;APP=SPSS
For
Windows;WSID=GN1MGCW05;DATABASE=datawarehouse1;Trusted_Connection=Yes'
  /SQL="SELECT idnumber, firstname, lastname, gender, dob, membermonth "
           +"FROM dbo.Members WHERE membermonth=!yrmonth"
  /ASSUMEDSTRWIDTH=255.
SAVE OUTFILE='\\database\membership\members in !yrmonth.sav'
  /COMPRESSED.


Thank you for any help, and be well.

Alan



"If people don't want to come out to the ballpark, how are you going to
stop them?"
"I always thought that record would stand until it was broken."
-Yogi Berra

Alan D. Krinsky  PhD, MPH
Medical Management Interventions Manager
UMass Memorial Health Care
Hahnemann Campus
281 Lincoln St.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail: alan.krinsky@...
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential and/or
privileged material.  Any review, transmission, re-transmission,
dissemination or other use of, or taking of any action in reliance
upon this information by persons or entities other than the
intended recipient is prohibited.  If you received this in error,
please contact the sender and delete the material from any
computer.

=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@... (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: SPSS books

John F Hall
In reply to this post by Gary Oliver-3
Gary
 
Depends what  you're looking for.  Books to learn SPSS or books to learn statistics (using SPSS). 
 
I did a bit of web-searching a couple of years ago and discovered a plethora of available texts (and some good down-loadable tutorials) for learning and using SPSS (15 titles from Allyn & Bacon alone).  There was so much material around it was difficult to know where to start.  I picked out the ones which seem to be aimed at (almost) beginners or which use data derived from questionnaire surveys or similar.
 
I've got some review copies that I'm working on for the (UK) Social Research Association.  To date the list includes:
 
Darren George and Paul Mallery, SPSS for Windows Step-by-Step, (6th edition, Allyn & Bacon 2006)
 
Andy Field Discovering Statistics Using SPSS (Sage, 2005: paper ISBN 978-0-7619-4452-2)
 
George Argyrous Statistics for Research (Sage, 2005: paper ISBN 978-1-4129-1948-7)
 
Eric L Einspruch An Introductory Guide to SPSS for Windows (Sage, 2005: paper ISBN 978-1-4129-0415-5)
 
Neil Salkind Statistics for People Who (Think They Hate) Statistics (Sage, 2004: ISBN 978-0-7619-2776-1)
 
Eelko Huizingh Applied Statistics with SPSS (Sage, 2007: paper ISBN 978-1-4129-1931-9)
 
Julie Pallant, SPSS Survival Manual; A Step by Step Guide to data Analysis using SPSS for Windows (3rd edition, 2007, McGraw Hill/Open University Press)
 
Paul R Kinnear and Colin D Gray SPSS 14 Made Simple (Psychology Press 2006: paper ISBN 1-84169-651-X)
 
Jacqueline Collier Using SPSS Syntax: a Beginner's Guide (Sage, 2010)
 
Marija Norusis also sent me a copy of her book Guide to Data Analysis for SPSS13 (Prentice-Hall, 2005)
 
There are now new editions of some of these
 
At this rate, we'll need a survey to see who uses SPSS and what for: we'll end up doing the Which? Guide to SPSS books and perhaps come up with a best buy!  Perhaps we should organise a symposium thread and do collective reviews of the lot.
 
There are downloadable tutorials all over the place:
 
Raynald Levesque's SPSSTools, Bruce Weaver's site and the hour long introductory video by Tom McFarland at Nova Southeastern Univ. http://tegrity1.acast.nova.edu/tegrityUtils/GetCourseListing.aspx?Session_Info=D7NKe874TJcRsn1229fNKud6kRVilVqgqFkv4XQsk0J7kYJwH/MOMyVAn6srRFhD
 
Most of the books and tutorials approach SPSS via the drop-down menus: few if any include syntax, except indirctly via PASTE.  Many are basically statistics texts in various fields (psychology, medicine, [social] statistics) and mainly contain decriptive and/or inferential statistics.  Few deal with file design and construction or tabulation, specifically with data from questionnaire surveys (which was what SPSS was used for when it first came out).
 
The best book I ever used was Norusis (various editions 1988-1991) which followed the logic and chronology of the research process and fitted the SPSS round that (in syntax).  The latest book is totally different and uses mainly the menus. 
 
Pallant, a deserved best-seller, is very good on process, presentation and statistics, but is aimed at postgraduate psychologists desperately trying to write theses on their own or with inadequate supervision, and has virtually no syntax or tabulation.  Swathes of SPSS facilities are completely missing.  Needs to be used in conjunction with other texts.  See my reviews of the 1st and 2nd editions on http://surveyresearch.weebly.com/8-reviews-of-spss-text-books.html
 
Collier is really a handbook for people already familiar with menus, but attempting to wean them over to syntax, which is far easier and quicker to use and has more facilities than those available via the menus.  It's the only one to deal with dates.
 
Field is by far the best book combining statistics and SPSS.
 
Einspruch is a nice little book for beginners and includes a fair bit of syntax (but only via PASTE)
 
Kinnear and Gray is a heavy duty statistics book by guys who know their way round SPSS.  It's comprehensive, detailed and the examples are mainly medical.
 
Huizingh is a much easier read, suitable for beginners and very easy to use.
 
Salkind is very student friendly and beautifully written
 
Argyrous has probably the most honest title, deals with much bigger, and real, data sets.  Some of the most useful stuff is on a CD instead of in the book, and he's obviously been round SPSS a lot.
 
George and Mallery has cursory statistical explanations, constantly referring to other texts, and has constant references to the SPSS manual.  It also has irritating repetitions for every procedure of how to open SPSS.  From memory it may be the only one to cover Discriminant Ananlysis, Cluster Analysis and Multi-Dimensional Scaling.
 
Given modern technology, all the above could be improved with colour text and graphics.
 
None of the above start from absolute scratch with where data come from in the first place (including problem formulation and research design) and none approach SPSS in syntax only.  For syntax readers are referred to the tutorials for Survey Analysis Workshop http://surveyresearch.weebly.com/survey-analysis-workshop-and-spss.html on my website.  These are based on the postgrad course I designed and taught (using SPSS-X 8 on a Vax cluster) from 1976 to 1992 when I took early retirement.  I never used artificial data: all data come from real surveys on real people.  They have been greatly expanded and updated for SPSS 15 and comprise gentle, step-by-step (with screenshots at each step) demonstrations using colour text and colour graphics, each step worked in both syntax and drop-down menus.  There are many more tutorials for later stages to write and upload, but if you work through what's already there, you should be able to move easily to the above texts.  You might even become fluent in the SPSS command language and come over to the syntax camp!
 
If you want to know a bit more about the early history of SPSS, and see some comparisons of syntax with menus, or inexperienced use of SPSS, check out the Old Dog, Old tricks presentation and accompanying slide-shows I prepared for the ASSESS (SPSS users) annual get-together at York University in 2006.  http://surveyresearch.weebly.com/7-old-dog-old-tricks.html
 
All materials on my siteare available for free download.
 
John Hall
 
 
----- Original Message -----
Sent: Friday, February 12, 2010 5:54 AM
Subject: SPSS books


Hi All

A while back a few list members were soliciting recommendations for SPSS books

Did that end up on a web site anywhere?

Warm regards/gary

=====================
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: SPSS books

John F Hall

Paulette

 

In addition to books, these are by far the best general sites I have found. 

 

Bruce Weaver's, http://sites.google.com/a/lakeheadu.ca/bweaver/Home/statistics/spss and  

http://sites.google.com/a/lakeheadu.ca/bweaver/Home/statistics/spss/my-spss-page

 

Raynald Levesque's http://www.spsstools.net/ 

 

I'm preparing a page for my website listing other sites with entry-level tutorials, some of which even contain syntax.  The best site I ever found for this was by someone based in the computer centre at a university in Turkey, but it's no longer available.  It would be nice to have look-up table for people to call up and indicate their field (statistics, education, survey research, medicine etc.) and level (novice, some experience, expert) from which they could be directed to the most suitable materials.

 

In spite of my dislike of drop-down menus (I'm fighting a losing battle with modern point-and-click students) the best book is still the one by Marija Norusis http://www.norusis.com/   The book she sent me is for 13, my current SPSS is version15, but there's already a book for 17

(USA:  http://www.pearsonhighered.com/educator/product/SPSS-170-Guide-to-Data-Analysis/9780321621436.page 

(UK: http://www.amazon.co.uk/SPSS-17-0-Guide-Data-Analysis/dp/0321621433)

and no doubt 18 in preparation.

 

For beginners, there's always Help (my version displays blank panes if I click on Tutorials or Case studies so I have to go to Topics  and click here under the appropriate heading) or the Help inside the drop-down menus. 

 
For syntax fans Help... Command syntax reference is useful for me, but a tad daunting for beginners.

 

John Hall

----- Original Message -----
Sent: Friday, February 12, 2010 8:06 PM
Subject: Re: SPSS books

I have taught SPSS/statistics from Using SPSS for Window: Analyzing and Understanding Data, 5th Edition, Green and Salkind
for years.  Very little syntax, but good for graduate students who are writing theses/dissertations.  Each Lesson is one stat. technque and has datasets worked out and written up in APA style.

Paulette Johnson, PhD
Statistical Consulting
Florida International University
Miami, Florida 33199
305 348 2261
www.fiu.edu/~statcon


John F Hall wrote:
Gary
 
Depends what  you're looking for.  Books to learn SPSS or books to learn statistics (using SPSS). 
 
I did a bit of web-searching a couple of years ago and discovered a plethora of available texts (and some good down-loadable tutorials) for learning and using SPSS (15 titles from Allyn & Bacon alone).  There was so much material around it was difficult to know where to start.  I picked out the ones which seem to be aimed at (almost) beginners or which use data derived from questionnaire surveys or similar.
 
I've got some review copies that I'm working on for the (UK) Social Research Association.  To date the list includes:
 
Darren George and Paul Mallery, SPSS for Windows Step-by-Step, (6th edition, Allyn & Bacon 2006)
 
Andy Field Discovering Statistics Using SPSS (Sage, 2005: paper ISBN 978-0-7619-4452-2)
 
George Argyrous Statistics for Research (Sage, 2005: paper ISBN 978-1-4129-1948-7)
 
Eric L Einspruch An Introductory Guide to SPSS for Windows (Sage, 2005: paper ISBN 978-1-4129-0415-5)
 
Neil Salkind Statistics for People Who (Think They Hate) Statistics (Sage, 2004: ISBN 978-0-7619-2776-1)
 
Eelko Huizingh Applied Statistics with SPSS (Sage, 2007: paper ISBN 978-1-4129-1931-9)
 
Julie Pallant, SPSS Survival Manual; A Step by Step Guide to data Analysis using SPSS for Windows (3rd edition, 2007, McGraw Hill/Open University Press)
 
Paul R Kinnear and Colin D Gray SPSS 14 Made Simple (Psychology Press 2006: paper ISBN 1-84169-651-X)
 
Jacqueline Collier Using SPSS Syntax: a Beginner's Guide (Sage, 2010)
 
Marija Norusis also sent me a copy of her book Guide to Data Analysis for SPSS13 (Prentice-Hall, 2005)
 
There are now new editions of some of these
 
At this rate, we'll need a survey to see who uses SPSS and what for: we'll end up doing the Which? Guide to SPSS books and perhaps come up with a best buy!  Perhaps we should organise a symposium thread and do collective reviews of the lot.
 
There are downloadable tutorials all over the place:
 
Raynald Levesque's SPSSTools, Bruce Weaver's site and the hour long introductory video by Tom McFarland at Nova Southeastern Univ. http://tegrity1.acast.nova.edu/tegrityUtils/GetCourseListing.aspx?Session_Info=D7NKe874TJcRsn1229fNKud6kRVilVqgqFkv4XQsk0J7kYJwH/MOMyVAn6srRFhD
 
Most of the books and tutorials approach SPSS via the drop-down menus: few if any include syntax, except indirctly via PASTE.  Many are basically statistics texts in various fields (psychology, medicine, [social] statistics) and mainly contain decriptive and/or inferential statistics.  Few deal with file design and construction or tabulation, specifically with data from questionnaire surveys (which was what SPSS was used for when it first came out).
 
The best book I ever used was Norusis (various editions 1988-1991) which followed the logic and chronology of the research process and fitted the SPSS round that (in syntax).  The latest book is totally different and uses mainly the menus. 
 
Pallant, a deserved best-seller, is very good on process, presentation and statistics, but is aimed at postgraduate psychologists desperately trying to write theses on their own or with inadequate supervision, and has virtually no syntax or tabulation.  Swathes of SPSS facilities are completely missing.  Needs to be used in conjunction with other texts.  See my reviews of the 1st and 2nd editions on http://surveyresearch.weebly.com/8-reviews-of-spss-text-books.html
 
Collier is really a handbook for people already familiar with menus, but attempting to wean them over to syntax, which is far easier and quicker to use and has more facilities than those available via the menus.  It's the only one to deal with dates.
 
Field is by far the best book combining statistics and SPSS.
 
Einspruch is a nice little book for beginners and includes a fair bit of syntax (but only via PASTE)
 
Kinnear and Gray is a heavy duty statistics book by guys who know their way round SPSS.  It's comprehensive, detailed and the examples are mainly medical.
 
Huizingh is a much easier read, suitable for beginners and very easy to use.
 
Salkind is very student friendly and beautifully written
 
Argyrous has probably the most honest title, deals with much bigger, and real, data sets.  Some of the most useful stuff is on a CD instead of in the book, and he's obviously been round SPSS a lot.
 
George and Mallery has cursory statistical explanations, constantly referring to other texts, and has constant references to the SPSS manual.  It also has irritating repetitions for every procedure of how to open SPSS.  From memory it may be the only one to cover Discriminant Ananlysis, Cluster Analysis and Multi-Dimensional Scaling.
 
Given modern technology, all the above could be improved with colour text and graphics.
 
None of the above start from absolute scratch with where data come from in the first place (including problem formulation and research design) and none approach SPSS in syntax only.  For syntax readers are referred to the tutorials for Survey Analysis Workshop http://surveyresearch.weebly.com/survey-analysis-workshop-and-spss.html on my website.  These are based on the postgrad course I designed and taught (using SPSS-X 8 on a Vax cluster) from 1976 to 1992 when I took early retirement.  I never used artificial data: all data come from real surveys on real people.  They have been greatly expanded and updated for SPSS 15 and comprise gentle, step-by-step (with screenshots at each step) demonstrations using colour text and colour graphics, each step worked in both syntax and drop-down menus.  There are many more tutorials for later stages to write and upload, but if you work through what's already there, you should be able to move easily to the above texts.  You might even become fluent in the SPSS command language and come over to the syntax camp!
 
If you want to know a bit more about the early history of SPSS, and see some comparisons of syntax with menus, or inexperienced use of SPSS, check out the Old Dog, Old tricks presentation and accompanying slide-shows I prepared for the ASSESS (SPSS users) annual get-together at York University in 2006.  http://surveyresearch.weebly.com/7-old-dog-old-tricks.html
 
All materials on my siteare available for free download.
 
John Hall
 
 
----- Original Message -----
Sent: Friday, February 12, 2010 5:54 AM
Subject: SPSS books


Hi All

A while back a few list members were soliciting recommendations for SPSS books

Did that end up on a web site anywhere?

Warm regards/gary

=====================
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