Need help with looping and substitution

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help with looping and substitution

Mike Donatello-3
All,

I’m calculating index scores for a bunch of ads that were rated on 27
different indicators.  The code to do this for one ad is listed below.

Rather than re-listing this code 16 times (e.g., Ad1, Ad2, … , Ad16) to do
these transformations for all tested ads, I’m wondering if there’s some
simple way to do this via a loop and index variable?  That is, something
that would essentially substitute the value of the loop index for the
numeral in the {Ad1, Ad2, … Ad16} variable list.  Unfortunately, my attempt
didn’t work.

Does anyone have suggestions or sample code that would let me do this?

Thanks!

* Relabel survey variables to match scale names.

COMPUTE Ad1_Reverent = Made_you_feel_ad1_0 .
COMPUTE Ad1_Worshipful = Made_you_feel_ad1_1 .
COMPUTE Ad1_Spiritual = Made_you_feel_ad1_2 .

COMPUTE Ad1_Loving = Made_you_feel_ad1_3 .
COMPUTE Ad1_Affectionate = Made_you_feel_ad1_4 .
COMPUTE Ad1_Friendly = Made_you_feel_ad1_5 .

COMPUTE Ad1_Grateful = Made_you_feel_ad1_6 .
COMPUTE Ad1_Thankful = Made_you_feel_ad1_7 .
COMPUTE Ad1_Appreciative = Made_you_feel_ad1_8 .

COMPUTE Ad1_Attentive = Made_you_feel_ad1_9 .
COMPUTE Ad1_Curious = Made_you_feel_ad1_10 .
COMPUTE Ad1_Interested = Made_you_feel_ad1_11 .

COMPUTE Ad1_Aroused = Made_you_feel_ad1_12 .
COMPUTE Ad1_Active = More_made_you_feel_ad1_0 .
COMPUTE Ad1_Excited = More_made_you_feel_ad1_1 .

COMPUTE Ad1_Playful = More_made_you_feel_ad1_2 .
COMPUTE Ad1_Entertained = More_made_you_feel_ad1_3 .
COMPUTE Ad1_Lighthearted = More_made_you_feel_ad1_4 .

COMPUTE Ad1_Sad = More_made_you_feel_ad1_5 .
COMPUTE Ad1_Distressed = More_made_you_feel_ad1_6 .
COMPUTE Ad1_Sorrowful = More_made_you_feel_ad1_7 .

COMPUTE Ad1_Fearful = More_made_you_feel_ad1_8 .
COMPUTE Ad1_Afraid = More_made_you_feel_ad1_9 .
COMPUTE Ad1_Anxious = More_made_you_feel_ad1_10 .

COMPUTE Ad1_Skeptical = More_made_you_feel_ad1_11 .
COMPUTE Ad1_Suspicious = More_made_you_feel_ad1_12 .
COMPUTE Ad1_Distrustful = More_made_you_feel_ad1_13 .

* Calculate subdimension scores as sums of indicator scales.

COMPUTE Ad1_Faith = Ad1_Reverent + Ad1_Worshipful + Ad1_Spiritual .
COMPUTE Ad1_Affection = Ad1_Loving + Ad1_Affectionate + Ad1_Friendly .
COMPUTE Ad1_Gratitude = Ad1_Grateful + Ad1_Thankful + Ad1_Appreciative .
COMPUTE Ad1_Interest = Ad1_Attentive + Ad1_Curious + Ad1_Interested .
COMPUTE Ad1_Activation = Ad1_Aroused + Ad1_Active + Ad1_Excited .
COMPUTE Ad1_Surgency = Ad1_Playful + Ad1_Entertained + Ad1_Lighthearted .
COMPUTE Ad1_Sadness = Ad1_Sad + Ad1_Distressed + Ad1_Sorrowful .
COMPUTE Ad1_Fear = Ad1_Fearful + Ad1_Afraid + Ad1_Anxious .
COMPUTE Ad1_Skepticism = Ad1_Skeptical + Ad1_Suspicious + Ad1_Distrustful .

