Date: Tue, 2 Dec 2008 18:08:53 +0000
Reply-To: Mario Giesel <spss.giesel@yahoo.de>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Mario Giesel <spss.giesel@yahoo.de>
Subject: AW: Using Macros to Recode
Content-Type: text/plain; charset=utf-8
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 <paugross@INDIANA.EDU>
An: SPSSX-L@LISTSERV.UGA.EDU
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
LISTSERV@LISTSERV.UGA.EDU (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
LISTSERV@LISTSERV.UGA.EDU (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
|