SPSS宏可自动执行顺序变量引用 [英] SPSS Macro to Automate Sequential Variable References
问题描述
我很难在不基于变量的SPSS中使用一个简单的循环来工作.
I've had a tough time getting a simple loop to work in SPSS that isn't based on variables.
简而言之,我想写
!sequentialVars varStr=/Var/ i=/20/.
或类似内容并获得: Var1 Var2 Var3 Var4 Var5 ... Var19 Var20 放入cTable或其他需要一串变量名的地方.
or similar and get: Var1 Var2 Var3 Var4 Var5 ... Var19 Var20 to put into a cTable or anywhere else that takes a string of variable names.
在伪VB中,它将是:
varString = "AnyVarName"
for i=1 to 20
newVarList = concatenate(newVarList," ",varString, i)
next i
我什至无法在SPSS循环中回显i,更不用说将其连接了.
I can't even echo back the i in an SPSS loop, let alone concatenate it.
谢谢!
推荐答案
下面的示例演示在宏中创建变量列表.它的作用是从1到n循环,并将数字连接到当前变量(base_i
)的末尾.然后,只需为循环中的每个变量添加X1 + X2 ....
即可.宏使用参数基本变量和项数.
The example below demonstrates making a list of variables within a macro. What it does is loop through 1 to n, and concatenates the number on the end of the current variable (base_i
). Then the X1 + X2 ....
is made by just adding on for every variable through the loop. The macro takes the arguments base variable and the number of items.
*making filler data frame.
data list free / V1 (F1.0).
begin data
1
3
5
end data.
dataset name input.
*making a vector list.
vector X(5,F1.0).
do repeat X = X1 to X5.
compute X = RV.BERNOULLI(0.5).
end repeat.
*what I want to do essentially.
ctables
/table X1 + X2.
*now to demonstrate looping through list.
DEFINE !loop_ctable (base = !TOKENS(1)
/n = !TOKENS(1))
!DO !I = 1 !TO !n
!IF (!I = 1) !then
!LET !base_stub = !concat(!base,"1")
!ELSE
!LET !base_i = !CONCAT(!base,!I)
!LET !base_stub = !concat(!base_stub," + ",!base_i)
!IFEND
!DOEND
ctables
/table !base_stub.
!ENDDEFINE.
set mprint on.
!loop_ctable base = X n = 5.
我可以想出一种遍历字母(至少遍历字母的首字母)的方法,尽管希望这样就足够了.
I can think of a way to loop through letters (at least through the initial letters of the alphabet), although hopefully this suffices.
这篇关于SPSS宏可自动执行顺序变量引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!