Search code examples
spssdata-manipulation

SPSS: How to Get Variable Type or Duplicate Variable?


I'm creating a number of new variables, and I want each variable type to be based on the type of the old variable. I don't want to do this manually--I have a script to automatically create the new variables based on the names of the old variables, so I'd like it to additionally automatically detect the type of the old variable.

Obviously I have variables of both type string and numeric.

Any ideas?

Another route, if the above is not possible, would be to duplicate the base variable, and then set the values to $sysmis. But then, how can I duplicate the variable?

Thanks!

EDIT, in response to commenter JKP: I'm restructuring a large longitudinal data set using the varstocases syntax. Every variable is not at every timepoint, so I need to create the missing variables so that they will be properly indexed during the restructure.

So, Var_t1 and Var_t3 exist, but when restructured, Var_t1 would get an index of 1, and Var_t3 would get an index of 3. So I need to create an empty Var_t2 in order to /make Var from Var_t1 Var_t2 Var_t3.

Of course, to restructure, the variables have to all be of the same type.

As an aside, this seems to me to be a very convoluted way of doing this, but it seems to be the only option I'm aware of in SPSS.


Solution

  • This would be easy to do using Python programmability. The Dataset class can give you the types, and you could use that in creating the new variables. You might also consider ALTER TYPE and APPLY DICTIONARY depending on exactly what you are trying to do.

    Another possibility would be to use the SPSSINC SELECT VARIABLES extension command to define sets of all the numeric and all the character variables as macros and go from there.

    I can spell this out further if you provide more guidance on what you need to do.