Macros Are Just String Parsers!
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 | Subject:Re: I'm stuck
Date:1997/10/02
Author:David Mark Marso <marso@spss.com>
Martin Lee wrote:
Using V7.5 win, I'm trying to produce a routine that will go thru a dataset that has numeric variables named in the style a1000,a1001, a1002... , take five consecutive vars, run some syntax with count and do if to produce a new var, and then move on, skipping two vars, repeat the first exercise, all up 32 times.
I've tried to use a macro - but I don't have an "SPSS Macros for Dummys" book: my best effort is:
> *Macro for counting dmft in DMDS p15, and p18 datasets.
> 1 DEFINE dmftmac (c1start = !TOKENS(1)
> 2 /c1end = !TOKENS(1)).
> 3 !DO !idx = c1start !TO c1end !BY 7.
> 4 !LET !a = !CONCAT(a,!idx).
> 5 !LET !b = !CONCAT(a,!idx + 4).
> 6 count #(!idx)= !a to !b (1 thru 5).
> do if (#(!idx) gt 0).
> compute t(!idx)=1.
> else.
> if (#(!idx) eq 0)t(!idx)=0.
> end if.
> !DOEND.
> !ENDDEFINE.
>
> dmftmac 1064 1281 .
> exec.
>
> Unfortunately it doesn't like the addition in line 5 (linenums added)and says addition isn't implemented, which is a pity as it's one of the few things I can do! I should have stuck to filling teeth.
Is the answer out there?
If it is I'd be be really grateful.
Martin Lee
Public Health Dentist
Healthlink South
Christchurch, New Zealand
leem1@hsl.co.nz
* No need for a macro here *
* Here is an untested but methodologically correct program using plain old syntax.
Vector vars=a1064 to a1288.
Vector t(32).
loop #=1 to 224 step 7.
do repeat new=XX1 XX2 XX3 XX4 XX5 / J=0 to 4.
compute new=vars(# + J).
end repeat.
count #temp=XX1 to XX5 (1 thru 5).
compute t((#-1)/7+1)= #temp > 0.
end loop.
You have discovered that MACROS are not too smart about arithmetic.
In fact Macro is just a string parser. If it had gone to college it would have failed mathematics ;-). If you want to do something like this and are absolutely committed to a macro solution I would urge you to study the Canonical Correlation Macro which ships with the Advanced statistics module. It simulates multiplication (m*n) by concatenating a string to a given length (n) and then concatenating that string (m) times. By checking the length of the mn string you have n*m as a macro string walla (sp) you have macro doing math (or something like it).
BTW. When I used to do Technical Support I used to be the MACRO specialist. The very first brain puller I received was somebody trying to get it to do math. That one kept me busy for awhile until I realized Macro is numerically unaware ;-) But since you have SPSS 7.5 the best way to do this is a script.
See the SPSSInfo object and its properties (in particular the VariableAt property).
*There is an example in the help file (copied below)*.
Dim objSpssInfo as ISpssInfo
Dim strVarName as String, strLabel as String
Set objSpssInfo = objSpssApp.SpssInfo
' Gets the variable names and reads the label for GENDER:
Dim Count as Integer, I as Integer
Count = objSpssInfo.NumVariables
For I = 0 To Count - 1
strVarName = objSpssInfo.VariableAt(I)
If strVarName = "gender" Then
strLabel = objSpssInfo.VariableLabelAt(I)
Exit For
End If
Next
|
Related pages
...
Navigate from here