I spent hours searching the net and reading tutorials, but I simply didn't find a solution for my problem. Maybe somebody here is able to help me out or at least point me in the right direction. I hope I will be able to explain my problem in an easy to understand way (I am not native English, so my apologies for any mistakes).

The Situation:

I faced myself with a task where I needed to manually calculate 350 different variables in SPSS so I decided to put in the effort and try and learn coding Macros and save a lot of time in the future. I am a newbie to programming, though, and ultimately I wasn't able to elaborate a working macro to solve my problem.

I have three variables in a dataset that I need to do some calculations with:

**Agglo00_No**stores numeric values for categories ranging from 1 to 50.

**st_typ**stores numeric values for categories ranging from 1 to 3.

**METERS**stores travel distances as numeric values.

Now, I would like to create a new variable for each combination of the categories (stored in the first two variables) which leads to having to calculate 350 new variables.

The Problem:

With my knowledge at the moment, I would have to calculate the variables manually. This is not a very dynamic and a very tedious task to do, so I decided to (try and) use a macro. The way I would code the syntax manually is as follows:

Code: Select all

```
DATASET ACTIVATE segmente_agglo.
IF (Agglo00_No = 1 & st_typ = 1) a01_1=METERS.
IF (Agglo00_No = 2 & st_typ = 1) a02_1=METERS.
.
.
IF (Agglo00_No = 1 & st_typ = 2) a01_2=METERS.
IF (Agglo00_No = 2 & st_typ = 2) a02_2=METERS.
.
.
.
and so on...
```

After hours of testing different options, reading reference manuals and tutorials and browsing the web I coded the following macro to do the above procedure 350 times:

Macro code:

Code: Select all

```
DEFINE PW_Agglo_ST_Typ (AggloNo = !TOKENS(1) / St_Typ = !TOKENS(1) / meters = !TOKENS(1))
!DO !I = 1 !TO 50
!IF (!St_Typ = 1) !THEN !LET !varname = !CONCAT ('a','0',!I,'_',!St_Typ).
!LET !t = !meters.
RENAME VARIABLES (t = !varname).
!IFEND
!IF (!St_Typ = 2) !THEN !LET !varname = !CONCAT ('a','0',!I,'_',!St_Typ).
!LET !t = !meters.
RENAME VARIABLES (t = !varname).
!IFEND
!IF (!St_Typ = 3) !THEN !LET !varname = !CONCAT ('a','0',!I,'_',!St_Typ).
!LET !t = !meters.
RENAME VARIABLES (t = !varname).
!IFEND
!DOEND
!ENDDEFINE.
```

Code: Select all

```
SET MPRINT ON.
PW_Agglo_ST_Typ AggloNo = Aglo00_No St_Typ = st_typ meters = METERS.
SET MPRINT OFF.
```

But when I run the script, nothing happens. Although there are no errors displayed, there is also no result to be seen. The output looks like this:

Code: Select all

```
DEFINE PW_Agglo_ST_Typ (AggloNo = !TOKENS(1) / St_Typ = !TOKENS(1) / meters = !TOKENS(1))
!DO !I = 1 !TO 50
!IF (!St_Typ = 1) !THEN !LET !varname = !CONCAT ('a','0',!I,'_',!St_Typ).
!LET !t = !meters.
RENAME VARIABLES (t = !varname).
!IFEND
!IF (!St_Typ = 2) !THEN !LET !varname = !CONCAT ('a','0',!I,'_',!St_Typ).
!LET !t = !meters.
RENAME VARIABLES (t = !varname).
!IFEND
!IF (!St_Typ = 3) !THEN !LET !varname = !CONCAT ('a','0',!I,'_',!St_Typ).
!LET !t = !meters.
RENAME VARIABLES (t = !varname).
!IFEND
!DOEND
!ENDDEFINE.
SET MPRINT ON.
PW_Agglo_ST_Typ AggloNo = Agglo00_No St_Typ = st_typ meters = METERS.
708 0 M>
709 0 M> .
710 0 M>
711 0 M> .
SET MPRINT OFF.
712 0 M> SET MPRINT OFF.
```

Thank you very much in advance!

Sincerely,

Jonas Bley