Simplifying syntax to compute a new variable from two variables

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Simplifying syntax to compute a new variable from two variables

Lyndon Riviere
Hello all,

Is there a way to simplify the syntax below?  It seems a bit long.


COMPUTE var3=$SYSMIS.

IF (var=1) | (var1=2) | (var1=3) | (var2=1) (var2=2) | (var2=3) var3 =1.

IF (var1=5) | (var2=4) | (var2=5) var3=2.

IF (var1=4) | (var2=6) | (var2=7) | (var2=8) var3=3.


Thanks
Lyndon

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Simplifying syntax to compute a new variable from two variables

Norton, John
Hi Lyndon,

Try the following:

DO IF ((ANY(var1,1,2,3)) OR (ANY(var2,1,2))).
COMPUTE var3 = 1.
ELSE IF (var1 = 5 OR (ANY(var2,4,5))).
COMPUTE var3 = 2.
ELSE IF (var1 = 4 )R (ANY(var2,6,7,8))).
COMPUTE var3 = 3.
END IF.

HTH,

John Norton
SPSS Inc.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Lyndon Riviere
Sent: Monday, June 16, 2008 11:31 AM
To: [hidden email]
Subject: Simplifying syntax to compute a new variable from two variables

Hello all,

Is there a way to simplify the syntax below?  It seems a bit long.


COMPUTE var3=$SYSMIS.

IF (var=1) | (var1=2) | (var1=3) | (var2=1) (var2=2) | (var2=3) var3 =1.

IF (var1=5) | (var2=4) | (var2=5) var3=2.

IF (var1=4) | (var2=6) | (var2=7) | (var2=8) var3=3.


Thanks
Lyndon

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Simplifying syntax to compute a new variable from two variables

Lyndon Riviere
Thanks John for responding.  The modified version of your syntax that I used
appears to "recode" SYSMIS to 0, which was not what I wanted.  I am
experimenting with ways to keep SYSMIS as SYSMIS.

On 6/16/08, Norton, John <[hidden email]> wrote:

>
> Hi Lyndon,
>
> Try the following:
>
> DO IF ((ANY(var1,1,2,3)) OR (ANY(var2,1,2))).
> COMPUTE var3 = 1.
> ELSE IF (var1 = 5 OR (ANY(var2,4,5))).
> COMPUTE var3 = 2.
> ELSE IF (var1 = 4 )R (ANY(var2,6,7,8))).
> COMPUTE var3 = 3.
> END IF.
>
> HTH,
>
> John Norton
> SPSS Inc.
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
> Lyndon Riviere
> Sent: Monday, June 16, 2008 11:31 AM
> To: [hidden email]
> Subject: Simplifying syntax to compute a new variable from two variables
>
> Hello all,
>
> Is there a way to simplify the syntax below?  It seems a bit long.
>
>
> COMPUTE var3=$SYSMIS.
>
> IF (var=1) | (var1=2) | (var1=3) | (var2=1) (var2=2) | (var2=3) var3 =1.
>
> IF (var1=5) | (var2=4) | (var2=5) var3=2.
>
> IF (var1=4) | (var2=6) | (var2=7) | (var2=8) var3=3.
>
>
> Thanks
> Lyndon
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Simplifying syntax to compute a new variable from two variables

Norton, John
Hi Lyndon,

 

I noticed that there were a couple typos in the code I sent.  But those typos would have interrupted the job completely and issued errors rather than assign a value of "0" to var3.  As you refer to a modified version of the code I sent, I can not speak to that in as much as I don't know what modifications you made.  However, below is a self-contained syntax job which will create two variables and 500 cases.  Then, I use the code I initially sent (after fixing the typos, that is) to create a new variable, "var3".  (Be careful when testing this code, as it will create new data, replacing your active data.  This could result in data loss if you've not saved your data prior).

 

NEW FILE.

INPUT PROGRAM.

VECTOR var(2).

LOOP #i = 1 TO 500.

LOOP #j = 1 TO 2.

