if-statementmissing-dataspss

How do I write a DO IF command asking to compute something ONLY if the value of that case is missing? [SPSS]


I'm new to SPSS and trying to figure out DO IF commands. I have a list of names, and would like to concatenate them into first, middle, last from separate columns. The problem with the "else" argument is that if there is a blank value for the surname_prefix, it inputs two spaces and it's pretty annoying. I wrote this command to see if there was any way I could reduce the space between names to only one space for names with only a first and last name.

For example, this is what currently happens to the new full_name variable when I try to concatenate:

John  Smith /* TWO SPACES HERE

Jasmine Cephas Jones

Here's the code.

do if missing(surname_prefix).
compute full_name = concat(rtrim(first_name), ' ', rtrim(last_name)).
else.
compute full_name = concat(rtrim(first_name),' ',rtrim(surname_prefix),' ',rtrim(last_name)).
end if.
execute.

The code seems to run - it just doesn't do anything. I tried to replace missing() with sysmis(), but it gave me an error that the argument has to be a string var even though surname_prefix is a string variable.

Thank you so much for the help!!


Solution

  • Your original code should work if you look for empty strings instead of for missing values. An empty string would be recognized as a missing value only if it was defined that way. so:

    do if surname_prefix="".
      compute full_name = concat(rtrim(first_name), ' ', rtrim(last_name)).
    else.
      compute full_name = concat(rtrim(first_name),' ',rtrim(surname_prefix),' ',rtrim(last_name)).
    end if.
    

    While you're at it, here's a shorter command for the same results - if two spaces were created they are replaced with one:

    compute full_name = replace(concat(rtrim(first_name),' ',rtrim(surname_prefix),' ',rtrim(last_name)), "  "," ").