Using Macros to Recode

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

Using Macros to Recode

Jake Gross
SPSS Folks,

I am slowly (and sometimes surely) trying to teach myself how to use macros
to be more efficient with my code. I need help using a macro to recode
multiple variables.

I have 6 variables I want to recode (all have the same values and are being
recoded the same way). Basically, I want to take a 7 category variable and
make it dichotomous for descriptive purposes.

I have two variables: The one I am recoding and the new one I need to create
(OldVar and NewVar below).

DEFINE !GradeVarDi () NewVar  !ENDDEFINE.
DEFINE !GradeValMulti() OldVar!ENDDEFINE.
DO IF (!GradeValMulti>=3).
 COMPUTE !GradeVarDi=1.
 ELSE IF (!GradeValMulti<3).
 COMPUTE !GradeVarDi=0.
END IF.
EXE.

The problem I am running into is how to specify that I have six tokens and
want SPSS to repeat the following code as many times as there are tokens. I
have consulted Raynald's book and the SPSS tools website, but somehow just
am not making the connection to figure out how to do this. Any suggestions?

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Using Macros to Recode

Art Kendall
the syntax that you would generalize in a macro would be something like
this.
recode *oldvar1 to oldvar6* (1 thru 3=1)(4 thru 7=0)
(sysmis=sysmis)(else=copy) into newvar1 to newvar6.
missing values newvar1 to newvar6 (...).
value labels newvar1 to newvar6 1 'whatever 3 stands for or lower' 0
'over whatever 3 stands for'
 ... {whatever user missing values you have}.



You would then have to pass only the variable list for the old
variables  and possibly the variable list for new variables.

ideally you would already cleaned your data so you have no sysmis
values.  They might become user missing values that indicate why they
had been system missing

e.g.
value labels ...
   -8 'unreadable according to format as instructed on read in'
  -9 'not able to do transformation as instructed'.


Art Kendall
Social Research Consultants


Jake Gross wrote:

> SPSS Folks,
>
> I am slowly (and sometimes surely) trying to teach myself how to use macros
> to be more efficient with my code. I need help using a macro to recode
> multiple variables.
>
> I have 6 variables I want to recode (all have the same values and are being
> recoded the same way). Basically, I want to take a 7 category variable and
> make it dichotomous for descriptive purposes.
>
> I have two variables: The one I am recoding and the new one I need to create
> (OldVar and NewVar below).
>
> DEFINE !GradeVarDi () NewVar  !ENDDEFINE.
> DEFINE !GradeValMulti() OldVar!ENDDEFINE.
> DO IF (!GradeValMulti>=3).
>  COMPUTE !GradeVarDi=1.
>  ELSE IF (!GradeValMulti<3).
>  COMPUTE !GradeVarDi=0.
> END IF.
> EXE.
>
> The problem I am running into is how to specify that I have six tokens and
> want SPSS to repeat the following code as many times as there are tokens. I
> have consulted Raynald's book and the SPSS tools website, but somehow just
> am not making the connection to figure out how to do this. Any suggestions?
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

AW: Using Macros to Recode

Mario Giesel
In reply to this post by Jake Gross
Hello, Jake,
if you can allow new names to be given by using old names and adding a prefix this one might help.
 
* 1. Data example.
DATA LIST /a 1 b 3.
BEGIN DATA
1 2
2 1
3 7
4 6
5 5
6 4
7 3
END DATA.
VAR LAB a "Mercedes".
VAR LAB b "Toyota".
 
* 2. Create macro.
* ========================================================== START MACRO.
/* macro dichotomizes a variable with respect to a given critical value*/
/*new variable names are built automatically by adding a prefix "d" to the old name*/
DEFINE !dichotomize (critval = !TOKENS(1) / old = !CMDEND)
!DO !item !IN (!old)
!LET !new = !CONCAT("d",!item)
IF (!item >= !critval) !new = 1.
IF (!item < !critval) !new = 0.
APPLY DICTIONARY FROM * / SOURCE VAR = !item/ TARGET VAR = !new.
VAL LAB !new 0 !QUOTE(!CONCAT("Below ",!critval)) 1 !QUOTE(!CONCAT("Greater or equal ",!critval)) .
CROSSTABS !item BY !new.
!DOEND
!ENDDEFINE.
* ========================================================== END MACRO.
 
* 3. Call macro.
!dichotomize critval = 3 old = a b.
 
 
Good luck,
Mario




________________________________
Von: Jake Gross <[hidden email]>
An: [hidden email]
Gesendet: Dienstag, den 2. Dezember 2008, 16:17:43 Uhr
Betreff: Using Macros to Recode

SPSS Folks,

I am slowly (and sometimes surely) trying to teach myself how to use macros
to be more efficient with my code. I need help using a macro to recode
multiple variables.

I have 6 variables I want to recode (all have the same values and are being
recoded the same way). Basically, I want to take a 7 category variable and
make it dichotomous for descriptive purposes.

I have two variables: The one I am recoding and the new one I need to create
(OldVar and NewVar below).

DEFINE !GradeVarDi () NewVar  !ENDDEFINE.
DEFINE !GradeValMulti() OldVar!ENDDEFINE.
DO IF (!GradeValMulti>=3).
COMPUTE !GradeVarDi=1.
ELSE IF (!GradeValMulti<3).
COMPUTE !GradeVarDi=0.
END IF.
EXE.

The problem I am running into is how to specify that I have six tokens and
want SPSS to repeat the following code as many times as there are tokens. I
have consulted Raynald's book and the SPSS tools website, but somehow just
am not making the connection to figure out how to do this. Any suggestions?

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD





====================To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Mario Giesel
Munich, Germany
Reply | Threaded
Open this post in threaded view
|

Re: Using Macros to Recode

Richard Ristow
In reply to this post by Jake Gross
At 10:17 AM 12/2/2008, Jake Gross wrote:

>I am slowly (and sometimes surely) trying to teach myself how to use
>macros to be more efficient with my code.

Congratulations! But, also pay attention to what you can do in direct code:

>I have 6 variables I want to recode (all have the same values and
>are being recoded the same way). Basically, I want to take a 7
>category variable and make it dichotomous for descriptive purposes.
>
>I have two variables: The one I am recoding and the new one I need to create
>(OldVar and NewVar below).
>
>DO IF (!GradeValMulti>=3).
>  COMPUTE !GradeVarDi=1.
>  ELSE IF (!GradeValMulti<3).
>  COMPUTE !GradeVarDi=0.
>END IF.
>
>The problem I am running into is how to specify that I have six
>tokens and want SPSS to repeat the following code as many times as
>there are tokens.

You can do it with a macro, but DO REPEAT in direct code is much
easier (untested):

DO REPEAT  GradeVarMulti = OldVar1 OldVar2 OldVar3
           /GradeVarDi    = NewVar1 NewVar2 NewVar3.
.  DO IF (!GradeValMulti>=3).
.     COMPUTE !GradeVarDi=1.
.  ELSE IF (!GradeValMulti<3).
.      COMPUTE !GradeVarDi=0.
.  END IF.
END REPEAT.

Or, RECODE (code also untested) is still easier:

RECODE   OldVar1 OldVar2 OldVar3
    (MISSING   = SYSMIS)
    (3 THRU HI = 1)
    (ELSE      = 0)
    INTO  NewVar1 NewVar2 NewVar3.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD