Discussion:
Help with Coding Variables with conditional statements!
(too old to reply)
Shiri Bogomolny
2017-03-07 03:36:19 UTC
Permalink
Hi,

I am trying to create a new variable in SPSS using two different additional variables. In Excel it would be a simple if statement (if variable=x pull from this column, otherwise pull form that column) but I can't get it to work here for some reason.

I have a variable that is membership type for a gym (Adult, youth, staff, etc) coded 1-8. I have another variable that is the age range of the member (18-29, 30-54, etc). I want to create a third variable in which if the member is an adult, rather than "adult" I want it to say his age range category. If the member is any other type of member I want it to retain the original membership type. I have coded both membership types and age ranges as values.

This is what I started:

DO IF (MemberCatNumber = 1).
RECODE AgeInMemberRange (30=30) (54=54) (18=18) INTO Final.
END IF.
EXECUTE.
DO IF (MemberCatNumber = 2).
RECODE MemberCatNumber (2 = 2) INTO Final.
END IF.
EXECUTE.
Error # 4070. Command name: END IF
The command does not follow an unclosed DO IF command. Maybe the DO IF
command was not recognized because of an error. Use the level-of-control
shown to the left of the SPSS Statistics commands to determine the range of
LOOPs and DO IFs.
Execution of this command stops.
END IF.

Thanks so much in advance for your support!
Rich Ulrich
2017-03-07 07:13:55 UTC
Permalink
On Mon, 6 Mar 2017 19:36:19 -0800 (PST), Shiri Bogomolny
Post by Shiri Bogomolny
Hi,
I am trying to create a new variable in SPSS using two different additional variables. In Excel it would be a simple if statement (if variable=x pull from this column, otherwise pull form that column) but I can't get it to work here for some reason.
I have a variable that is membership type for a gym (Adult, youth, staff, etc) coded 1-8. I have another variable that is the age range of the member (18-29, 30-54, etc). I want to create a third variable in which if the member is an adult, rather than "adult" I want it to say his age range category. If the member is any other type of member I want it to retain the original membership type. I have coded both membership types and age ranges as values.
Comment Set the missing to a particular Missing value.
COMPUTE Final = -9.
IF (MemberCatNumber = 1) Final= AgeInMemberRange.
IF (MemberCatNumber = 2) Final= MemberCatNumber.

That's all you need, probably. Else, create a temporary variable
like #age to be the object of the RECODE and what you set Final to.

But further...
I never used RECODE after a conditional; I think I remember
that this is now allowed.
Post by Shiri Bogomolny
DO IF (MemberCatNumber = 1).
RECODE AgeInMemberRange (30=30) (54=54) (18=18) INTO Final.
END IF.
EXECUTE.
DO IF (MemberCatNumber = 2).
RECODE MemberCatNumber (2 = 2) INTO Final.
You can't RECODE INTO a variable that already exists.
I would expect your Errors to also mention that. I think
the SPSS parser will only know that Final was "previously"
defined, and not account for the logic that says otherwise.
Post by Shiri Bogomolny
END IF.
EXECUTE.
Error # 4070. Command name: END IF
The command does not follow an unclosed DO IF command. Maybe the DO IF
command was not recognized because of an error. Use the level-of-control
shown to the left of the SPSS Statistics commands to determine the range of
LOOPs and DO IFs.
Execution of this command stops.
END IF.
--
Rich Ulrich
David Marso
2017-03-07 11:25:51 UTC
Permalink
COMPUTE Final=
SUM( (MemberCatNumber EQ 1)*ANY(AgeInMemberRange,30,54,18)*AgeInMemberRange,
(MemberCatNumber EQ 2)*2 ).

If AgeInMemberRange can assume ONLY values 30,54,18 then this simplifies to

COMPUTE Final=
SUM( (MemberCatNumber EQ 1)*AgeInMemberRange,(MemberCatNumber EQ 2)*2 ).
Post by Rich Ulrich
On Mon, 6 Mar 2017 19:36:19 -0800 (PST), Shiri Bogomolny
Post by Shiri Bogomolny
Hi,
I am trying to create a new variable in SPSS using two different additional variables. In Excel it would be a simple if statement (if variable=x pull from this column, otherwise pull form that column) but I can't get it to work here for some reason.
I have a variable that is membership type for a gym (Adult, youth, staff, etc) coded 1-8. I have another variable that is the age range of the member (18-29, 30-54, etc). I want to create a third variable in which if the member is an adult, rather than "adult" I want it to say his age range category. If the member is any other type of member I want it to retain the original membership type. I have coded both membership types and age ranges as values.
Comment Set the missing to a particular Missing value.
COMPUTE Final = -9.
IF (MemberCatNumber = 1) Final= AgeInMemberRange.
IF (MemberCatNumber = 2) Final= MemberCatNumber.
That's all you need, probably. Else, create a temporary variable
like #age to be the object of the RECODE and what you set Final to.
But further...
I never used RECODE after a conditional; I think I remember
that this is now allowed.
Post by Shiri Bogomolny
DO IF (MemberCatNumber = 1).
RECODE AgeInMemberRange (30=30) (54=54) (18=18) INTO Final.
END IF.
EXECUTE.
DO IF (MemberCatNumber = 2).
RECODE MemberCatNumber (2 = 2) INTO Final.
You can't RECODE INTO a variable that already exists.
I would expect your Errors to also mention that. I think
the SPSS parser will only know that Final was "previously"
defined, and not account for the logic that says otherwise.
Post by Shiri Bogomolny
END IF.
EXECUTE.
Error # 4070. Command name: END IF
The command does not follow an unclosed DO IF command. Maybe the DO IF
command was not recognized because of an error. Use the level-of-control
shown to the left of the SPSS Statistics commands to determine the range of
LOOPs and DO IFs.
Execution of this command stops.
END IF.
--
Rich Ulrich
David Marso
2017-03-07 11:28:12 UTC
Permalink
Please note. You DO NOT need those inefficient EXECUTE statements.