* Calculate dimension scores as sums of subdimension scores.

COMPUTE Ad1_Pleasure = Ad1_Faith + Ad1_Affection + Ad1_Gratitude .
COMPUTE Ad1_Arousal = Ad1_Interest + Ad1_Activation + Ad1_Surgency .
COMPUTE Ad1_Domination = Ad1_Sadness + Ad1_Fear + Ad1_Skepticism .




---
Mike Donatello
Director, Research
USA TODAY
7950 Jones Branch Dr., McLean, VA 22108
V 703.854.4572   F 703.854.2165
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Need help with looping and substitution

Marks, Jim
Mike:

Here is a partial solution worked out for the first subscale.

*** sample data-- use your own.
DATA LIST FREE /id
  made_you_feel_ad1_0
  made_you_feel_ad1_1
  made_you_feel_ad1_2
  made_you_feel_ad2_0
  made_you_feel_ad2_1
  made_you_feel_ad2_2
.
BEGIN DATA
1 1 1 2 5 2 7
2 3 7 6 2 1 3
3 4 5 1 6 4 5
END DATA.

*** rename variables to make the macro simple.
*** expand out to cover your complete list.
RENAME VARIABLES
  (made_you_feel_ad1_0 = ad1s0)
  (made_you_feel_ad1_1 = ad1s1)
  (made_you_feel_ad1_2 = ad1s2)
 (made_you_feel_ad2_0 = ad2s0)
  (made_you_feel_ad2_1 = ad2s1)
 (made_you_feel_ad2_2 = ad2s1)
.
VARIABLE LABELS
  ad1s1 'Ad1 Reverent'
  ad1s2 'Ad1 Worshipful'
  ad1s3 'Ad1 Spiritual'
  ad2s1 'Ad2 Reverent'
  ad2s2 'Ad2 Worshipful'
  ad2s3 'Ad2 Spiritual'
.

*** define the macro.
set mprint  =  no.
DEFINE !do_ad (R = !CMDEND ).

!DO !var !IN (!R).

COMPUTE !CONCAT(!var,Faith)  =sum( !CONCAT(!var,s0) ,
  !CONCAT(!var,s1) ,
  !CONCAT(!var,s2) )
.
EXE.

!DOEND .
!ENDDEFINE .

*** run the macro ********** list all the ads you want to analyse separately after "R = ".
set mprint = yes.
!do_ad R = ad1 ad2.

Note this is a much easier task with the condensed variable names: replacing 'made_you_feel_ad1_s0" with "ad1s0", and eliminating the "_" if the scale summary items makes the !CONCAT easier to write.

FWIW you don't need to use COMPUTE to change variable names-- RENAME VARIABLES (newname = oldname) works without reading the dataset.

You can set options so that the variable label is shown in your output to show which items are which.

If you prefer the descriptive names run something like this after the macro completes:

RENAME VARIABLES
  (ad1s0 = Ad1_Reverent)
  (ad1s1 = Ad1_Worshipful)
  (ad1s2 = Ad1_Spiritual)
.

--jim



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mike Donatello
Sent: Wednesday, October 03, 2007 3:02 PM
To: [hidden email]
Subject: Need help with looping and substitution

All,

I’m calculating index scores for a bunch of ads that were rated on 27 different indicators.  The code to do this for one ad is listed below.

Rather than re-listing this code 16 times (e.g., Ad1, Ad2, … , Ad16) to do these transformations for all tested ads, I’m wondering if there’s some simple way to do this via a loop and index variable?  That is, something that would essentially substitute the value of the loop index for the numeral in the {Ad1, Ad2, … Ad16} variable list.  Unfortunately, my attempt didn’t work.

Does anyone have suggestions or sample code that would let me do this?

Thanks!

* Relabel survey variables to match scale names.

