How can I loop through variables in SPSS? I want to avoid code duplication

余生颓废 提交于 2019-12-30 05:11:08

问题


Is there a "native" SPSS way to loop through some variable names? All I want to do is take a list of variables (that I define) and run the same procedure for them:

pseudo-code - not really a good example, but gets the point across...

for i in varlist['a','b','c']
do
  FREQUENCIES VARIABLES=varlist[i] / ORDER=ANALYSIS.
end

I've noticed that people seem to just use R or Python SPSS plugins to achieve this basic array functionality, but I don't know how soon I can get those configured (if ever) on my installation of SPSS.

SPSS has to have some native way to do this...right?


回答1:


There are two easy solutions for looping through variables (easier compared to using Python in SPSS).

1) DO REPEAT-END REPEAT

The draw back is that you can use DO REPEAT-END REPEAT mainly only for data transformations - for example COMPUTE, RECODE etc. Frequencies are not allowed. For example:

DO REPEAT R=REGION1 TO REGION5.
COMPUTE R=0.
END REPEAT.

2) DEFINE-!ENDDEFINE (macro facility)

You can do Frequencies in a loop of variables using macro command. For example:

DEFINE macdef (!POS !CHAREND('/'))
!DO !i !IN (!1)
frequencies variables = !i.
!DOEND
!ENDDEFINE.

macdef VAR1 VAR2 VAR3  /.



回答2:


If I understand the question correctly, there may be no need to use a looping construct. SPSS commands with a VARIABLES subcommand like FREQUENCIES allow you to specify multiple variables.

The basic syntax for the FREQUENCIES is:

FREQUENCIES
    VARIABLES= varlist [varlist...] 

where [varlist] is a single variable name, multiple space-delimited variable names, a range of consecutive variables specified with the TO keyword, the keyword ALL, or a combination of the previous options.

For example:

FREQUENCIES VARIABLES=VARA

FREQUENCIES VARIABLES=VARA VARB VARC

FREQUENCIES VARIABLES=VARA TO VARC     

FREQ VAR=ALL

FREQ VAR=VARA TO VARC VARM VARX TO VARZ

See SPSS Statistics 17.0 Command Syntax Reference available at http://support.spss.com/ProductsExt/SPSS/Documentation/SPSSforWindows/index.htm

Note that it's been years since I've actually used SPSS.




回答3:


It's more efficient to do all these frequencies on one data pass, e.g., FREQUENCIES a to c. but Python lets you do looping and lots of other control flow tricks.

begin program.
import spss
for v in ['a','b','c']:
  spss.Submit("FREQUENCIES " + v)
end program.

Using Python requires installing the (free) Python plugin available from SPSS Developer Central, www.spss.com/devcentral.

You can, of course, use macros for this sort of things, but Python is a lot more powerful and easier once you get the hang of it.




回答4:


Yes, SPSS can do this. Sounds like the guys at UCLA use python 'cause they know how to do it in python and not in SPSS. :)

Let's call your variables VARA, VARB, VARC. They must be numerical (since you are doing frequencies) and they must be consecutive in your spss data file. Then you create a vector saying in effect "here is the series of variables I want to loop through".

VECTOR VectorVar = VarA TO VarC.
LOOP #cnt = 1 to 3 by 1.
    FREQUENCIES VARIABLES=VectorVar(#cnt) / ORDER=ANALYSIS
ENDLOOP.
EXECUTE.

(The above has not been tested. Might be missing a period somewhere, etc.)




回答5:


Here's a page from UCLA's Academic Technology Services that describes looping over lists of variables. Quote,

"Because we are looping through more than one variable, we will need to use Python."

In my experience, UCLA ATS is probably the site with the best coverage of all of the major statistical computing systems. If they say you need Python... you probably need Python.

Er... sorry for being that guy, but maybe it's time to switch to a different stats system.




回答6:


I haven't used SPSS macros very much, but maybe they can get you where you need to be? Check out this site for some examples:

http://spsstools.net/Macros.htm

Also, the SPSS Data Management book may be helpful as well.

Lastly, if memory serves, I think the problem may even be the main example of how to leverage Python inside of SPSS syntax. I have only used Python and SPSS a few times, but it is very handy to have that language accessible if need be.

HTH




回答7:


How can do this stata sintxis for spss.

foreach var of varlist  pob_multi pob_multimod pob_multiex vul_car vul_ing nopob_nov espacio carencias carencias_3 ic_rezedu ic_asalud ic_ss  ic_cv  ic_sbv ic_ali  pobex pob  {
    tabstat `var' [w=factor] if pob_multi!=., stats(mean) save
    matrix define `var'_pp =(r(StatTotal))
    matrix rownames `var'_pp = `var'_pp
}

matrix tabla1 = (pob_multi_pp \ pob_multimod_pp \ pob_multiex_pp \ vul_car_pp \ vul_ing_pp \ nopob_nov_pp \ espacio_pp \ carencias_pp \ carencias_3_pp \ espacio_pp \ ic_rezedu_pp\ ic_asalud_pp \ ic_ss_pp \ ic_cv_pp \ ic_sbv_pp\ ic_ali_pp \ espacio_pp \ pobex_pp \ pob_pp   )
matrix list tabla1

thanks.



来源:https://stackoverflow.com/questions/2884479/how-can-i-loop-through-variables-in-spss-i-want-to-avoid-code-duplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!