|
I have the following macro that normalizes a variable from 0 to 1.
DEFINE !transf (var=!TOKENS(1) /newvar=!TOKENS(1)) AGGREGATE /OUTFILE=* MODE=ADDVARIABLES OVERWRITE=YES /BREAK=country /minval = MIN(!var) /maxval = MAX(!var). COMPUTE !newvar=(!var-minval)/(maxval - minval). !ENDDEFINE. The problem that I'm encountering is when I feed a variable with values that don't differ until the 4th or 5th decimal place I get a "divide by zero" error. For example, if all of the values are .99996, .99997, .99998, I get an error. Apparently, the macro is treating minval and maxval as F8.2 and thus it thinks I'm giving it all 1s (or all .99s). So when it computes maxval - minval, it comes up with 0 and then tries to divide by that. I've tried declaring minval and maxval inside the macro, but that produces an error, even if I DELETE VARIABLES right before it; I've also tried declaring the variables outside the macro, but that seems to have no effect. Any ideas how I can stop this "divide by zero" error? Mark Lavender Application Architect and Developer Finance IT [hidden email] ===================== 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 |
|
Thanks for the input.
It turns out that the problem is due to no variance in a variable. What threw me, however, and the reason I didn't realize that is because the error message that I received came after passing a different variable to the macro. In the log below, the error message is presented after I try to normalize CMR3s, but the actual variable with no variance is CMR2s. The log makes it look like the problem is with CMR3s, however. !transf var=CMR2s newvar=CMR2norm. !transf var=CMR3s newvar=CMR3norm. Warning # 511 A division by zero has been attempted on the indicated command. The result has been set to the system-missing value. Command line: 1941 Current case: 169 Current splitfile group: 1 Mark Lavender Application Architect and Developer Finance IT From: Ruben van den Berg <[hidden email]> To: Mark Lavender/Raleigh/IBM@IBMUS, SPSS mailing list <[hidden email]> Date: 10/11/2010 04:32 PM Subject: RE: Problem with rounding in a macro Dear Mark, To the best of my knowledge, all numbers in SPSS are internally stored as double precision floating point, independently of their read/write format. I believe this means that all numeric data points consist of 16 digits. Especially given the given example (differences in the 5th decimal place) I think rounding is not the problem. Do all cases get a system missing value? And if so, is there really a difference between minval and maxval? Since I tend towards empiricism, I tried to replicate your finding with the syntax below but this runs fine. HTH, Ruben *Test data. input program. loop id=1 to 100. end case. end loop. end file. end input program. compute v1=rv.uni(.99999,1)./*Values in a very narrow range. compute country=rnd(rv.uni(.5,5.5)). *Your macro definition. DEFINE !transf (var=!TOKENS(1) /newvar=!TOKENS(1)) AGGREGATE /OUTFILE=* MODE=ADDVARIABLES OVERWRITE=YES /BREAK=country /minval = MIN(!var) /maxval = MAX(!var). COMPUTE !newvar=(!var-minval)/(maxval - minval). execute. !ENDDEFINE. *The macro call. !transf var=v1 newvar=new_v1. > Date: Mon, 11 Oct 2010 10:03:41 -0400 > From: [hidden email] > Subject: Problem with rounding in a macro > To: [hidden email] > > I have the following macro that normalizes a variable from 0 to 1. > > DEFINE !transf (var=!TOKENS(1) /newvar=!TOKENS(1)) > > AGGREGATE /OUTFILE=* MODE=ADDVARIABLES > OVERWRITE=YES /BREAK=country /minval = MIN(!var) /maxval = MAX(!var). > COMPUTE !newvar=(!var-minval)/(maxval - minval). > > !ENDDEFINE. > > The problem that I'm encountering is when I feed a variable with values > that don't differ until the 4th or 5th decimal place I get a "divide by > zero" error. For example, if all of the values > I get an error. Apparently, the macro is treating minval and maxval as > F8.2 and thus it thinks I'm giving it all 1s (or all .99s). So when it > computes maxval - minval, it comes up with 0 and then tries to divide by > that. > > I've tried declaring minval and maxval inside the macro, but that produces > an error, even if I DELETE VARIABLES right before it; I've also tried > declaring the variables outside the macro, but that seems to have no > effect. > > Any ideas how I can stop this "divide by zero" error? > > Mark Lavender > Application Architect and Developer > Finance IT > [hidden email] > > ===================== > 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 |
| Free forum by Nabble | Edit this page |