COMPUTE Ad1_Reverent = Made_you_feel_ad1_0 .
COMPUTE Ad1_Worshipful = Made_you_feel_ad1_1 .
COMPUTE Ad1_Spiritual = Made_you_feel_ad1_2 .

COMPUTE Ad1_Loving = Made_you_feel_ad1_3 .
COMPUTE Ad1_Affectionate = Made_you_feel_ad1_4 .
COMPUTE Ad1_Friendly = Made_you_feel_ad1_5 .

COMPUTE Ad1_Grateful = Made_you_feel_ad1_6 .
COMPUTE Ad1_Thankful = Made_you_feel_ad1_7 .
COMPUTE Ad1_Appreciative = Made_you_feel_ad1_8 .

COMPUTE Ad1_Attentive = Made_you_feel_ad1_9 .
COMPUTE Ad1_Curious = Made_you_feel_ad1_10 .
COMPUTE Ad1_Interested = Made_you_feel_ad1_11 .

COMPUTE Ad1_Aroused = Made_you_feel_ad1_12 .
COMPUTE Ad1_Active = More_made_you_feel_ad1_0 .
COMPUTE Ad1_Excited = More_made_you_feel_ad1_1 .

COMPUTE Ad1_Playful = More_made_you_feel_ad1_2 .
COMPUTE Ad1_Entertained = More_made_you_feel_ad1_3 .
COMPUTE Ad1_Lighthearted = More_made_you_feel_ad1_4 .

COMPUTE Ad1_Sad = More_made_you_feel_ad1_5 .
COMPUTE Ad1_Distressed = More_made_you_feel_ad1_6 .
COMPUTE Ad1_Sorrowful = More_made_you_feel_ad1_7 .

COMPUTE Ad1_Fearful = More_made_you_feel_ad1_8 .
COMPUTE Ad1_Afraid = More_made_you_feel_ad1_9 .
COMPUTE Ad1_Anxious = More_made_you_feel_ad1_10 .

COMPUTE Ad1_Skeptical = More_made_you_feel_ad1_11 .
COMPUTE Ad1_Suspicious = More_made_you_feel_ad1_12 .
COMPUTE Ad1_Distrustful = More_made_you_feel_ad1_13 .

* Calculate subdimension scores as sums of indicator scales.

COMPUTE Ad1_Faith = Ad1_Reverent + Ad1_Worshipful + Ad1_Spiritual .
COMPUTE Ad1_Affection = Ad1_Loving + Ad1_Affectionate + Ad1_Friendly .
COMPUTE Ad1_Gratitude = Ad1_Grateful + Ad1_Thankful + Ad1_Appreciative .
COMPUTE Ad1_Interest = Ad1_Attentive + Ad1_Curious + Ad1_Interested .
COMPUTE Ad1_Activation = Ad1_Aroused + Ad1_Active + Ad1_Excited .
COMPUTE Ad1_Surgency = Ad1_Playful + Ad1_Entertained + Ad1_Lighthearted .
COMPUTE Ad1_Sadness = Ad1_Sad + Ad1_Distressed + Ad1_Sorrowful .
COMPUTE Ad1_Fear = Ad1_Fearful + Ad1_Afraid + Ad1_Anxious .
COMPUTE Ad1_Skepticism = Ad1_Skeptical + Ad1_Suspicious + Ad1_Distrustful .

* Calculate dimension scores as sums of subdimension scores.

COMPUTE Ad1_Pleasure = Ad1_Faith + Ad1_Affection + Ad1_Gratitude .
COMPUTE Ad1_Arousal = Ad1_Interest + Ad1_Activation + Ad1_Surgency .
COMPUTE Ad1_Domination = Ad1_Sadness + Ad1_Fear + Ad1_Skepticism .




---
Mike Donatello
Director, Research
USA TODAY
7950 Jones Branch Dr., McLean, VA 22108
V 703.854.4572   F 703.854.2165
[hidden email]