RECODE question

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

RECODE question

Zdaniuk, Bozena-3
Is it possible to ask SPSS to create a set of new variable names by adding a character to the original names?
for example, if I have var1, var2, var3 next to each other in my file, can I run a command:

RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r
?
I have a vague recollection that i managed something like that once but that there was a trick to what "appendage" can be used for SPSS to automatically assign new names. But maybe I was dreaming...
thanks so much.
bozena
Reply | Threaded
Open this post in threaded view
|

Re: RECODE question

Ruben Geert van den Berg
Dear Bozena,

Yes, this is possible but SPSS will only interpret the "TO" command correctly in this case if the subscript is at the end of the variable name.

So

RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r.

won't work but

RECODE var1 to var3 (1=0) (0=1) INTO var_r1 to var_r3.

will (assuming that the two lists have equal lengths). If you insist on having the variable names from the first example, that's a total piece of cake with Python of course.

Kind regards,

Ruben

Date: Thu, 22 Nov 2012 03:20:20 +0000
From: [hidden email]
Subject: RECODE question
To: [hidden email]

Is it possible to ask SPSS to create a set of new variable names by adding a character to the original names?
for example, if I have var1, var2, var3 next to each other in my file, can I run a command:

RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r
?
I have a vague recollection that i managed something like that once but that there was a trick to what "appendage" can be used for SPSS to automatically assign new names. But maybe I was dreaming...
thanks so much.
bozena
Reply | Threaded
Open this post in threaded view
|

Re: RECODE question

John F Hall
In reply to this post by Zdaniuk, Bozena-3

Bozena

 

Ruben’s solution will work, but I also use a convention by changing the varnames to begin with R or REC for recode, thus:

 

do repeat x = var1 to varN / y=rec1 to recN.

recode x ( <recodes wanted>  ) into y.

end repeat.

 

This works as long as the variables are contiguous in the file.

 

Some time ago Jon (no H) Peck explained why you can’t generate automatic names using varA to varaZ, but it would still be a nice addition to the SPSS arsenal.  I’ve been asking for such a facility since 1974.

 

John F Hall (Mr)

 

Email:     [hidden email]

Website: www.surveyresearch.weebly.com

 

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Zdaniuk, Bozena
Sent: 22 November 2012 04:20
To: [hidden email]
Subject: RECODE question

 

Is it possible to ask SPSS to create a set of new variable names by adding a character to the original names?
for example, if I have var1, var2, var3 next to each other in my file, can I run a command:

RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r
?
I have a vague recollection that i managed something like that once but that there was a trick to what "appendage" can be used for SPSS to automatically assign new names. But maybe I was dreaming...
thanks so much.
bozena

Reply | Threaded
Open this post in threaded view
|

Re: RECODE question

Bruce Weaver
Administrator
In reply to this post by Ruben Geert van den Berg
Here's a NPR (No Python Required) solution.

data list list/var1 to var3 (3f1).
begin data
0 0 0
1 0 0
0 1 0
0 0 1
1 1 0
1 0 1
0 1 1
1 1 1
end data.

RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r .
LIST.

* ERROR!  var1_r TO var3_r does not work on RECODE.
* But what if those variables already exist?.

NUMERIC var1_r var2_r var3_r (f1).
RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r .
LIST.

* Now it does work.
* So the problem becomes how to create those variables
* when there is a LONG list of them -- e.g., var1_r to var99_r.
* A macro could be used for this.

data list list/var1 to var3 (3f1).
begin data
0 0 0
1 0 0
0 1 0
0 0 1
1 1 0
1 0 1
0 1 1
1 1 1
end data.

DEFINE !MakeVars
 ( Low = !tokens(1) /
   High = !tokens(1) )
!DO !i = !Low !TO !High
NUMERIC !CONCAT("var",!i,"_r") (F1).
!DOEND
EXECUTE.
!ENDDEFINE.

set mprint on.
!MakeVars Low=1 High=3.
set mprint off.

* Comment out the SET commands above when you're sure everything is working okay.

RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r .
LIST.


Ruben van den Berg wrote
Dear Bozena,
Yes, this is possible but SPSS will only interpret the "TO" command correctly in this case if the subscript is at the end of the variable name.
So
RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r.
won't work but
RECODE var1 to var3 (1=0) (0=1) INTO var_r1 to var_r3.
will (assuming that the two lists have equal lengths). If you insist on having the variable names from the first example, that's a total piece of cake with Python of course.
Kind regards,
Ruben
Date: Thu, 22 Nov 2012 03:20:20 +0000
From: [hidden email]
Subject: RECODE question
To: [hidden email]







Is it possible to ask SPSS to create a set of new variable names by adding a character to the original names?


for example, if I have var1, var2, var3 next to each other in my file, can I run a command:



RECODE var1 to var3 (1=0) (0=1) INTO var1_r to var3_r

?

I have a vague recollection that i managed something like that once but that there was a trick to what "appendage" can be used for SPSS to automatically assign new names. But maybe I was dreaming...

thanks so much.

bozena
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).