Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Randomly recoding cases within 30 variables (according to counts within the variables)

2 views
Skip to first unread message

Joe Murray

unread,
Feb 25, 2010, 3:36:26 PM2/25/10
to
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.00 cases and 30 variables).

I look forward to any suggestions for how to do this! Thank you!

Message has been deleted

Joe Murray

unread,
Feb 27, 2010, 9:49:03 PM2/27/10
to
Raynald Levesque extremely kindly wrote the following solution to
this
problem. Thanks Ray!!

CD "C:/temp".

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.

0 new messages