COMPUTE var(#j) = TRUNC(UNIFORM(10)) + 1.

END LOOP.

END CASE.

END LOOP.

END FILE.

END INPUT PROGRAM.

EXE.

 

DO IF (ANY(var1,1,2,3)) OR (ANY(var2,1,2)).

COMPUTE var3 = 1.

ELSE IF (var1 = 5) OR (ANY(var2,4,5)).

COMPUTE var3 = 2.

ELSE IF (var1 = 4) OR (ANY(var2,6,7,8)).

COMPUTE var3 = 3.

END IF.

EXE.

 

 

Remember that we're using the "OR" clause in the logical argument of the DO IF.  The "OR" allows one or the other side of the clause to be evaluated as true before executing the transformation.  So, in the incomplete DO IF example below...

 

DO IF x = 1 OR y = 2.

COMPUTE z = 1.

 

.. the variable z will be computed to 1 when either "x = 1" is true, or "y = 2" is true.  That means that z can still equal 1 even if y = 999, as long as x = 1.  Likewise, z will equal 1 even if x = 132456789, so long as y = 2.  But if neither element of the argument is true - if x is other than 1 AND y is other than 2 (both conditions must exist) - then the transformation statement is skipped completely, and at this point in the example, z remains undefined.  The variable z is *not* assigned a value of 0.  

 

This logic applies to each condition within the DO IF procedure; if a logical argument is evaluated as true, then the transformation statement(s) which immediately follows that condition is/are executed, and then SPSS proceeds to the next case.  If the argument is evaluated as false, then SPSS skips the transformation statement(s) which immediately follows it and goes to the next logical evaluation.  

 

Implicitly, then, the argument at each step of the DO IF procedure (each condition) must be evaluable as true or false.  If the argument can not be evaluated as true or false, then SPSS can not assign a value to the target variable, and therefore that target variable will remain as undefined.

 

HTH,

 

JN

 

________________________________

From: Lyndon Riviere [mailto:[hidden email]]
Sent: Tuesday, June 17, 2008 1:56 PM
To: Norton, John
Cc: [hidden email]
Subject: Re: Simplifying syntax to compute a new variable from two variables

 

Thanks John for responding.  The modified version of your syntax that I used appears to "recode" SYSMIS to 0, which was not what I wanted.  I am experimenting with ways to keep SYSMIS as SYSMIS.

On 6/16/08, Norton, John <[hidden email]> wrote:

Hi Lyndon,

Try the following:

DO IF ((ANY(var1,1,2,3)) OR (ANY(var2,1,2))).
COMPUTE var3 = 1.
ELSE IF (var1 = 5 OR (ANY(var2,4,5))).
COMPUTE var3 = 2.
ELSE IF (var1 = 4 )R (ANY(var2,6,7,8))).
COMPUTE var3 = 3.
END IF.

HTH,

John Norton
SPSS Inc.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Lyndon Riviere
Sent: Monday, June 16, 2008 11:31 AM
To: [hidden email]
Subject: Simplifying syntax to compute a new variable from two variables

Hello all,

Is there a way to simplify the syntax below?  It seems a bit long.


COMPUTE var3=$SYSMIS.

IF (var=1) | (var1=2) | (var1=3) | (var2=1) (var2=2) | (var2=3) var3 =1.

IF (var1=5) | (var2=4) | (var2=5) var3=2.

IF (var1=4) | (var2=6) | (var2=7) | (var2=8) var3=3.


Thanks
Lyndon

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

 

====================To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Simplifying syntax to compute a new variable from two variables

Art Kendall
In reply to this post by Lyndon Riviere
The default for var3 has a particular value because *you* set it to that
value, *not* because there is something wrong with the instructions you
gave it. Although setting var3 to $sysmis will work, that is usually
undesirable.   One of the strengths of SPSS is the distinction between
user missing and system missing.  In the long run it is a good idea to
reserve $sysmis to 1) the situation where the program cannot read the
input data or 2) you are asking SPSS to do a transformation that is not
possible.  Maintaining this distinction can be a major help in the
debugging and data cleaning that is almost always necessary.

One way to improve your syntax, is to continually revise it, until you
know why a value is missing and label it.  Any $sysmis values can flag a
situation where you should revise your syntax.  In the situation of
input data, you may have to change the format or double check your data
entry etc.
In the situation where $sysmis is returned from transformations, you can
go back and change the initialization value or make the transformation
dependent on a condition.
do if denominator ne 0.
compute myvar = numerator/denominator.
else.
compute myvar = 9999.
end if.
value labels myvar 9999 'zero denominator'.



For almost any variable it is reasonable to set aside a range of values
as missing.  For many variables that can be all negative numbers
missing values myvar (lo thru -1).
or extremely large beyond the reasonable range of values
missing values birthyear (3000 thru hi) IQ (180 thru hi), etc.
value labels birthyear 3000 'after date of survey' 3001 'refused to
tell' 3002 'respondent did not know year for this person'
The use of a range allows you do have many labeled missing values.
value labels birthyear 3000 'after date of survey'
 3001 'refused to tell' 3002 'respondent did not know for this person'

Art Kendall
Social Research Consultants.


Lyndon Riviere wrote:

