注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

零售创新,创新那些事儿,SPSS,VBA

零售创新

 
 
 

日志

 
 
关于我

新浪微博,零售创新 研究经理,数据分析师 希望和市场研究和零售业的同事共同进步! 本博客发表的都是免费或试用的资料,如果有版权问题请发邮件wangli12a@163.com联系删除。 spss excel vba blog

网易考拉推荐

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

2010-08-24 18:27:57|  分类: spss学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 

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

 

from http://groups.google.com.hk/group/comp.soft-sys.stat.spss/browse_thread/thread/f2409c11a92fb335?hl=en

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).

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.

 

 

  评论这张
 
阅读(554)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017