Doing Arithmetic With Macro Variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | /* Author: Paul H. Cook, 2002. /* E-mail: me _at_ myhomeaddress _dot_ org (replace _at_ and _dot_) /* This document demonstrates how to perform arithmetic in SPSS macros. /* Arithmetic is not implemented in SPSS macros. Assignments such as /* "!let !x = !y + 3" generate an error. Two solutions to this omission /* are demonstrated in this document: using strings, and using loops. /* String manipulations can be used to simulate arithmetic. Concatenation /* can be used to perform addition, and substrings can be used to perform /* subtraction. Multiplication and division can be implemented as repeated /* addition and subtraction. /* The looping construct in SPSS macros implements true arithmetic. The /* indexing variable is incremented and decremented using integer /* arithmetic. This functionality can be harnessed and used. /* In this document, macro !demo() demonstrates: /* 1) addition, using strings /* 2) addition, using loops /* 3) multiplication, using strings /* 4) multiplication, using loops /* 5) subtraction, using strings /* 6) subtraction, using loops /* 7) division, using strings /* 8) division, using loops /* 9) division, using a hybrid approach /* 10) modulus, using strings /* 11) modulus, using loops /* 12) modulus, using a hybrid approach /* If you want to perform addition, subtraction or multiplication, then the /* most clear and concise method is that using strings. /* If you want to perform division or calculate the modulus, then the most /* clear and concise method is the hybrid approach using both the loops /* method and the strings method. /* The most common scenario for using arithmetic is to increment or decrement /* a variable by a fixed amount. These can be accomplished with single lines /* of code when using the strings methodology: /* - to increment !counter by 1, use... /* !let !counter = !length(!concat(!blanks(!counter), !blanks(1))) /* - to decrement !counter by 1, use... /* !let !counter = !length(!substr(!blanks(!counter), 2)) /* This demonstration is implemented for whole numbers only {0, 1, 2, 3,...}. /* The methodology could probably be extended to work with negative numbers /* and decimal fractions. /* Division is implemented as integer division, such that 6/2=3 and 7/2=3. /* Methodologies for calculating the modulus (remainder) are given, such that /* the modulus of 6/2=0 and the modulus of 7/2=1. /* The looping construct in SPSS macros uses long integers, which limits the /* maximum result to +/- 2,147,483,647 when using the loops method. Please /* note that SPSS does not generate an overflow error if you exceed this; it /* just produces the wrong result. /* This code cannot be encapsulated in macros called from other macros. This /* is because assignments such as "!let !a = !add(6,2)" do not work; SPSS /* will not recognise any tokens following the macro name "!add". /************************ start of macro ************************/ define !demo() /* ------------------------------------------------------ */ /* initialise */ /* ------------------------------------------------------ */ /* The operations 6+2, 6*2, 6-2, 6/2, and the modulus of 6/2 will */ /* be demonstrated using the variables !a and !b. */ !let !a = 6 !let !b = 2 /* ------------------------------------------------------ */ /* addition, using strings (!a + !b) */ /* ------------------------------------------------------ */ /* add !a and !b */ !let !answer = !length(!concat(!blanks(!a), !blanks(!b))) /* display the answer */ compute add_str = !answer. /* ------------------------------------------------------ */ /* addition, using loops (!a + !b) */ /* ------------------------------------------------------ */ /* add !a and !b */ !let !first = true !do !x = !a !to 2147483647 !by !b !if (!first = true) !then !let !first = false !else !let !answer = !x !break !ifend !doend /* display the answer */ compute add_loop = !answer. /* ------------------------------------------------------ */ /* multiplication, using strings (!a * !b) */ /* ------------------------------------------------------ */ /* repeated addition of !b */ !let !tally = !blanks(0) !do !x = 1 !to !a !let !tally = !concat(!tally, !blanks(!b)) !doend /* record the answer */ !let !answer = !length(!tally) /* free the memory */ !let !tally = !blanks(0) /* display the answer */ compute mul_str = !answer. /* ------------------------------------------------------ */ /* multiplication, using loops (!a * !b) */ /* ------------------------------------------------------ */ /* multiply !a and !b */ !let !answer = 0 !do !y = 1 !to !a !let !first = true !do !x = !answer !to 2147483647 !by !b !if (!first = true) !then !let !first = false !else !let !answer = !x !break !ifend !doend !doend /* display the answer */ compute mul_loop = !answer. /* ------------------------------------------------------ */ /* subtraction, using strings (!a - !b) */ /* ------------------------------------------------------ */ /* subtract !b from !a */ !let !answer = !length(!substr(!blanks(!a), !length(!concat(!blanks(!b), !blanks(1))))) /* display the answer */ compute sub_str = !answer. /* ------------------------------------------------------ */ /* subtraction, using loops (!a - !b) */ /* ------------------------------------------------------ */ /* subtract !b from !a */ !let !first = true !do !x = !a !to -2147483647 !by -!b !if (!first = true) !then !let !first = false !else !let !answer = !x !break !ifend !doend /* display the answer */ compute sub_loop = !answer. /* ------------------------------------------------------ */ /* division, using strings (!a / !b) */ /* ------------------------------------------------------ */ /* if we don't have a division-by-zero error, then do the division */ !if (!b <> 0) !then /* count how many times !b goes into !a */ !let !tally = !blanks(0) !let !num = !blanks(!a) !do !x = 1 !to !a /* NB "!if (10 < 2) !then" evaluates to True, because SPSS does a string comparison. */ /* Hence, to do a numerical comparison "!if (!length(!num) < !b) !then"... */ !if (!length(!length(!num)) = !length(!b) !and !length(!num) < !b !or !length(!length(!num)) < !length(!b) ) !then /* done */ !break !else /* subtract !b from !num */ !if (!length(!num) = !b) !then !let !num = !blanks(0) !else !let !num = !substr(!num, !length(!concat(!blanks(!b), !blanks(1)))) !ifend /* increment the tally */ !let !tally = !concat(!tally, !blanks(1)) !ifend !doend /* record the answer */ !let !answer = !length(!tally) /* free the memory */ !let !tally = !blanks(0) !let !num = !blanks(0) /* display the answer */ compute div_str = !answer. !ifend /* ------------------------------------------------------ */ /* division, using loops (!a / !b) */ /* ------------------------------------------------------ */ /* if we don't have a division-by-zero error, then do the division */ !if (!b <> 0) !then !let !answer = 0 /* count how many times !b goes into !a */ !do !y = !a !to !b !by -!b /* increment the answer */ !let !first = true !do !x = !answer !to 2147483647 !if (!first = true) !then !let !first = false !else !let !answer = !x !break !ifend !doend !doend /* display the answer */ compute div_loop = !answer. !ifend /* ------------------------------------------------------ */ /* division, using a hybrid approach (!a / !b) */ /* ------------------------------------------------------ */ /* if we don't have a division-by-zero error, then do the division */ !if (!b <> 0) !then !let !answer = 0 /* count how many times !b goes into !a */ !do !y = !a !to !b !by -!b /* increment the answer */ !let !answer = !length(!concat(!blanks(!answer), !blanks(1))) !doend /* display the answer */ compute div_hyb = !answer. !ifend /* ------------------------------------------------------ */ /* modulus of !a / !b, using strings */ /* ------------------------------------------------------ */ /* if we don't have a division-by-zero error... */ !if (!b <> 0) !then /* do division */ !let !num = !blanks(!a) !do !x = 1 !to !a /* NB "!if (10 < 2) !then" evaluates to True, because SPSS does a string comparison. */ /* Hence, to do a numerical comparison "!if (!length(!num) < !b) !then"... */ !if (!length(!length(!num)) = !length(!b) !and !length(!num) < !b !or !length(!length(!num)) < !length(!b) ) !then /* done */ !break !else /* subtract !b from !num */ !if (!length(!num) = !b) !then !let !num = !blanks(0) !else !let !num = !substr(!num, !length(!concat(!blanks(!b), !blanks(1)))) !ifend !ifend !doend /* record the answer */ !let !answer = !length(!num) /* free the memory */ !let !num = !blanks(0) /* display the answer */ compute mod_str = !answer. !ifend /* ------------------------------------------------------ */ /* modulus of !a / !b, using loops */ /* ------------------------------------------------------ */ /* if we don't have a division-by-zero error... */ !if (!b <> 0) !then !let !answer = 0 /* do division */ !do !y = !a !to !b !by -!b !doend /* modulus = !y - !b */ !let !first = true !do !x = !y !to 0 !by -!b !if (!first = true) !then !let !first = false !else !let !answer = !x !break !ifend !doend /* display the answer */ compute mod_loop = !answer. !ifend /* ------------------------------------------------------ */ /* modulus of !a / !b, using a hybrid approach */ /* ------------------------------------------------------ */ /* if we don't have a division-by-zero error... */ !if (!b <> 0) !then /* do division */ !do !y = !a !to !b !by -!b !doend /* modulus = !y - !b !if (!y = !b) !then !let !answer = 0 !else !let !answer = !length(!substr(!blanks(!y), !length(!concat(!blanks(!b), !blanks(1))))) !ifend /* display the answer */ compute mod_hyb = !answer. !ifend !enddefine. /************************* end of macro *************************/ /* Create a data set, and run the macro. data list list /a b. begin data. 6 2 end data. !demo. execute. |
Related pages
...