Furthermore DO IF, ELSE IF, END IF might be a better way to do this than 2 IF ,END IF blocks.

OTOH, a simple COMPUTE is the way I would do it.
---
Post by David Marso
COMPUTE Final=
SUM( (MemberCatNumber EQ 1)*ANY(AgeInMemberRange,30,54,18)*AgeInMemberRange,
(MemberCatNumber EQ 2)*2 ).
If AgeInMemberRange can assume ONLY values 30,54,18 then this simplifies to
COMPUTE Final=
SUM( (MemberCatNumber EQ 1)*AgeInMemberRange,(MemberCatNumber EQ 2)*2 ).
Post by Rich Ulrich
On Mon, 6 Mar 2017 19:36:19 -0800 (PST), Shiri Bogomolny
Post by Shiri Bogomolny
Hi,
I am trying to create a new variable in SPSS using two different additional variables. In Excel it would be a simple if statement (if variable=x pull from this column, otherwise pull form that column) but I can't get it to work here for some reason.
I have a variable that is membership type for a gym (Adult, youth, staff, etc) coded 1-8. I have another variable that is the age range of the member (18-29, 30-54, etc). I want to create a third variable in which if the member is an adult, rather than "adult" I want it to say his age range category. If the member is any other type of member I want it to retain the original membership type. I have coded both membership types and age ranges as values.
Comment Set the missing to a particular Missing value.
COMPUTE Final = -9.
IF (MemberCatNumber = 1) Final= AgeInMemberRange.
IF (MemberCatNumber = 2) Final= MemberCatNumber.
That's all you need, probably. Else, create a temporary variable
like #age to be the object of the RECODE and what you set Final to.
But further...
I never used RECODE after a conditional; I think I remember
that this is now allowed.
Post by Shiri Bogomolny
DO IF (MemberCatNumber = 1).
RECODE AgeInMemberRange (30=30) (54=54) (18=18) INTO Final.
END IF.
EXECUTE.
DO IF (MemberCatNumber = 2).
RECODE MemberCatNumber (2 = 2) INTO Final.
You can't RECODE INTO a variable that already exists.
I would expect your Errors to also mention that. I think
the SPSS parser will only know that Final was "previously"
defined, and not account for the logic that says otherwise.
Post by Shiri Bogomolny
END IF.
EXECUTE.
Error # 4070. Command name: END IF
The command does not follow an unclosed DO IF command. Maybe the DO IF
command was not recognized because of an error. Use the level-of-control
shown to the left of the SPSS Statistics commands to determine the range of
LOOPs and DO IFs.
Execution of this command stops.
END IF.
--
Rich Ulrich
Shiri Bogomolny
2017-03-07 16:48:03 UTC
Permalink
Post by David Marso
COMPUTE Final=
SUM( (MemberCatNumber EQ 1)*ANY(AgeInMemberRange,30,54,18)*AgeInMemberRange,
(MemberCatNumber EQ 2)*2 ).
If AgeInMemberRange can assume ONLY values 30,54,18 then this simplifies to
COMPUTE Final=
SUM( (MemberCatNumber EQ 1)*AgeInMemberRange,(MemberCatNumber EQ 2)*2 ).
Post by Rich Ulrich
On Mon, 6 Mar 2017 19:36:19 -0800 (PST), Shiri Bogomolny
Post by Shiri Bogomolny
Hi,
I am trying to create a new variable in SPSS using two different additional variables. In Excel it would be a simple if statement (if variable=x pull from this column, otherwise pull form that column) but I can't get it to work here for some reason.
I have a variable that is membership type for a gym (Adult, youth, staff, etc) coded 1-8. I have another variable that is the age range of the member (18-29, 30-54, etc). I want to create a third variable in which if the member is an adult, rather than "adult" I want it to say his age range category. If the member is any other type of member I want it to retain the original membership type. I have coded both membership types and age ranges as values.
Comment Set the missing to a particular Missing value.
COMPUTE Final = -9.
IF (MemberCatNumber = 1) Final= AgeInMemberRange.
IF (MemberCatNumber = 2) Final= MemberCatNumber.
That's all you need, probably. Else, create a temporary variable
like #age to be the object of the RECODE and what you set Final to.
But further...
I never used RECODE after a conditional; I think I remember
that this is now allowed.
Post by Shiri Bogomolny
DO IF (MemberCatNumber = 1).
RECODE AgeInMemberRange (30=30) (54=54) (18=18) INTO Final.
END IF.
EXECUTE.
DO IF (MemberCatNumber = 2).
RECODE MemberCatNumber (2 = 2) INTO Final.
You can't RECODE INTO a variable that already exists.
I would expect your Errors to also mention that. I think
the SPSS parser will only know that Final was "previously"
defined, and not account for the logic that says otherwise.
Post by Shiri Bogomolny
END IF.
EXECUTE.
Error # 4070. Command name: END IF
The command does not follow an unclosed DO IF command. Maybe the DO IF
command was not recognized because of an error. Use the level-of-control
shown to the left of the SPSS Statistics commands to determine the range of
LOOPs and DO IFs.
Execution of this command stops.
END IF.
--
Rich Ulrich
Thank you so much!!

Loading...