Login  Register

Re: Repeat case equals VAR

Posted by Richard Ristow on Mar 30, 2013; 9:37pm
URL: http://spssx-discussion.165.s1.nabble.com/Repeat-case-equals-VAR-tp5719153p5719202.html

At 04:17 PM 3/28/2013, Peter Spangler wrote:

>My data has 4 Columns. Each column has user IDs in Rows. Some of the
>IDs repeat between VAR1 to VAR3. I would like to assign the names of
>the variables to the cases for which that ID repeats.
>
>
>VAR1 VAR2 VAR3 NEW_VAR
>  1    2    3   VAR1, VAR2
>  0    1    0

I gather from what you write, and later discussion, that your example
here does not represent what you really want; you'd want instead,
with the input you give,

VAR1 VAR2 VAR3 NEW_VAR
  1    2    3
  0    1    0   VAR1, VAR3

If I'm wrong, the following is in vain; if I'm write, I hope it
helps. In the test data below, I've added a variable "Record" which
uniquely identifies the input records; some such record identifier is
essential. (And if you had four or more "VARn" variables, so that
more than one ID could be duplicated, the problem would be
significantly more complicated.)

[TestData]
Record VAR1 VAR2 VAR3

A        1    2    3
B        1    3    3
C        3    2    3
D        3    3   3

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

NEW FILE.
ADD FILES /FILE=TestData.

EXECUTE /* SPSS v.14 glitch: without this, VARSTOCASES */
         /* sees no data                                */.

VARSTOCASES
  /MAKE PersonID FROM VAR1 VAR2 VAR3
  /INDEX   = VarName(PersonID)
  /KEEP    = Record
  /NULL    = DROP.

Variables to Cases

Generated Variables
|--------|------|
|Name    |Label |
|--------|------|
|VarName |<none>|
|PersonID|<none>|
|--------|------|
Processing Statistics
|-------------|-|
|Variables In |4|
|Variables Out|3|
|-------------|-|
DATASET NAME     Calculate.
LIST.

List
[Calculate]
Record VarName PersonID

A      VAR1        1
A      VAR2        2
A      VAR3        3
B      VAR1        1
B      VAR2        3
B      VAR3        3
C      VAR1        3
C      VAR2        2
C      VAR3        3
D      VAR1        3
D      VAR2        3
D      VAR3        3
  Number of cases read:  12    Number of cases listed:  12

SORT CASES BY Record PersonID VarName.
ADD FILES
   /FILE=*
   /BY Record PersonID
   /FIRST=Start
   /LAST =End.

STRING NewVar (A15).

DO IF   Start.
.  COMPUTE NewVar = VarName.
ELSE.
.  COMPUTE NewVar = CONCAT(RTRIM(LAG(NewVar)),
                            ', '              ,
                            VarName           ).
END IF.

*  If the following LIST is removed, it must be         .
*  replaced by EXECUTE, or the SELECT IF that follows   .
*  may fail.                                            .

LIST.

List
[Calculate]
Record VarName PersonID Start End NewVar

A      VAR1        1      1    1  VAR1
A      VAR2        2      1    1  VAR2
A      VAR3        3      1    1  VAR3
B      VAR1        1      1    1  VAR1
B      VAR2        3      1    0  VAR2
B      VAR3        3      0    1  VAR2, VAR3
C      VAR2        2      1    1  VAR2
C      VAR1        3      1    0  VAR1
C      VAR3        3      0    1  VAR1, VAR3
D      VAR1        3      1    0  VAR1
D      VAR2        3      0    0  VAR1, VAR2
D      VAR3        3      0    1  VAR1, VAR2, VAR

Number of cases read:  12    Number of cases listed:  12

SELECT IF End AND NOT Start.

MATCH FILES
    /FILE=TestData
    /FILE=Calculate
    /By   Record
    /DROP=VarName, PersonID, Start, End.

LIST.

List
Record VAR1 VAR2 VAR3 NewVar

A        1    2    3
B        1    3    3  VAR2, VAR3
C        3    2    3  VAR1, VAR3
D        3    3   3  VAR1, VAR2, VAR

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

==================================
APPENDIX: All code, with test data
==================================
*  C:\Documents and Settings\Richard\My Documents   .
*    \Technical\spssx-l\Z-2013\                     .
*    2013-03-28 Spangler-Repeat case equals VAR.SPS .

*  In response to posting                           .
*  Date:    Thu, 28 Mar 2013 13:17:00 -0700         .
*  From:    Peter Spangler <[hidden email]>  .
*  Subject: Repeat case equals VAR                  .
*  To:      [hidden email]                .

*  The language of the posting is opaque, and I won't try to repeat   .
*  it here. I think I'm giving him what he wants.                     .

*  ................................................................. .
*  .................   Test data               ..................... .

DATA LIST LIST/
    Record  VAR1 VAR2 VAR3
    (  A1,   F1,  F1,  F1).
BEGIN DATA
       A      1    2    3
       B      1    3    3
       C      3    2    3
       D      3    3    3
END DATA.
DATASET NAME     TestData.
LIST.

NEW FILE.
ADD FILES /FILE=TestData.

EXECUTE /* SPSS v.14 glitch: without this, VARSTOCASE */
         /* sees no data                               */.

VARSTOCASES
  /MAKE PersonID FROM VAR1 VAR2 VAR3
  /INDEX   = VarName(PersonID)
  /KEEP    = Record
  /NULL    = DROP.

DATASET NAME     Calculate.
LIST.

SORT CASES BY Record PersonID VarName.

ADD FILES
   /FILE=*
   /BY Record PersonID
   /FIRST=Start
   /LAST =End.

STRING NewVar (A15).

DO IF   Start.
.  COMPUTE NewVar = VarName.
ELSE.
.  COMPUTE NewVar = CONCAT(RTRIM(LAG(NewVar)),
                            ', '              ,
                            VarName           ).
END IF.

*  If the following LIST is removed, it must be         .
*  replaced by EXECUTE, or the SELECT IF that follows   .
*  may fail.                                            .

LIST.

SELECT IF End AND NOT Start.

.  /* Debugging only */ LIST.

MATCH FILES
    /FILE=TestData
    /FILE=Calculate
    /By   Record
    /DROP=VarName, PersonID, Start, End.

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