遍历一组共享相同前缀的变量

Looping through a set of variables sharing the same prefix

我经常处理学生考试文件,其中对考试项目的每个回答都以分数记录。我想将该变量转换为 1 或 0(有效地将每个项目减少为正确或不正确)。

每个数据集都有相同的命名法,其中变量以 points_ 为前缀,然后是标识号(例如,points_18616)。我正在使用以下语法:

RECODE points_18616 (0=Copy) (SYSMIS=SYSMIS) (ELSE=1) INTO Binary_18616.
VARIABLE LABELS  Binary_18616 'Binary Conversion of Item_18616'.
EXECUTE.

所以我最终为每个变量创建了这种语法,并且由于每个数据集都不同,所以它变得乏味。有没有办法遍历数据集并对所有前缀为 points_ 的变量执行此转换?

这里有一个方法:

首先,我将创建一些假数据来演示:

data list list/points_18616 points_18617 points_18618 points_18619 (4f2).
begin data
4 5 6 7
5 6 7 8
6 7 8 9
7 8 9 9
end data.

* the following code will create a list of all the relevant variables in a new file.
SPSSINC SELECT VARIABLES MACRONAME="!list" /PROPERTIES  PATTERN = "points_*".

* now we'll use the created list in a macro to loop your syntax over all the vars.    
define !doList ()
!do !lst !in(!eval(!list))
RECODE !lst (0=Copy) (SYSMIS=SYSMIS) (ELSE=1) INTO !concat("Binary", !substr(!lst,7)).
VARIABLE LABELS  !concat("Binary", !substr(!lst,7))  !concat("'Binary Conversion of Item",!substr(!lst,7) ,"'.").
!doend
!enddefine.

!doList.
EXECUTE.