Create new variable from multiple variables using DO IF command

I need to create a new variable (var_1) that results in value 1 when all the other variables (var_2, var_3, var_4, var_5) have data available. Otherwise, if only one of them doesn't have any data it should show value 2.

So var_1 = 1 means that all data is available and var_1 = 2 means that not all data is available.

So I have written the following syntax in SPSS:

DO IF (var_2, var_3, var_4, var_5) GE 0.

COMPUTE var_1 = 1.

ELSE IF ANY (var_2, var_3, var_4, var_5) missing.

COMPUTE var_1 = 2.

END IF.

EXECUTE.

I don't know how to code "missing" and is giving me multiple error warnings. There may be a very easy solution for this but I'm really struggling anyway because I'm still a beginner, so any help will be much appreciated. Thank you!!

Upvotes: 2

Views: 1016

Answers (2)

eli-k
eli-k

Reputation: 11350

One approach is to use nmiss function:

compute var_1=1+(nmiss(var_2, var_3, var_4, var_5)>0).

So var_1 will be 1 unless the number of missing values in the rest of the variables if larger than 0, in which case var_1 becomes 2.

Upvotes: 2

user45392
user45392

Reputation: 622

You can use the COUNT command to count the number of columns that have a given value, value range, or even different sets of values.

In your case the most straightforward approach is likely to use COUNT followed by a RECODE statement to binarize your data as desired.

* cnt number of non-negatives across var2 thru var5 .
COUNT var1 = var2 var3 var4 var5 (0 THRU HI) .

* if all four vars >=0 then 1, otherwise 2 .
RECODE var1 (0 THRU 3 = 2) (4 = 1) .
EXE .

Edit: I'll also note that the above code is based on your sample code referencing GE O. If you want this to be based on any valid value (not just >=0), you can updated your COUNT like below.

COUNT var1 = var2 var3 var4 var5 (LO THRU HI) .

If you even want to include user-missing values as valid (only excluding system-missing values), you could take the same approach, but in reverse (count how many are missing, and set to 1 if none are).

COUNT var1 = var2 var3 var4 var5 (SYSMIS) . 
RECODE var1 (0 = 1) (1 THRU 4 = 2) .
EXE .

Upvotes: 2

Related Questions