|
I thought I would shorten a procedure by writing a macro, but the macro does
not seem to like the VALUELABEL command Here is what I have in my hands. I have a data set that is sent to me, sometimes it has been recoded to a 1 - 5 scale, sometimes it is in a -2 - +2 scale. If it is in a -2 to +2 scale, I need to recode the variables to a 1 to 5 scale in order to run my analyses. The one thing that I can always count on is the value label to be the same if the scale had not been recoded, so I thought I would take advantage of it and below is what I came up with. The reason for the macro is because there are many different files where I can apply this same procedure, so any help would be appreciated. Thanks BK Syntax Below =============================== *//////////////////////////////////////. DEFINE !Rec ( agr=!tokens (1) ). if (valuelabel(!agr) = "I totally agree it totally applies (2)" and !agr=2) concat(!agr,"_a") = 1. Execute. Rmv concat(!agr,"_a")=smean(concat(!agr,"_a")). Execute. Do If (concat(!agr,"_a") = 1). RECODE !agr (2=5) (1=4) (0=3) (-1=2) (-2=1) . End If. EXECUTE . !ENDDEFINE. *///////////////////////////////////. !rec agr=agr1stb !rec agr=agrplan !rec agr=agrfunc !rec agr=agrvalu !rec agr=agrtry !rec agr=agrahd !rec agr=agrbpri !rec agr=agrreco !rec agr=agrset !rec agr=agrcare !rec agr=agrwill !rec agr=agrseek !rec agr=agrkeep . |
|
At 11:10 PM 5/7/2007, Bubba K wrote:
>I thought I would shorten a procedure by writing a macro, but the >macro does not seem to like the VALUELABEL command > >I have a data set that is sent to me, sometimes it has been recoded to >a 1 - 5 scale, sometimes it is in a -2 - +2 scale. If it is in a -2 >to +2 scale, I need to recode the variables to a 1 to 5 scale in order >to run my analyses. The one thing that I can always count on is the >value label to be the same if the scale had not been recoded, so I >thought I would take advantage of it and below is what I came up >with. > > >*//////////////////////////////////////. >DEFINE !Rec ( > agr=!tokens (1) >). > >if (valuelabel(!agr) = "I totally agree it totally applies (2)" and >!agr=2) >concat(!agr,"_a") = 1. >Execute. >Rmv concat(!agr,"_a")=smean(concat(!agr,"_a")). >Execute. >Do If (concat(!agr,"_a") = 1). >RECODE > !agr (2=5) (1=4) (0=3) (-1=2) (-2=1) . >End If. >EXECUTE . >!ENDDEFINE. >*///////////////////////////////////. OK, you have a few things wrong. The first, though, is that the approach is wrong. To see what's happening (ALWAYS, to see what's happening in a macro) run with SET MPRINT ON. Let's see what the macro expands into, with parameter "agr1stb": if (valuelabel(agr1stb) = "I totally agree it totally applies (2)" and agr1stb=2) concat(agr1stb,"_a") = 1. Execute. Rmv concat(agr1stb,"_a")=smean(concat(agr1stb,"_a")). Execute. Do If (concat(agr1stb,"_a") = 1). RECODE agr1stb (2=5) (1=4) (0=3) (-1=2) (-2=1) . End If. EXECUTE . a.) You're trying to use "concat" to build variable names; but "concat" works on string *values* within a transformation program. . That, in itself, makes the code invalid: you can't assign to 'concat(agr1stb,"_a")', which is not a variable name. Instead of "concat", you want the macro function "!concat". If that's what you'd used (I'm not writing the revised macro syntax), you'd get this: if (valuelabel(agr1stb) = "I totally agree it totally applies (2)" and agr1stb=2) agr1stb_a = 1. Execute. Rmv agr1stb_a=smean(agr1stb_a). Execute. Do If (agr1stb_a = 1). RECODE agr1stb (2=5) (1=4) (0=3) (-1=2) (-2=1) . End If. EXECUTE . b.) If I understand what you want, the approach is wrong. I *think* you want to recode all variables for which the label for value 2 is "I totally agree it totally applies (2)", using "agr1stb_a" as a flag for whether to do this. As you've written it, ignoring the "rmv" for the moment, the RECODE will run only if the label for value 2 is "I totally agree it totally applies (2)" *AND* "agr1stb" is 2 *in that particular case*. (I've no idea what the "RMV" will do in your situation. Probably nothing useful.) c.) By the way (standard exhortation): the "execute" statements do nothing useful, and can slow processing significantly if your file is large. .................... Here's an approach that may work. It assumes that the value labels are consistent between variables with both codings. I'm making "#ValuLab" a scratch string variable, but it could be a regular string variable. And I'm using shorter value labels, to make the code easier to read. "agr1stb_u" is the variable "agr1stb", but with uniform coding. This is SPSS open code, *not* part of a macro: STRING #ValuLab (A20). COMPUTE #ValuLab = VALUELABEL(agr1stb). RECODE #ValueLab ('Totally disagree' = 1) ('Disagree' = 2) ('Neither' = 3) ('Agree' = 4) ('Totally agree' = 5). If this works for you, you'll still have to 'wrap' it in macro syntax, but I'll leave that to you. By the way, I said that you should always debug macros by running with SET MPRINT ON. Another rule is, never write code as a macro, until you have it working as direct non-macro code. -Good luck, Richard |
|
Thanks Richard,
Actually the missing ! in !concat was the problem for me (along with some missing periods and some extra execute statements) What I was trying to do is replace the values based on the variable labels. So, the syntax checks for the presence of one value label, and flags it. The RMV is just a way to copy the flag to the entire range of value labels without having to repeat the syntax 5 times for each variable.... I admit, it is a bit sloppy, and it only works as long as there is one "2" answer.... But now that I have significantly shortened the syntax thanks to folks in this forum, I will just go ahead and flag each label/value match and recode them all. Thanks for the help! BK On 5/8/07, Richard Ristow <[hidden email]> wrote: > > At 11:10 PM 5/7/2007, Bubba K wrote: > > >I thought I would shorten a procedure by writing a macro, but the > >macro does not seem to like the VALUELABEL command > > > >I have a data set that is sent to me, sometimes it has been recoded to > >a 1 - 5 scale, sometimes it is in a -2 - +2 scale. If it is in a -2 > >to +2 scale, I need to recode the variables to a 1 to 5 scale in order > >to run my analyses. The one thing that I can always count on is the > >value label to be the same if the scale had not been recoded, so I > >thought I would take advantage of it and below is what I came up > >with. > > > > > >*//////////////////////////////////////. > >DEFINE !Rec ( > > agr=!tokens (1) > >). > > > >if (valuelabel(!agr) = "I totally agree it totally applies (2)" and > >!agr=2) > >concat(!agr,"_a") = 1. > >Execute. > >Rmv concat(!agr,"_a")=smean(concat(!agr,"_a")). > >Execute. > >Do If (concat(!agr,"_a") = 1). > >RECODE > > !agr (2=5) (1=4) (0=3) (-1=2) (-2=1) . > >End If. > >EXECUTE . > >!ENDDEFINE. > >*///////////////////////////////////. > > OK, you have a few things wrong. The first, though, is that the > approach is wrong. To see what's happening (ALWAYS, to see what's > happening in a macro) run with SET MPRINT ON. Let's see what the macro > expands into, with parameter "agr1stb": > > if (valuelabel(agr1stb) = "I totally agree it totally applies (2)" > and agr1stb=2) > concat(agr1stb,"_a") = 1. > Execute. > Rmv concat(agr1stb,"_a")=smean(concat(agr1stb,"_a")). > Execute. > Do If (concat(agr1stb,"_a") = 1). > RECODE > agr1stb (2=5) (1=4) (0=3) (-1=2) (-2=1) . > End If. > EXECUTE . > > a.) You're trying to use "concat" to build variable names; but "concat" > works on string *values* within a transformation program. . That, in > itself, makes the code invalid: you can't assign to > 'concat(agr1stb,"_a")', which is not a variable name. > > Instead of "concat", you want the macro function "!concat". If that's > what you'd used (I'm not writing the revised macro syntax), you'd get > this: > > if (valuelabel(agr1stb) = "I totally agree it totally applies (2)" > and agr1stb=2) > agr1stb_a = 1. > Execute. > Rmv agr1stb_a=smean(agr1stb_a). > Execute. > Do If (agr1stb_a = 1). > RECODE > agr1stb (2=5) (1=4) (0=3) (-1=2) (-2=1) . > End If. > EXECUTE . > > b.) If I understand what you want, the approach is wrong. I *think* you > want to recode all variables for which the label for value 2 is "I > totally agree it totally applies (2)", using "agr1stb_a" as a flag for > whether to do this. As you've written it, ignoring the "rmv" for the > moment, the RECODE will run only if the label for value 2 is "I totally > agree it totally applies (2)" *AND* "agr1stb" is 2 *in that particular > case*. (I've no idea what the "RMV" will do in your situation. Probably > nothing useful.) > > c.) By the way (standard exhortation): the "execute" statements do > nothing useful, and can slow processing significantly if your file is > large. > .................... > Here's an approach that may work. It assumes that the value labels are > consistent between variables with both codings. I'm making "#ValuLab" a > scratch string variable, but it could be a regular string variable. And > I'm using shorter value labels, to make the code easier to read. > "agr1stb_u" is the variable "agr1stb", but with uniform coding. This is > SPSS open code, *not* part of a macro: > > STRING #ValuLab (A20). > COMPUTE #ValuLab = VALUELABEL(agr1stb). > RECODE #ValueLab > ('Totally disagree' = 1) > ('Disagree' = 2) > ('Neither' = 3) > ('Agree' = 4) > ('Totally agree' = 5). > > If this works for you, you'll still have to 'wrap' it in macro syntax, > but I'll leave that to you. > > By the way, I said that you should always debug macros by running with > SET MPRINT ON. Another rule is, never write code as a macro, until you > have it working as direct non-macro code. > > -Good luck, > Richard > |
|
Although the stated problem is solved, here is an illustration of how one might go about this type of problem with programmability. Following is a program that
- opens employee data.sav (no different from running a GET command) - creates a Python variable dictionary for the jobcat variable - checks to see whether there is a value label for the value 1 - if so, recodes the values into a new variable according to the mapping in braces - The specialtransforms.Recode class by default assigns as the value labels for the output values the input value(s) that were mapped to them. This little class can also handle interval recodes, date values and do a few other little tricks. begin program. import spss, spssaux, specialtransforms spssaux.OpenDataFile("c:/spss15/employee data.sav") vardict = spssaux.VariableDict("jobcat") if "1" in vardict["jobcat"].ValueLabels: r = specialtransforms.Recode("jobcatRecoded", "jobcat", mapping={1:10, 2:11, 3:12}, elserc=999) r.generate() end program. Another variation would look at the distribution of values and base the recode decision on that. This code, which first modifies the jobcat variable so that the recode will trigger, - passes the data and calculates the minimum and maximum values for the jobcat variable. - does the recode if the minimum is negative and the maximum <=2. get file="c:/spss15/employee data.sav". compute jobcat = jobcat -3. begin program. import spss, specialtransforms, spssdata cursor = spssdata.Spssdata("jobcat") vmin, vmax = 99999, -99999 for case in cursor: vmin = min(vmin, case[0]) vmax = max(vmax, case[0]) cursor.CClose() if vmin < 1 and vmax <= 2: r = specialtransforms.Recode("jobcatRecoded", "jobcat", mapping={1:10, 2:11, 3:12}, elserc=999) r.generate() end program. This type of function could be parameterized and saved in a module that could be called with the relevant parameters. As always, these modules can be downloaded from SPSS Developer Central www.spss.com/devcentral Regards, Jon Peck -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bubba K Sent: Tuesday, May 08, 2007 7:24 PM To: [hidden email] Subject: Re: [SPSSX-L] Macro Help - Value Label Thanks Richard, Actually the missing ! in !concat was the problem for me (along with some missing periods and some extra execute statements) What I was trying to do is replace the values based on the variable labels. So, the syntax checks for the presence of one value label, and flags it. The RMV is just a way to copy the flag to the entire range of value labels without having to repeat the syntax 5 times for each variable.... I admit, it is a bit sloppy, and it only works as long as there is one "2" answer.... But now that I have significantly shortened the syntax thanks to folks in this forum, I will just go ahead and flag each label/value match and recode them all. Thanks for the help! BK On 5/8/07, Richard Ristow <[hidden email]> wrote: > > At 11:10 PM 5/7/2007, Bubba K wrote: > > >I thought I would shorten a procedure by writing a macro, but the > >macro does not seem to like the VALUELABEL command > > > >I have a data set that is sent to me, sometimes it has been recoded to > >a 1 - 5 scale, sometimes it is in a -2 - +2 scale. If it is in a -2 > >to +2 scale, I need to recode the variables to a 1 to 5 scale in order > >to run my analyses. The one thing that I can always count on is the > >value label to be the same if the scale had not been recoded, so I > >thought I would take advantage of it and below is what I came up > >with. > > > > > >*//////////////////////////////////////. > >DEFINE !Rec ( > > agr=!tokens (1) > >). > > > >if (valuelabel(!agr) = "I totally agree it totally applies (2)" and > >!agr=2) > >concat(!agr,"_a") = 1. > >Execute. > >Rmv concat(!agr,"_a")=smean(concat(!agr,"_a")). > >Execute. > >Do If (concat(!agr,"_a") = 1). > >RECODE > > !agr (2=5) (1=4) (0=3) (-1=2) (-2=1) . > >End If. > >EXECUTE . > >!ENDDEFINE. > >*///////////////////////////////////. > > OK, you have a few things wrong. The first, though, is that the > approach is wrong. To see what's happening (ALWAYS, to see what's > happening in a macro) run with SET MPRINT ON. Let's see what the macro > expands into, with parameter "agr1stb": > > if (valuelabel(agr1stb) = "I totally agree it totally applies (2)" > and agr1stb=2) > concat(agr1stb,"_a") = 1. > Execute. > Rmv concat(agr1stb,"_a")=smean(concat(agr1stb,"_a")). > Execute. > Do If (concat(agr1stb,"_a") = 1). > RECODE > agr1stb (2=5) (1=4) (0=3) (-1=2) (-2=1) . > End If. > EXECUTE . > > a.) You're trying to use "concat" to build variable names; but "concat" > works on string *values* within a transformation program. . That, in > itself, makes the code invalid: you can't assign to > 'concat(agr1stb,"_a")', which is not a variable name. > > Instead of "concat", you want the macro function "!concat". If that's > what you'd used (I'm not writing the revised macro syntax), you'd get > this: > > if (valuelabel(agr1stb) = "I totally agree it totally applies (2)" > and agr1stb=2) > agr1stb_a = 1. > Execute. > Rmv agr1stb_a=smean(agr1stb_a). > Execute. > Do If (agr1stb_a = 1). > RECODE > agr1stb (2=5) (1=4) (0=3) (-1=2) (-2=1) . > End If. > EXECUTE . > > b.) If I understand what you want, the approach is wrong. I *think* you > want to recode all variables for which the label for value 2 is "I > totally agree it totally applies (2)", using "agr1stb_a" as a flag for > whether to do this. As you've written it, ignoring the "rmv" for the > moment, the RECODE will run only if the label for value 2 is "I totally > agree it totally applies (2)" *AND* "agr1stb" is 2 *in that particular > case*. (I've no idea what the "RMV" will do in your situation. Probably > nothing useful.) > > c.) By the way (standard exhortation): the "execute" statements do > nothing useful, and can slow processing significantly if your file is > large. > .................... > Here's an approach that may work. It assumes that the value labels are > consistent between variables with both codings. I'm making "#ValuLab" a > scratch string variable, but it could be a regular string variable. And > I'm using shorter value labels, to make the code easier to read. > "agr1stb_u" is the variable "agr1stb", but with uniform coding. This is > SPSS open code, *not* part of a macro: > > STRING #ValuLab (A20). > COMPUTE #ValuLab = VALUELABEL(agr1stb). > RECODE #ValueLab > ('Totally disagree' = 1) > ('Disagree' = 2) > ('Neither' = 3) > ('Agree' = 4) > ('Totally agree' = 5). > > If this works for you, you'll still have to 'wrap' it in macro syntax, > but I'll leave that to you. > > By the way, I said that you should always debug macros by running with > SET MPRINT ON. Another rule is, never write code as a macro, until you > have it working as direct non-macro code. > > -Good luck, > Richard > |
|
In reply to this post by Bubba K
At 08:24 PM 5/8/2007, Bubba K wrote:
>What I was trying to do is replace the values based on the variable >labels. So, the syntax checks for the presence of one value label, and >flags it. The RMV is just a way to copy the flag to the entire range >of [cases] Cute! I'm sorry not to have seen how it worked. >I admit, it is a bit sloppy, and it only works as long as there is one >"2" answer.... No, not bad; not bad at all. Yes, you could check all five values; then, so long as ANY value ever occurred, you'd set the flag properly. (You'd still set only one flag per variable.) The RMV will force a data pass, I'm pretty sure. If you have a big file and a lot of variables, that could slow execution enough to be troublesome. >[Now] I will flag each label/value match and recode them all. Go for it. One last point, that many here would advise: It's unwise to recode your original variables. It could be especially confusing in your case, where you leave value labels in place, that then become inaccurate. Recommended practice is to create one new variable for each old one, and recode into it, even though that can clutter your file pretty badly. Good luck! Richard |
|
Can one have the situation where a spearman or similar correlation is high
gt .5 but r^2 or R is not significant, but a low correlation is significant. If under what circumstances does this arise. Brian Cooper |
|
The significance of the correlation depends on the sample size. Low
correlations with large sample sizes may be significant and high correlations with small sample sizes may not be significant. The significance only tells you if the correlation in the population is likely to be zero or not |
| Free forum by Nabble | Edit this page |
