Home > front end >  How can I create a new variable in SPSS that contains the lowest number out of 6 variables?
How can I create a new variable in SPSS that contains the lowest number out of 6 variables?

Time:03-13

I have 6 age variables (agek1..agek6) and need to create a new variable that contains the value of the lowest age of all those 6 variables. This new variable agejk should therefore only contain the age of the youngest child.

The age variables are not ordered by age, because sometimes the youngest age was written in agek1 and sometimes in one of the other age variables. The age variables agek2..agek6 also contain missings (999), because some people only had one child.

I have tried different solutions in the SPSS syntax, but none really worked sufficiently. Here is one of the solutions I tried for the case when the youngest age was written in agek1:

Do iF (AgeK1  LT AgeK2) AND (AgeK3= 999) OR (AgeK4= 999) OR (AgeK5= 999) OR (AgeK6= 999) .
     Recode Agejk (Else= copy) INTO AgeK1.
End if.

I've also tried the IF-function:

IF AgeK1  LT AgeK2 AND AgeK1 LT AgeK3 AND AgeK1 LT AgeK4 AND AgeK1 LT AgeK5 AND AgeK1 LT AgeK6
     Agejk EQ AgeK1.
IF AgeK2 LT AgeK1  AND AgeK2 LT AgeK3 AND AgeK2 LT AgeK4 AND AgeK2 LT AgeK5 AND AgeK2 LT AgeK6
     Agejk EQ AgeK2.
IF AgeK3 LT AgeK1  AND AgeK3 LT AgeK2  AND AgeK3 LT AgeK4 AND AgeK3 LT AgeK5 AND AgeK3 LT AgeK6
     Agejk EQ AgeK3.

Thanks a lot for your help!

CodePudding user response:

What you need is the min function:

compute Agejk = min(agek1 to agek6).

Note that if the age variables are not consecutive in the dataset, you'll need to white it this way:

compute Agejk = min(agek1, agek2, agek3, agek4, agek5, agek6).
  • Related