> Thanks John for responding.  The modified version of your syntax that I used
> appears to "recode" SYSMIS to 0, which was not what I wanted.  I am
> experimenting with ways to keep SYSMIS as SYSMIS.
>
> On 6/16/08, Norton, John <[hidden email]> wrote:
>
>> Hi Lyndon,
>>
>> Try the following:
>>
>> DO IF ((ANY(var1,1,2,3)) OR (ANY(var2,1,2))).
>> COMPUTE var3 = 1.
>> ELSE IF (var1 = 5 OR (ANY(var2,4,5))).
>> COMPUTE var3 = 2.
>> ELSE IF (var1 = 4 )R (ANY(var2,6,7,8))).
>> COMPUTE var3 = 3.
>> END IF.
>>
>> HTH,
>>
>> John Norton
>> SPSS Inc.
>>
>> -----Original Message-----
>> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
>> Lyndon Riviere
>> Sent: Monday, June 16, 2008 11:31 AM
>> To: [hidden email]
>> Subject: Simplifying syntax to compute a new variable from two variables
>>
>> Hello all,
>>
>> Is there a way to simplify the syntax below?  It seems a bit long.
>>
>>
>> COMPUTE var3=$SYSMIS.
>>
>> IF (var=1) | (var1=2) | (var1=3) | (var2=1) (var2=2) | (var2=3) var3 =1.
>>
>> IF (var1=5) | (var2=4) | (var2=5) var3=2.
>>
>> IF (var1=4) | (var2=6) | (var2=7) | (var2=8) var3=3.
>>
>>
>> Thanks
>> Lyndon
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>> [hidden email] (not to SPSSX-L), with no body text except the
>> command. To leave the list, send the command
>> SIGNOFF SPSSX-L
>> For a list of commands to manage subscriptions, send the command
>> INFO REFCARD
>>
>>
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Simplifying syntax to compute a new variable from two variables

Art Kendall
I am not sure why you did not use the ANY for m01 to m06.
You also do not specify how your syntax was not successful.
I am not sure I follow your logic.  I don't see how you can have only 2
values.
if the only values for h01 to h06 and for p01 to p06 are 1,2,3,4, then
try  this set of untested syntax.

numeric z(f2).
compute z=-1
compute condition1 = any(m01 TO m06,1) OR NOT ANY(h01 TO h06,1) OR NOT
ANY(p01 TO p06, 1).
compute condition2 = any(m01 TO m06,2) OR          ANY(h01 TO h06,1)
OR         ANY(p01 TO p06,1).
if condition1 z=1.
if condition2 z=0
crosstabs tables = condition1 by condition2 by z.

otherwise try this set.

numeric z(f2).
compute z=-1
compute condition1 = any(m01 TO m06,1)
    OR  ANY(h01 TO h06,2) OR ANY(h01 TO h06,3) OR ANY(h01 TO h06,4)
    or  ANY(p01 TO p06,2) or ANY(p01 TO p06,3) or ANY(p01 TO p06,4) .
compute condition2 = any(m01 TO m06,2) OR   ANY(h01 TO h06,1)  OR
ANY(p01 TO p06,1).
if condition1 z=1.
if condition2 z=0
crosstabs tables = condition1 by condition2 by z.

I do not think you can have [a list of variables] and [a list of values]
as arguments to the ANY function. but try this.
numeric z(f2).
compute z=-1
compute condition1 = any(m01 TO m06,1) OR  ANY(h01 TO h06,2,3,4)
 or ANY(p01 TO p06,4) .
compute condition2 = any(m01 TO m06,2) OR   ANY(h01 TO h06,1)  OR
ANY(p01 TO p06,1).
if condition1 z=1.
if condition2 z=0
crosstabs tables = condition1 by condition2 by z.

FINALLY, try changing your m01 to mo6 to a ANY and add a third block to
your do if.
ELSE.
COMPUTE Z=2.

then do a FREQUENCIES ON Z.

Art

Lyndon Riviere wrote:

