counting number of missing values for a row in SPSS Syntax

67 Views Asked by At

I have a survey based data with each row representing a particular respondent and each column representing a response for a particular question in the survey. I have a dataset 72 columns and 2417 rows. For each row, I want to find the number of missing values for each row. What I am basically trying to assess is what % of missing values there are in each row.

COMPUTE counting_var = 0.
LOOP #i = 1 TO NCOLS(Dataset1).
   DO IF (MISSING(Dataset1(#i))).
      counting_var = counting_var + 1.
   END IF.
END LOOP.
counting_var = counting_var/NCOLS(Dataset1)
EXECUTE.

This code is written using SPSS syntax. Through this code, I am trying to find the percent of survey questions a particular respondent has answered. If all the columns for a particular row are filled, it means that the respondent has answered all the questions. However, this code is not working as I expect and is simply creating a new column called counting_var and filling it with 0s. I would be grateful if someone could help me fix this code. Unfortunately, I can only do this in SPSS syntax and no other programming language.

1

There are 1 best solutions below

1
eli-k On

SPSS has a function nmiss that will get you the number of missing values in a row without a loop. Assuming all the variables between var1 and var72 in the function are numerical, and consecutive in the data, this should work for you:

compute Pmiss=nmiss(var1 to var72)/72.

If you do have string variables in between, the following approach will work better:

* first we create a list of numeric variables.
SPSSINC SELECT VARIABLES MACRONAME="!allnums" /PROPERTIES TYPE=NUMERIC.
* now we count the number of variables in the list.
compute Nvrs = 0.
do repeat vr = !allnums .
   compute Nvrs = Nvrs + 1.
end repeat.
compute Pmiss=nmiss( !allnums ) / Nvrs.