How can I iterate over variables in SPSS? I want to avoid code duplication

Is there a “native” SPSS way to scroll through variable names? All I want to do is take a list of variables (which I define) and follow the same procedure for them:

pseudocode is not a very good example, but gets the point at ...

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

I noticed that people seem to just use the R or Python SPSS plugins to achieve this core array functionality, but I don't know how soon I can get these settings (if ever) when installing SPSS.

SPSS should have some own way of doing this ... right?

+4
source share
7 answers

There are two simple solutions for looping through variables (easier than using Python in SPSS).

1) DO REPEAT-END REPEAT

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

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

2) DEFINE-!ENDDEFINE (macro)

You can do frequencies in a loop of variables with a macro. For instance:

 DEFINE macdef (!POS !CHAREND('/')) !DO !i !IN (!1) frequencies variables = !i. !DOEND !ENDDEFINE. macdef VAR1 VAR2 VAR3 /. 
+6
source

If I understand the question correctly, there is no need to use a loop construct. SPSS commands with the VARIABLES subcommand, such as FREQUENCIES, allow you to specify several variables.

The basic syntax for FREQUENCIES:

 FREQUENCIES VARIABLES= varlist [varlist...] 

where [varlist] is one variable name, several variable names with spaces delimited, a range of consecutive variables specified with the TO keyword, the ALL keyword, or a combination of the previous parameters.

For instance:

 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. The syntax for the commands is available at http://support.spss.com/ProductsExt/SPSS/Documentation/SPSSforWindows/index.htm

Please note that many years have passed since I used SPSS.

+5
source

It is more efficient to run all of these frequencies in a single pass of data, for example, FREQUENCIES a to c. but Python allows looping and many other control flow tricks.

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

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

You can, of course, use macros for this kind of thing, but Python is much more powerful and simpler when you get it.

+1
source

Yes, SPSS can do this. It seems like the guys from UCLA are using python because they know how to do this in python and not in SPSS. :)

Let me name the variables VARA, VARB, VARC. They must be numerical (as you make frequencies) and they must be sequential in your spss data file. Then you create a vector that says essentially: "Here is a series of variables that I want to skip."

 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. Perhaps something is missing, etc.)

+1
source

Here is a page from the UCLA Academic Technology Services that describes a loop through variable lists . Quote,

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

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

Er ... sorry for being a guy, but maybe it's time to switch to another statistics system.

0
source

I didn’t use SPSS macros very often, but maybe they can get you where you need to be? Check out this site for some examples:

http://spsstools.net/Macros.htm

In addition, the SPSS data management book can help.

Finally, if memory is working, I think the problem might even be a basic example of how to use Python inside SPSS syntax. I only used Python and SPSS a few times, but it’s very convenient to have this language if necessary.

NTN

0
source

How to do 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.

-2
source

Source: https://habr.com/ru/post/1310545/


All Articles