> Thanks for you response Art.  I did not define my problem adequately.
> Let me try to do so again.   I have three sets of variables from which
> I want to create one variable.  I am trying to find a simple way to it
> without referring to every "input" variable.  This is the synax that I
> ran unsuccessfully:
>
>
> DO IF ((m01 TO m06=1)) OR (ANY(h01 TO h06,2, 3,4)) OR (ANY(p01 TO p06,
> 2,3,4)).
>
> COMPUTE z = 1.
>
> ELSE IF ((m01 TO m06=2) OR (h01 TO h06=1) OR (p01 TO p06=1)).
>
> COMPUTE z = 0.
>
> END IF.
>
> EXE.
>
>
>
> I want to keep any sysmis variables as they are.  I don't want them
> recoded or transformed, since I am only going to pay attention to the
> valid cases.  I hope this is clearer now.
>
>
>
>
>
>
> On 6/18/08, *Art Kendall* <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     The default for var3 has a particular value because *you* set it
>     to that value, *not* because there is something wrong with the
>     instructions you gave it. Although setting var3 to $sysmis will
>     work, that is usually undesirable.   One of the strengths of SPSS
>     is the distinction between user missing and system missing.  In
>     the long run it is a good idea to reserve $sysmis to 1) the
>     situation where the program cannot read the input data or 2) you
>     are asking SPSS to do a transformation that is not possible.
>     Maintaining this distinction can be a major help in the debugging
>     and data cleaning that is almost always necessary.
>
>     One way to improve your syntax, is to continually revise it, until
>     you know why a value is missing and label it.  Any $sysmis values
>     can flag a situation where you should revise your syntax.  In the
>     situation of input data, you may have to change the format or
>     double check your data entry etc.
>     In the situation where $sysmis is returned from transformations,
>     you can go back and change the initialization value or make the
>     transformation dependent on a condition.
>     do if denominator ne 0.
>     compute myvar = numerator/denominator.
>     else.
>     compute myvar = 9999.
>     end if.
>     value labels myvar 9999 'zero denominator'.
>
>
>
>     For almost any variable it is reasonable to set aside a range of
>     values as missing.  For many variables that can be all negative
>     numbers
>     missing values myvar (lo thru -1).
>     or extremely large beyond the reasonable range of values
>     missing values birthyear (3000 thru hi) IQ (180 thru hi), etc.
>     value labels birthyear 3000 'after date of survey' 3001 'refused
>     to tell' 3002 'respondent did not know year for this person'
>     The use of a range allows you do have many labeled missing values.
>     value labels birthyear 3000 'after date of survey'
>      3001 'refused to tell' 3002 'respondent did not know for this person'
>
>     Art Kendall
>     Social Research Consultants.
>
>
>
>     Lyndon Riviere wrote:
>>     Thanks John for responding.  The modified version of your syntax that I used
>>     appears to "recode" SYSMIS to 0, which was not what I wanted.  I am
>>     experimenting with ways to keep SYSMIS as SYSMIS.
>>
>>     On 6/16/08, Norton, John <[hidden email]> <mailto:[hidden email]> wrote:
>>
>>>     Hi Lyndon,
>>>
>>>     Try the following:
>>>
>>>     DO IF ((ANY(var1,1,2,3)) OR (ANY(var2,1,2))).
>>>     COMPUTE var3 = 1.
>>>     ELSE IF (var1 = 5 OR (ANY(var2,4,5))).
>>>     COMPUTE var3 = 2.
>>>     ELSE IF (var1 = 4 )R (ANY(var2,6,7,8))).
>>>     COMPUTE var3 = 3.
>>>     END IF.
>>>
>>>     HTH,
>>>
>>>     John Norton
>>>     SPSS Inc.
>>>
>>>     -----Original Message-----
>>>     From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
>>>     Lyndon Riviere
>>>     Sent: Monday, June 16, 2008 11:31 AM
>>>     To: [hidden email] <mailto:[hidden email]>
>>>     Subject: Simplifying syntax to compute a new variable from two variables
>>>
>>>     Hello all,
>>>
>>>     Is there a way to simplify the syntax below?  It seems a bit long.
>>>
>>>
>>>     COMPUTE var3=$SYSMIS.
>>>
>>>     IF (var=1) | (var1=2) | (var1=3) | (var2=1) (var2=2) | (var2=3) var3 =1.
>>>
>>>     IF (var1=5) | (var2=4) | (var2=5) var3=2.
>>>
>>>     IF (var1=4) | (var2=6) | (var2=7) | (var2=8) var3=3.
>>>
>>>
>>>     Thanks
>>>     Lyndon
>>>
>>>     =====================
>>>     To manage your subscription to SPSSX-L, send a message to
>>>     [hidden email] <mailto:[hidden email]> (not to SPSSX-L), with no body text except the
>>>     command. To leave the list, send the command
>>>     SIGNOFF SPSSX-L
>>>     For a list of commands to manage subscriptions, send the command
>>>     INFO REFCARD
>>>
>>>
>>     =====================
>>     To manage your subscription to SPSSX-L, send a message to
>>     [hidden email] <mailto:[hidden email]> (not to SPSSX-L), with no body text except the
>>     command. To leave the list, send the command
>>     SIGNOFF SPSSX-L
>>     For a list of commands to manage subscriptions, send the command
>>     INFO REFCARD
>>
>>
>>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Art Kendall
Social Research Consultants