Re: Randomly recoding cases within 30 variables (according to counts within those variables)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Randomly recoding cases within 30 variables (according to counts within those variables)

Joe Murray-3
Raynald Levesque extremely kindly wrote the following solution to this
problem. Thanks Ray!!

CD "C:\Users\Joe\Documents\CURRENT WORK\PYS work and APA book
\Raymond's help".


DATA LIST LIST / var1 TO var5.
BEGIN DATA
 ,0, , ,
1,0, , ,
 , ,1,0,1
 , ,0,0,
0,1, , ,
 , , ,1,
 , ,1, ,
0,0, , ,0
 ,1, ,1,
 ,0, , ,
END DATA.
COMPUTE ID=$CASENUM.
SAVE OUTFILE="data.sav".


DEFINE !doVars(vars=!CMDEND)


!DO !var !IN (!vars)
DATASET CLOSE ALL.
NEW FILE.
GET FILE="data.sav" /KEEP=ID !var.
COMPUTE is0= (!var=0).
COMPUTE is1= (!var=1).
COMPUTE draw=UNIFORM(1).
AGGREGATE
  /nb0 nb1 =SUM(is0 is1).
DO IF nb0>nb1.
  RECODE !var (1=0)(0=1).
END IF.
SORT CASES !var (D).
IF $CASENUM LE ABS(nb0 - nb1) !var=$SYSMIS.
SORT CASES BY ID.
DO IF nb0>nb1.
  RECODE !var (1=0)(0=1).
END IF.
SAVE OUTFILE=!QUOTE(!CONCAT(!var,".sav")) /KEEP=ID !var.
!DOEND


MATCH FILES !DO !var !IN (!vars) /FILE=!QUOTE(!CONCAT(!var,".sav")) !
DOEND
                        /BY=ID.
TITLE Frequencies after replacement of the required number of values.
FREQ VAR=!vars.
SAVE OUTFILE="final data file.sav".
!ENDDEFINE.


SET MPRINT=YES /PRINTBACK=YES.
!doVars vars=var1 var2 var3 var4 var5.


On Thu, 25 Feb 2010 15:43:55 -0500, Joe Murray <[hidden email]> wrote:

>In a data set with 1000 cases and 30 variables (all with values of 1, 0,
>and missing), I want to recode a random selection of 1's and 0's so that,
>WITHIN EACH VARIABLE, the count of 1's equals the count of 0's.
>
>I have only a limited knowledge of SPSS syntax and can't figure out how to
>do this. I would be so grateful for any advice.
>
>The data look something like the following (except with 1,000 cases and 30
>variables).
>
>As in the real data set, in this small one, the difference in the count of
>0's and 1's varies across variables.
>In var1, there are two 0's and one 1 (therefore one randomly selected 0
>should be recoded to missing).
>In var2, there are four 0's and two 1's (so two randomly selected 0's
>should be recoded to missing).
>In var3, there is one 0 and two 1's (so one randomly selected 1 should be
>recoded to missing).
>
>DATA LIST LIST / var1 TO var5.
>BEGIN DATA
> ,0, , ,
>1,0, , ,
> , ,1,0,1
> , ,0,0,
>0,1, , ,
> , , ,1,
> , ,1, ,
>0,0, , ,0
> ,1, ,1,
> ,0, , ,
>END DATA.
>
>I look forward to any suggestions for how to do this! Thank you!
>
>=====================
>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