3 short Questions

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

3 short Questions

Simon Levin
Hi guys.
Great Website!
I'm a beginner in SPSS. I'm wondering if somebody can help me with 3 short
questions. Thank you.

I create a small database:

DATA LIST /iD (F1) Pos (F3) FV 5-50(A).
BEGIN DATA
0 1
1 0 X1 X2 X3
2 0 aggvar1 aggvar2 aggvar3

END DATA.
LIST.
SAVE OUTFILE='c:\SPSS\TEST.sav'.

Q1: How can I copy the value of iD = 1 to iD = 0? I tried with using LEAD,
but it doesn't work:
CREATE FV1 = LEAD(FV).

Q2: When working with a different database, I need to assign to an
AGGREGATE operator a list of variables from the 'c:\SPSS\TEST.sav'
database, as values of FV variable, when iD = 1 and iD =2. The following
doesn't work.
AGGREGATE
  /OUTFILE = file
  /BREAK = varlist
  /aggvar1 aggvar2 aggvar3 = sum (X1 X2 X3).
I also unsuccessfully tried to use DEFINE.
  /!2 = sum (!1).

Q3: How to end a loop on a condition? The following examples don't work:
LOOP #I=1 TO 100.
...
IF (iD=#I AND Pos=0) #I=99.
...
END LOOP IF (Pos=0).

Thank you very much,
All your help and time is greatly appreciated!

=====================
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: 3 short Questions

Richard Ristow
At 01:27 PM 11/11/2007, Simon Levin wrote:

>I'm wondering if somebody can help me with 3 short questions.
>
>I create a small database:

|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 00:12:33       |
|-----------------------------|---------------------------|
[TEST]

iD Pos FV

  0   1
  1   0 X1 X2 X3
  2   0 aggvar1 aggvar2 aggvar3
  .   .
Number of cases read:  4    Number of cases listed:  4
(See code to create this, in APPENDIX to this posting. Did you mean
to include that fourth, blank record?)

>Q1: How can I copy the value of iD = 1 to iD = 0? I tried with using
>LEAD, but it doesn't work:
>CREATE FV1 = LEAD(FV).

No. From the draft output, running the above CREATE:

>Warnings
>|-----------------------------------------------|
>|A string variable was used in a variable list  |
>|where only numeric variables are allowed.      |
>|Found FV                                       |
>|-----------------------------------------------|
>|This command is not executed.                  |
>|-----------------------------------------------|
>|The number of values specified within          |
>|parentheses is less than the number required.  |
>|-----------------------------------------------|
So, it doesn't do anything.

*  One reasonably simple solution is to reverse the order, and use   .
*  LAG:                                                              .

DATASET ACTIVATE TEST.
DATASET COPY     Lag.
DATASET ACTIVATE Lag.

SORT CASES BY iD(D).
STRING  FV1 (A26).
COMPUTE FV1 = LAG(FV).

SORT CASES BY iD.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 00:35:31       |
|-----------------------------|---------------------------|
[Lag]

iD Pos FV                         FV1

  .   .
  0   1                            X1 X2 X3
  1   0 X1 X2 X3                   aggvar1 aggvar2 aggvar3
  2   0 aggvar1 aggvar2 aggvar3

Number of cases read:  4    Number of cases listed:  4

(Notice the blank record is now at the head of the file. I'm guessing
that doesn't matter.)


>Q2: When working with a different database, I need to assign to an
>AGGREGATE operator a list of variables from the 'c:\SPSS\TEST.sav'
>database, as values of FV variable, when iD = 1 and iD =2. The
>following doesn't work.
>AGGREGATE
>   /OUTFILE = file
>   /BREAK = varlist
>   /aggvar1 aggvar2 aggvar3 = sum (X1 X2 X3).

With your TEST data as you gave it, that AGGREGATE can't work:

+ "OUTFILE = file" could be OK, but you might be surprised where the
resulting "file.SAV" ended up.

+ "/BREAK = varlist": 'varlist' isn't meant to be entered literally.
It represents a set of one or more variables that divide the records
into groups. AGGREGATE calculates statistics for each group, where a
'group' is all records having the same values on all variables in 'varlist'.

+ In "/aggvar1 aggvar2 aggvar3 = sum (X1 X2 X3)" -- you don't have
variables in your TEST dataset named X1, X2, and X3; those are values
that occur in one record in the string variable FV.

Sorry to write only that it won't work, instead of giving you
something that will. But I don't understand just what you are trying to do.

If you would, give us a listing of the data you're starting with, and
describe what you'd like the result to be.

>Q3: How to end a loop on a condition? The following examples don't work:
>LOOP #I=1 TO 100.
>...
>IF (iD=#I AND Pos=0) #I=99.
>...
>END LOOP IF (Pos=0).

A couple of things:

+ Modifying the loop counter (#IX) doesn't work. That's a feature of
how SPSS implemented "LOOP". The apparent loop counter isn't the real
one; the real one is hidden. At the start of each loop pass, the
'loop counter' you name on the LOOP statement is given the value of
the hidden counter.

+ "END LOOP IF (Pos=0)" should work. In what way doesn't it?

+ I'm not sure what you want to do, but I hope you're aware: A LOOP
only works *within the processing of one record*. If you're trying to
loop across several records, other techniques are necessary.

Again, here, could you expand what you're giving, so we can see what
you have at the beginning, and what result you want?

-Best of luck,
  Richard Ristow
=================================================
APPENDIX: Test data
-------------------------------------------------
(From the posting, but creating a dataset instead
of a scratch file; and shortening variable FV.)
=================================================
DATA LIST /iD (F1) Pos (F3) FV 5-30(A).
BEGIN DATA
0 1
1 0 X1 X2 X3
2 0 aggvar1 aggvar2 aggvar3

END DATA.
DATASET NAME TEST.
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