Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • export spss declare missing

    Hi everyone,

    my company finally got an Update from Stata 16 to Stata 19 and now I have access to the export spss command. I was wondering if there is an option or a way to declare missing in the export command. We use unfortunately always our own missings, like -99, -98 and it would be great if I didn't have to write an spss syntax anymore to tell spss, which values are missings. If there is not such an option with the export spss command, has anyone heard of an other way to desclare missings, without actually writing it as an spss syntax?

  • #2
    I don't understand your question -- please clarify what you want.

    Code:
    sysuse auto, clear 
    su // NB rep78 has missing values
    export spss using junk.sav, replace
    import spss using junk.sav, clear
    su // NB results as per original file
    Stata has multiple missing value codes ".", ".a", ".b", etc. Do you use those when using Stata?

    If you do, then in Stata you could replace the different missing value codes for a given variable with "-99", "-98", etc., and then use -export spss- on that modified file. (?)

    Comment


    • #3
      export spss does not support the definition of "user missing values". If you trust the unofficial documentation, you might be able to post-process the exported binary file and manipulate the variable record fields. However, it seems much simpler to create a short SPSS syntax script that defines the missing values.

      Comment


      • #4
        Sorry Stephen, I try to be more specific. We use in Stata not the in-build missings like .a, .b and so on, but our own. For example -99 would be "missing by design" -98 "invalid". So if I use export spss, it preserves the variable labels and value labels, but it does not declare missings in SPSS. Which means the column "Missing" in SPSS is empty and therefor I would have to write an SPSS syntax to declare -99, -98 as a missing.

        Regarding your solution: I tried already to recode my missings to the Stata missings (-99 to .a) and use then export spss, but it still would leave the column "Missing" empty. I tried also savespss, but it would not recognize the missing correctly and use 9 instead of -99

        Comment


        • #5
          Hi Marius Kaltenbach I think you can use Dirk Enzmann's -dta2sav- command which will automatically produce MISSING VALUES commands of SPSS syntax for you.
          Code:
          ssc install dta2sav
          sysuse auto
          recode rep78 (.=.a), gen(rep78m)
          recode price (10000/max = .a ">= 10,000"), gen(pricem)
          label define origin .a "n.k." .b "n.a." .d "ambiguous", modify
          replace foreign=.a in 8/10
          replace foreign=.b in 28/30
          replace foreign=.d in 58/60
          dta2sav, verbose name(`"`c(pwd)'/auto_spss"')
          The above code will produce message like this:
          Code:
          SPSS syntax:
            +--------------------------------------------------------------------------+
            | /* ------------------------------------------------------------------ */ |
            | /* 1978 Automobile Data */                                               |
            | /* Filename: '******\ado\base/a/auto.dta' */               |
            | /* File changed since 13 Apr 2018 17:45 */                               |
            | /* Cases: 74 (exported 74), variables: 14 (exported 14) */               |
            | /* Exported from Stata: 24 Jun 2025 18:45:04 */                          |
            | /* ------------------------------------------------------------------ */ |
            |                                                                          |
            | GET STATA FILE='******/auto_spss.dts'.                         |
            | FILE LABEL 1978 Automobile Data.                                         |
            |                                                                          |
            | MISSING VALUES foreign (7 THRU 9).                                       |
            | MISSING VALUES pricem (9999).                                            |
            |                                                                          |
            | SAVE OUTFILE = '******/auto_spss.sav'.                         |
            +--------------------------------------------------------------------------+
          After all of these procedure, you can OPEN auto_spss.sps and RUN it in SPSS.

          And as to the rule of encoding missing values in -dta2sav-, you can read this description in its help file.
          The rule to encode the extended missing values into "numeric" values is easier to understand by an example than by an abstract description: Imagine that the maximum valid value is a one digit integer, say 1, and there are two extended missing values .a and .b which are labeled. In this case .a will be encoded into 9, the maximum possible one-digit integer. .b will be encoded into a next smaller integer, in this case 8. If, however, the maximum valid value is 998 and there are three extended missing values .a, .b, and .d which are labeled (e.g. "not known", "not applicable", and "> 998"), there are not enough three-digits integers greater than 998. Therefore, .a will be encoded into 9999, .b into 9998, and .d into 9997 and SPSS syntax will be created that defines the values 9999, 9998, and 9997 as missing (after the task dta2sav is completed, the original data will be restored in memory).
          Click image for larger version

Name:	20250624190642.png
Views:	1
Size:	131.4 KB
ID:	1779195

          Comment


          • #6
            PS: The -dta2sav- has no effect if you use non-system missing-value codes (. and the extended missing-value codes .a, .b, ..., .z.) in Stata. So you can first recode your missings to the Stata missings (-99 to .a), and then use -dta2sav-.
            We use in Stata not the in-build missings like .a, .b and so on, but our own. For example -99 would be "missing by design" -98 "invalid". So if I use export spss, it preserves the variable labels and value labels, but it does not declare missings in SPSS. Which means the column "Missing" in SPSS is empty and therefor I would have to write an SPSS syntax to declare -99, -98 as a missing.

            Comment


            • #7
              Originally posted by daniel klein View Post
              export spss does not support the definition of "user missing values". If you trust the unofficial documentation, you might be able to post-process the exported binary file and manipulate the variable record fields. However, it seems much simpler to create a short SPSS syntax script that defines the missing values.
              I completely agree with daniel klein. You could write one short SPSS syntax file (with the needed MISSING VALUES and ADD VALUE LABELS commands) and instruct end users who need it to include an INSERT command in their main syntax files.
              --
              Bruce Weaver
              Email: [email protected]
              Version: Stata/MP 19.5 (Windows)

              Comment


              • #8
                The general idea of "dta2sav" (at SSC) is that before using it you did use Stata's extended missing values consistently to code specific kinds of missing values as .a, .b., .c, .d, etc. and label them accordingly (e.g. .a "no answer", .b "not applicable", .c "larger than 996", d. "don't know"). "dta2sav" then identifies to which missing values (later used in SPSS) these extended missing values are recoded (e.g. 9, 99, 999, or .8, 98, 998, etc., or 7, 97, 997, etc., or 6, 96, 996, etc.) depending on the number of digits of the maximum valid values of a variable (see also the help to "dta2sav").

                Note that because the width (= number of digits) of the missing values for SPSS depend on you data (i.e. the width of the maximum of valid values per variable), the width of the misssing values for SPSS can be different for different data sets. If you want to make sure that the width is the same across different datasets, you can't use "dta2sav" but have to write your own syntax to recode Stata's extended missing values to values for SPSS. I could privately send you an extended example but I am warning you that it is long and complicated. For most cases, however, "dta2sav" should be sufficient.
                Last edited by Dirk Enzmann; Yesterday, 06:58.

                Comment


                • #9
                  Dear Bruce Weaver , dear daniel klein , thanks for your feedback. It is my usual workflow to do it with a SPSS syntax, but I was hoping to be able to Skip that step with the (for me) new Stata export command. However, it seems like that is rather complicated. I will give the answer of Chen Samulsion a try and otherwise will probably stay with my current approach.

                  Comment

                  Working...
                  X