Арифметика с макропеременными
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 | It is not possible to (directly) do arithmetic with macro variables. For instance, given the following 2 macro statements: !LET !a=1. !LET !b=2. the following is not a valid command: !LET !c=!a + !b. The following is an indirect method of obtaining the sum of !a and !b. !LET !c=!LENGTH(!CONCAT(!BLANK(!a),!BLANK(!b)). This is not a very friendly method... Try to do a division (or calculate the log of !b) with a similar method! * Suppose we need the macro variable !myvar to take on "complex" values such as the ln(2), ln(3) and ln(4). Any number of values and any other formulas could be handled, but I will use these 3 to illustrate the method. The trick is to have SPSS calculate the numeric values and write the syntax which will be subsequently run. SET MPRINT=yes. *///////////////////////////////////. DEFINE !doit(nbval=!TOKENS(1)) INPUT PROGRAM. LOOP cnt=1 TO !nbval. COMPUTE myval=LN(cnt). END CASE. END LOOP. END FILE. END INPUT PROGRAM. * Next line assumes you need 5 digits. FORMAT myval(F8.5). * Define the macro variable. DO IF cnt=1. WRITE OUTFILE='c:\temp\macro var.sps' /"DEFINE !values()"/myval. ELSE IF cnt<!nbval. WRITE OUTFILE='c:\temp\macro var.sps' /myval. ELSE. WRITE OUTFILE='c:\temp\macro var.sps' /myval /"!ENDDEFINE.". END IF. EXECUTE. * Run the syntax to define the variable. INCLUDE FILE='c:\temp\macro var.sps'. EXECUTE. !ENDDEFINE. *///////////////////////////////////. !doit nbval=3. *In the following macro, the macro variable !myvar takes on the values ln(1), ln(2) and ln(3). DEFINE !test() !LET !cnt=!NULL !LET !val=!EVAL(!values) !DO !myvar !IN (!val) !LET !cnt=!CONCAT(!cnt,!BLANK(1)) COMPUTE !CONCAT(var,!LENGTH(!cnt))=!myvar. !DOEND EXECUTE. !ENDDEFINE. !test. * Next line is to see all five digits. FORMAT var1 TO var2 (F8.5). |
Related pages
...