Same results using two different logical operators. Why?

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

iara
Posts: 11
Joined: Tue Oct 20, 2015 3:22 pm

Same results using two different logical operators. Why?

Two different logical statements yielded the same results. Logically version 1 does not make sense, but it seems to work. I am not sure why? Any ideas why this would be?

The two sets of syntax combine 7 variables into a new variable (New) into either with our without a certain characteristic. For each of the variables, 1 means the population does not has a certain characteristic and 2 means they do have. The data are not mutually exclusive

______________

*Version 1

COMPUTE New=0.
EXECUTE.
IF DIFFREM = 1 OR DIFFPHYS = 1 OR DIFFMOB= 1 OR DIFFCARE= 1 OR DIFFSENS= 1 OR DIFFEYE= 1 OR DIFFHEAR= 1 New = 1.
IF DIFFREM = 2 OR DIFFPHYS = 2 OR DIFFMOB= 2 OR DIFFCARE= 2 OR DIFFSENS= 2 OR DIFFEYE= 2 OR DIFFHEAR= 2 New = 2.

VALUE LABELS New
1 "Without"
2 "With".

Formats New (F5.0).
EXECUTE.

* Version 2

COMPUTE New2=0.
EXECUTE.
IF DIFFREM = 1 AND DIFFPHYS = 1 AND DIFFMOB= 1 AND DIFFCARE= 1 AND DIFFSENS= 1 AND DIFFEYE= 1 AND DIFFHEAR= 1 New2= 1.

IF DIFFREM = 2 OR DIFFPHYS = 2 OR DIFFMOB= 2 OR DIFFCARE= 2 OR DIFFSENS= 2 OR DIFFEYE= 2 OR DIFFHEAR= 2 New2= 2.

VALUE LABELS New2
1 "Without"
2 "With".

Formats New2 (F5.0).
EXECUTE.

Thanks
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Same results using two different logical operators. Why?

Hi,
A bit difficult to see without the data. But
Version 1:
1. First sets New to 0.
2. Then, if at least one variable has value 1, changes New to 1.
3. Then, regardless of what happened in 2, sets new to to if at least one variable has value 2.
This is probably not what you want.

Version 2.
1. First sets New2 to 0.
2. Sets New2 to 1, if all variables have 1. Ignores all other combinations, which remain 0.
3. Sets New2 to 2, if all variables have 2. Again, ignores all other combinations, which remain 0

So version 1 will give you 1 if no variable has value 2, i.e. all variables have value 1 (otherwise it will be changed by the next line)
So version 1 and 2 should be equal for value 1. But I would not think that they are equal for value 2?
hth
Jon
iara
Posts: 11
Joined: Tue Oct 20, 2015 3:22 pm

Re: Same results using two different logical operators. Why?

Thanks, Jon.
In this dataset (census disability data), 0 = N/A, 1 is without a certain disability, and 2 is with a certain disability. I think the two sets work because in the particular dataset there were no N/As (0). Version 2 is definitely the correct one.
I came up with a one-step alternative, but I have not figured out a way to include the N/As (0). I suppose I would have to add a second step:

COMPUTE VARTest3= (DIFFREM=2 OR DIFFPHYS=2 OR DIFFMOB=2 OR DIFFCARE=2 OR DIFFSENS=2 OR DIFFEYE=2 OR DIFFHEAR=2) .
VARIABLE LABELS VARTest3 'DisabilityTest'.

VALUE LABELS VARTest3
1 "With a disability"
0 "Without a disability".

Formats VARTest3 (F5.0).
EXECUTE.

_____
I have another multi-step statement that I would like to simplify. It uses census pums data to create a veteran and non veteran category for persons 17+. In the original dataset a zero is both under 17 and never served, one is not a veteran, and 2 is a veteran or active service. I used EMPSTATD to remove those in active duty from veterans who are no longer serving. Is it possible to change the following syntax into something more elegant/simpler:

COMPUTE VETSTATRECODE=\$SYSMIS.
EXECUTE.
IF VET01LTR = 0 AND VET90X01 = 0 AND VET75X90= 0 AND VETVIETN= 0 AND VET55X64= 0 AND VETKOREA= 0 AND VET47X50= 0 AND VETWWII = 0 AND VETOTHER = 0 AND AGE > 16 VETSTATRECODE = 0.
IF VET01LTR = 1 AND VET90X01 = 1 AND VET75X90= 1 AND VETVIETN= 1 AND VET55X64= 1 AND VETKOREA= 1 AND VET47X50= 1 AND VETWWII = 1 AND VETOTHER = 1 AND AGE > 16 VETSTATRECODE = 0.
IF VET01LTR = 2 OR VET90X01 = 2 OR VET75X90= 2 OR VETVIETN= 2 OR VET55X64= 2 OR VETKOREA= 2 OR VET47X50= 2 OR VETWWII = 2 OR VETOTHER = 2 AND AGE > 16 VETSTATRECODE = 1.
IF EMPSTATD = 13 OR EMPSTATD = 14 OR EMPSTATD =15 VETSTATRECODE = 0.
VARIABLE LABELS VETSTATRECODE 'Veteran Status'.

VALUE LABELS VETSTATRECODE
0 "Not a veteran"
1 "Veteran".

Formats VETSTATRECODE (F5.0).
EXECUTE.
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Same results using two different logical operators. Why?

Hi,
For the first, depends on what you want to do with the NAs. The simplest would be to set 0 to missing for all variables. Then you will get a missing if all variables have missing (given that you use OR) which is more or less what you would want.
For the second.
Firts, you don't need the
COMPUTE VETSTATRECODE=\$SYSMIS.
EXECUTE.
If VESTTATRECODE is not assigned anything during the subsequent commands, it will remain sysmis anyway. (also, the EXECUTE is redundant - you only need an EXECUTE after all the transformation commands.
Given execution order, it is actually line 5 and 6 that does most of the work.
I would guess you could do something like (not tested):

Code: Select all

``````DO IF any(EMPSTATD,13,14,15).
compute VETSTATRECODE=0.
else.
compute  VETSTATRECODE= (VET01LTR = 2 OR VET90X01 = 2 OR VET75X90= 2 OR VETVIETN= 2 OR VET55X64= 2 OR VETKOREA= 2 OR VET47X50= 2 OR VETWWII = 2 OR VETOTHER = 2) AND AGE > 16.
* Check if my interpretation of the parentheses is right!.
end if.
``````
Jon
iara
Posts: 11
Joined: Tue Oct 20, 2015 3:22 pm

Re: Same results using two different logical operators. Why?

Thanks so much, Jon. Very helpful. I understand it now.

Who is online

Users browsing this forum: No registered users and 1 guest