Login  Register

Re: IF statement to overwite an existing value with a missing value

Posted by Richard Ristow on Feb 09, 2007; 4:40am
URL: http://spssx-discussion.165.s1.nabble.com/Information-about-S-curve-in-SPSS-tp1073784p1073791.html

At 10:56 PM 2/8/2007, Gary Oliver wrote:

>I have the following set of IF statements which successfully execute.
>IF ( P01_VAR4 > 0 ) P01_VAR3 = P01_VAR1 .
>...
>IF ( P35_VAR4 > 0 ) P35_VAR3 = P01_VAR1 .
>
>In some cases the value for VAR3 is zero and I rightly get a warning
>message and the cell is marked with a dot for missing value.

I'm afraid I flat-out don't understand this. In the first place, I see
no VAR3 in the syntax you've given. In the second, I don't see how
you'd get a warning message from a 0 value for any of the variables.
(Nor from a missing value, though it might be a good idea if a missing
value for a Pnn_VAR4 variable *did* cause a warning.)

>The IF statement above does not replace the pre-existing values with a
>blank cell.

If I understand you're meaning, that's right. An IF statement either
applies its change, or does nothing. Your IF statements will copy the
value of P01_VAR1 if the condition tests 'true', and do nothing at all
otherwise; that's how IF is designed to work.

>(a) Is there a command that I can apply to a variable to clear values?

COMPUTE var1 = $SYSMIS.

Or, for a list of variables,

RECODE <varlist> (ELSE=SYSMIS).

(Careful. You must use '$SYSMIS' in the COMPUTE, and 'SYSMIS' without
the '$' in the RECODE.)

>(b) Is there an amendment to the syntax which can force a missing
>value
>to be inserted overriding whatever is in the cell?

If you want a test that assigns a value of a test is true and clears
the variable otherwise, use DO IF instead of IF. Instead of

IF ( P01_VAR4 > 0 ) P01_VAR3 = P01_VAR1 .

use (not tested)

DO IF             ( P01_VAR4 > 0 )
    AND NOT MISSING( P01_VAR4 > 0 ).
.  COMPUTE P01_VAR3 = P01_VAR1 .
ELSE.
.  COMPUTE P01_VAR3 = $SYSMIS.
END IF.

(Using "AND NOT MISSING( P01_VAR4 > 0 )" causes the variable to be
cleared if the test returns 'missing' as well as if it returns
'false'.)

Finally, DO REPEAT, or VECTORs and LOOP, would probably make your code
much more compact, and easier to read and maintain.