Date: Wed, 14 Jun 2006 01:43:04 -0400
Reply-To: Richard Ristow <wrristow@mindspring.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Richard Ristow <wrristow@mindspring.com>
Subject: Re: Macro calls with two variables but not double looping
Content-Type: text/plain; charset=us-ascii; format=flowed;
x-avg-checked=avg-ok-692F1C4C
Since this feels like 'macro' day, let my try settling this one.
At 05:00 AM 6/9/2006, Michael Pearmain wrote:
>I'm trying to write a macro to take the variable names from 40
>variables and put them onto 40 new variables in the same dataset (in a
>one to one match). At the moment I run into an error because the marco
>double loops around what I am trying to do and thus gives all 40
>variables the same name.
Exactly. This question, and problem, turned up on the List back in
February.
Here's the problem:
>DEFINE !ApplyDic(source = !CHAREND (/)
> /TargetV = !CMDEND).
>
>!DO !S !IN (!source)
>!DO !T = (!TargetV)
You want a single loop, transferring attributes from each source
variable to its corresponding target variable. You've written two
nested loops, which will apply all the information from the first
source variable to ALL target variables; then, information from the
second source variable to ALL target variables; ...
You can write a macro loop that goes through two lists in parallel, not
nested; use, as Simon Freidin said, !HEAD and !TAIL. Here's an example
that does it - SPSS draft output. (Not all dictionary attributes are
copied, because the VARINFO subcommand doesn't ask for that. But the
ones that are copied, are copied pairwise.)
DEFINE !ApplyDic(source = !CHAREND ('/')
/TargetV = !CMDEND).
!LET !T_List = !TargetV.
. /**/ ECHO !QUOTE(!CONCAT(' Srce arg: ',!source)).
. /**/ ECHO !QUOTE(!CONCAT(' Trgt arg: ',!TargetV)).
. /**/ ECHO !QUOTE(!CONCAT(' Tgt list: ',!T_List)).
!DO !S !IN (!source)
!LET !T = !HEAD(!T_List)
!LET !T_List = !TAIL(!T_List)
. /**/ ECHO ' '.
. /**/ ECHO !QUOTE(!CONCAT(' Srce var: ',!S)).
. /**/ ECHO !QUOTE(!CONCAT(' Trgt var: ',!T)).
. /**/ ECHO !QUOTE(!CONCAT(' Tgt list: ',!T_List)).
. APPLY DICTIONARY
/FROM *
/SOURCE VARIABLES = !s
/TARGET VARIABLES = !T
/FILEINFO
/VARINFO VARLABEL.
!DOEND
!ENDDEFINE.
.......................................................
File Information
|-------------------------|------------------------|
|Output Created |14-JUN-2006 01:23:53 |
|-------------------------|------------------------|
Variable Information
|--------|--------|------------|------------|------|---------|------|------|-------|
|Variable|Position|Label |Measurement |Column|Alignment|Print
|Write |Missing|
| | | |Level |Width
| |Format|Format|Values |
|--------|--------|------------|------------|------|---------|------|------|-------|
|survey#
|1 |Survey |Ordinal |6 |Right |F4 |F4 |
|
| | |(response) | | | | |
| |
| | |number; | | | | |
| |
| | |unique | | | | |
| |
| | |record
key | | | | | | |
|--------|--------|------------|------------|------|---------|------|------|-------|
|dropout |4 |Whether
& |Scale |7 |Left |F2 |F2 |9 |
| | |why | | | | |
| |
| | |respondent | | | | |
| |
| | |dropped
from| | | | | | |
| | |study | | | | |
| |
|--------|--------|------------|------------|------|---------|------|------|-------|
|team |5 |Respondent's|Ordinal |4 |Right |F3 |F3
|99 |
| | |team number
| | | | | | |
|--------|--------|------------|------------|------|---------|------|------|-------|
|coached |6 |Was this
a |Ordinal |8 |Left |F2 |F2 | |
| | |coached | | | | |
| |
| | |team? | | | | |
| |
|--------|--------|------------|------------|------|---------|------|------|-------|
Variables in the working file
Variable Values
|-------|----|------------|
|Value | |Label |
|-------|----|------------|
|dropout|0 |Active |
| |----|------------|
| |1 |Withdrew |
| |----|------------|
| |2 |Didn't sbmt |
| | |svys |
| |----|------------|
| |3 |Left SCEC |
| |----|------------|
| |9(a)|MISCODE |
|-------|----|------------|
|coached|1 |Coached |
| |----|------------|
| |2 |Not coached |
|-------|----|------------|
a Missing value
NUMERIC SURVEY2 DROP2 TEAM2 COACH2.
DISPLAY DICTIONARY
/VARIABLES=SURVEY2 DROP2 TEAM2 COACH2.
File Information
Notes
|-------------------------|------------------------|
|Output Created |14-JUN-2006 01:23:53 |
|-------------------------|------------------------|
Variable Information
|--------|--------|------|------------|------------|---------|------------|------------|
|Variable|Position|Label |Measurement |Column Width|Alignment|Print
Format|Write Format|
| | | |Level | | |
| |
|--------|--------|------|------------|------------|---------|------------|------------|
|SURVEY2
|23 |<none>|Scale |10 |Right |F8.2 |F8.2
|
|--------|--------|------|------------|------------|---------|------------|------------|
|DROP2 |24 |<none>|Scale |10 |Right |F8.2
|F8.2 |
|--------|--------|------|------------|------------|---------|------------|------------|
|TEAM2 |25 |<none>|Scale |10 |Right |F8.2
|F8.2 |
|--------|--------|------|------------|------------|---------|------------|------------|
|COACH2 |26 |<none>|Scale |10 |Right |F8.2
|F8.2 |
|--------|--------|------|------------|------------|---------|------------|------------|
Variables in the working file
PRESERVE.
SET MPRINT ON.
* 01:23 6/13/2006: Specify "targetv" by name. Not tested.
363 M> * 01:23 6/13/2006: Specify "targetv" by name. Not tested.
!ApplyDic source = SURVEY# DROPOUT TEAM COACHED
/targetv = SURVEY2 DROP2 TEAM2 COACH2 .
364 M>
365 M> .
366 M>
367 M> ECHO ' Srce arg: SURVEY# DROPOUT TEAM COACHED'.
Srce arg: SURVEY# DROPOUT TEAM COACHED
368 M> ECHO ' Trgt arg: SURVEY2 DROP2 TEAM2 COACH2'.
Trgt arg: SURVEY2 DROP2 TEAM2 COACH2
369 M> ECHO ' Tgt list: SURVEY2 DROP2 TEAM2 COACH2'.
Tgt list: SURVEY2 DROP2 TEAM2 COACH2
370 M> . ECHO ' '.
371 M> ECHO ' Srce var: SURVEY#'.
Srce var: SURVEY#
372 M> ECHO ' Trgt var: SURVEY2'.
Trgt var: SURVEY2
373 M> ECHO ' Tgt list: DROP2 TEAM2 COACH2'.
Tgt list: DROP2 TEAM2 COACH2
374 M> APPLY DICTIONARY /FROM * /SOURCE VARIABLES = SURVEY# /TARGET
VARIABLES
= SURVEY2 /FILEINFO /VARINFO VARLABEL.
375 M> . ECHO ' '.
376 M> ECHO ' Srce var: DROPOUT'.
Srce var: DROPOUT
377 M> ECHO ' Trgt var: DROP2'.
Trgt var: DROP2
378 M> ECHO ' Tgt list: TEAM2 COACH2'.
Tgt list: TEAM2 COACH2
379 M> APPLY DICTIONARY /FROM * /SOURCE VARIABLES = DROPOUT /TARGET
VARIABLES
= DROP2 /FILEINFO /VARINFO VARLABEL.
380 M> . ECHO ' '.
381 M> ECHO ' Srce var: TEAM'.
Srce var: TEAM
382 M> ECHO ' Trgt var: TEAM2'.
Trgt var: TEAM2
383 M> ECHO ' Tgt list: COACH2'.
Tgt list: COACH2
384 M> APPLY DICTIONARY /FROM * /SOURCE VARIABLES = TEAM /TARGET
VARIABLES = T
EAM2 /FILEINFO /VARINFO VARLABEL.
385 M> . ECHO ' '.
386 M> ECHO ' Srce var: COACHED'.
Srce var: COACHED
387 M> ECHO ' Trgt var: COACH2'.
Trgt var: COACH2
388 M> ECHO ' Tgt list: '.
Tgt list:
389 M> APPLY DICTIONARY /FROM * /SOURCE VARIABLES = COACHED /TARGET
VARIABLES
= COACH2 /FILEINFO /VARINFO VARLABEL.
390 M> .
RESTORE.
391 M> RESTORE.
DISPLAY DICTIONARY
/VARIABLES=SURVEY2 DROP2 TEAM2 COACH2.
File Information
|-------------------------|------------------------|
|Output Created |14-JUN-2006 01:23:54 |
|-------------------------|------------------------|
Variable Information
|--------|--------|------------|------------|------|---------|------|------|
|Variable|Position|Label |Measurement |Column|Alignment|Print
|Write |
| | | |Level |Width
| |Format|Format|
|--------|--------|------------|------------|------|---------|------|------|
|SURVEY2
|23 |Survey |Scale |10 |Right |F8.2 |F8.2 |
| | |(response) | | | | |
|
| | |number; | | | | |
|
| | |unique | | | | |
|
| | |record
key | | | | | |
|--------|--------|------------|------------|------|---------|------|------|
|DROP2 |24 |Whether
& |Scale |10 |Right |F8.2 |F8.2 |
| | |why | | | | |
|
| | |respondent | | | | |
|
| | |dropped
from| | | | | |
| | |study | | | | |
|
|--------|--------|------------|------------|------|---------|------|------|
|TEAM2 |25 |Respondent's|Scale |10 |Right |F8.2
|F8.2 |
| | |team number
| | | | | |
|--------|--------|------------|------------|------|---------|------|------|
|COACH2 |26 |Was this
a |Scale |10 |Right |F8.2 |F8.2 |
| | |coached | | | | |
|
| | |team? | | | | |
|
|--------|--------|------------|------------|------|---------|------|------|
Variables in the working file