RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

Post by Anastasi » Fri, 12 Jun 2009 05:25:36


Hi Everyone!

I was wondering if it is possible to covert a string variable into a
numeric variable without creating a new variable.
I am dealing with a data set that has over 300 variables that are
coded as string variables that need to be converted into numeric
variables. Given that I am dealing with a large number of variables,
clicking on each of them is a very tedious option. At the same time,
using recode or compute command and creating a set of new variables
will change require writing out a name for each variable. I thought of
an option of dropping the original variables after that recoding and
then renaming recoded variables with names of the original variables,
but it still seems very time consuming, especially if I have to work
with the same problem over and over again.

Thanks,
Anastasia
 
 
 

RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

Post by Bruce Weav » Fri, 12 Jun 2009 05:41:17


If your string variables look like numbers (i.e., they contain only
the digits 0-9), then you might find the macro at the link below
useful.

www.angelfire.com/wv/bwhomedir/spss/convert_macro.txt

The first two examples convert string variables to numeric. Given the
large number of variables you have, you might want to rewrite the code
to so that you can call the macro with "V1 to Vn" rather than listing
all n variables. This assumes the n variables are contiguous in the
file, of course.

Bruce Weaver
XXXX@XXXXX.COM
http://www.yqcomputer.com/
"When all else fails, RTFM."

 
 
 

RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

Post by Anastasi » Fri, 12 Jun 2009 06:19:14

Thank you very much, Bruce .

My variables are coded as dichotomous with values "0" and "1". So, I
think that I will be able to use the macros link that you posted.

I am reading over Raynald's spss website for some help on how to use
macros because I have never done it before.

Thanks for a prompt response, you saved my day.

Anastasia
 
 
 

RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

Post by JKPec » Fri, 12 Jun 2009 09:56:09


There is no need to resort to macros if you have at least version 16.
Use the ALTER TYPE command. It can convert strings to numerics, and
it can do a number of other interesting tricks.

Regards,
Jon Peck
 
 
 

RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

Post by fred.sutto » Fri, 12 Jun 2009 17:09:15

Could you not just save the file out as a tab delimited file and then
re open it and hopefully it should recognise them as being numerics,
if not paste the syntax from the open command and change the A1
formats to F1?

Might be easier if you're in abn older version than 16 or don't have
the time to learn about macros
 
 
 

RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

Post by Bruce Weav » Fri, 12 Jun 2009 21:53:30


Good catch, Jon. I completely forgot about ALTER TYPE.

For the benefit of those using versions older than 16, here is the
syntax chart and some examples from the Help file.


ALTER TYPE varlist([input format = ] {output format }) [varlist...]
{AMIN [+ [n[%]] }
{AHEXMIN [+ [n[%]]}
[/PRINT [{ALTEREDTYPES*] [ALTEREDVALUES]}]
{NONE }


DATA LIST FREE
/Numvar1 (F2) Numvar2 (F2)
StringVar1 (A20) StringVar2 (A30)
StringDate1 (A11) StringDate2 (A10) StringDate3 (A10).
BEGIN DATA
1 23 a234 b2345 28-Oct-2007 10/28/2007 10/29/2008
END DATA.
ALTER TYPE Numvar1 (F5.2) Numvar2 (F3).
ALTER TYPE
StringDate1 to StringDate3 (A11 = DATE11).
ALTER TYPE
StringDate1 to StringDate3 (A10 = ADATE10).
ALTER TYPE ALL (A=AMIN).

Bruce Weaver
XXXX@XXXXX.COM
http://www.yqcomputer.com/
"When all else fails, RTFM."
 
 
 

RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

Post by JKPec » Sat, 13 Jun 2009 11:27:00

n Jun 11, 12:59m, Rich Ulrich < XXXX@XXXXX.COM > wrote:

You always have the ability to change the type interactively in the
Data Editor, but that gets very tedious if you have a lot of variables
to change.

And one thing that ALTER TYPE can do that is impractical in the DE in
most cases is to set the width of string variables to the minimum
required to hold the values.

Regards,
Jon