Variable Handle

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

Variable Handle

Riya
Hey SPSS Masters,

I have a HR data set which contains more than 80 variables. These variables fall into 20 categories.
Suppose a driver named *Manager*which has 5 questions under it. To calculate a score for this variable, i have to compute mean of the 5 questions.

Compute Manager = MEAN(V1, V2, V3, V4, V5).

For the other 19 categories, i have to do the same thing. In other words, i have to assign variable names for the respective category to calculate averages for them.

Can we do the same using DEFINE MACRO ?

I tried to accomplish this using the following code :

DEFINE Mgr ( ) var1,var2,var3,var4,var5 !ENDDEFINE.
Compute Manager = MEAN(Mgr).
Execute.
 
But, this doesn't serve the purpose as one has to manually put "," comma to seperate variables for each of the categories.I am looking for an approach which is the most efficient for handling variables.

* Something like *
* Define all categories and their sub categories in first part of the syntax*
* Call macro here and compute mean for each of the categories*
 

Reply | Threaded
Open this post in threaded view
|

Re: Variable Handle

Bruce Weaver
Administrator
One of the reasons (but not the only one) for using a macro is to avoid repetition of the same code.  You show the example,

Compute Manager = MEAN(V1, V2, V3, V4, V5).

and would like to replace (V1, V2, V3, V4, V5) with (Mgr).  But it's not clear whether that particular list of variables will be used again somewhere.  If not, you're making your code longer by defining all those macros.  It would be simpler to just have a bunch of COMPUTE lines, I think.  But perhaps you've just not given enough information about what you're doing, and why you think macros will help.

HTH.


Riya wrote
Hey SPSS Masters,

I have a HR data set which contains more than 80 variables. These variables fall into 20 categories.
Suppose a driver named *Manager*which has 5 questions under it. To calculate a score for this variable, i have to compute mean of the 5 questions.

Compute Manager = MEAN(V1, V2, V3, V4, V5).

For the other 19 categories, i have to do the same thing. In other words, i have to assign variable names for the respective category to calculate averages for them.

Can we do the same using DEFINE MACRO ?

I tried to accomplish this using the following code :

DEFINE Mgr ( ) var1,var2,var3,var4,var5 !ENDDEFINE.
Compute Manager = MEAN(Mgr).
Execute.
 
But, this doesn't serve the purpose as one has to manually put "," comma to seperate variables for each of the categories.I am looking for an approach which is the most efficient for handling variables.

* Something like *
* Define all categories and their sub categories in first part of the syntax*
* Call macro here and compute mean for each of the categories*
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Variable Handle

David Marso
Administrator
In reply to this post by Riya
If you only have 20, then you really will only end up with 20 computes right?
I don't see how macro buys you anything.
If the variables are contiguous (or can be made to be such) for these 'categories' then you can use the following form
COMPUTE newvar=MEAN(v1 TO v5).
COMPUTE another=MEAN(v6 TO V10).
Or if there are the same number of variables (5 in this case) you could blast away with a VECTOR and some loops and make it tight as a ...errr. <FILTERS ON >.

Untested (but..tried and true):

VECTOR SCORES(20).
RECODE SCORES1 TO SCORES20 (ELSE=0).
VECTOR V=V1 TO V100.
LOOP #=1 TO 20.
LOOP ##=1 TO 5.
COMPUTE SCORES(#)=SCORES(#) +  V( (#-1)* 5 + ##)/5.
END LOOP.
END LOOP.

But try explaining that to your manager when they say
HEY WHAT THE HECK IS THIS 6 months from now.
ie comment the heck out of your final version *.
--
Caveat: Please don't ask me to explain the above code.  
If it parses in your brain GREAT! Enjoy !!
If not, type what you were going to have to type in the first place.
---
Riya wrote
Hey SPSS Masters,

I have a HR data set which contains more than 80 variables. These variables fall into 20 categories.
Suppose a driver named *Manager*which has 5 questions under it. To calculate a score for this variable, i have to compute mean of the 5 questions.

Compute Manager = MEAN(V1, V2, V3, V4, V5).

For the other 19 categories, i have to do the same thing. In other words, i have to assign variable names for the respective category to calculate averages for them.

Can we do the same using DEFINE MACRO ?

I tried to accomplish this using the following code :

DEFINE Mgr ( ) var1,var2,var3,var4,var5 !ENDDEFINE.
Compute Manager = MEAN(Mgr).
Execute.
 
But, this doesn't serve the purpose as one has to manually put "," comma to seperate variables for each of the categories.I am looking for an approach which is the most efficient for handling variables.

* Something like *
* Define all categories and their sub categories in first part of the syntax*
* Call macro here and compute mean for each of the categories*
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Variable Handle

Rich Ulrich
Or, here is the same thing with more generality incorporated.


VECTOR SCORES(20).
RECODE SCORES1 TO SCORES20 (ELSE=0).
VECTOR V=V1 TO V100.
COMPUTE ## = 1.
* this scores the 100 items as continuous sets, of varying sizes.
* You can omit sizes and plug in 5, if they are all 5.
* I used to use the algorithm with additional vector of Item numbers,
*  (which could handily be defined as a set of fixed values, so instead
*   of SCORES(#) it would add in SCORES( itemlist(#))  )
*   for my general Fortran programming of computing scale scores.

DO REPEAT score= SCORES1 to SCORES20/ size= 5, 5, 5, 6, 4,
   3, 7, 4, 5, 6,  3, 4, 5, 6, 7,   5, 5, 5, 5, 5.
LOOP #=1 TO size.
+COMPUTE SCORES(#)=SCORES(#) + V( (##) .
+COMPUTE ##= ##+1 .
END LOOP.
COMPUTE score = score/size.
END REPEAT.

--
Rich Ulrich

> Date: Thu, 14 Mar 2013 15:03:20 -0700

> From: [hidden email]
> Subject: Re: Variable Handle
> To: [hidden email]
>
> If you only have 20, then you really will only end up with 20 computes right?
> I don't see how macro buys you anything.
> If the variables are contiguous (or can be made to be such) for these
> 'categories' then you can use the following form
> COMPUTE newvar=MEAN(v1 TO v5).
> COMPUTE another=MEAN(v6 TO V10).
> Or if there are the same number of variables (5 in this case) you could
> blast away with a VECTOR and some loops and make it tight as a ...errr.
> <FILTERS ON >.
>
> Untested (but..tried and true):
>
> VECTOR SCORES(20).
> RECODE SCORES1 TO SCORES20 (ELSE=0).
> VECTOR V=V1 TO V100.
> LOOP #=1 TO 20.
> LOOP ##=1 TO 5.
> COMPUTE SCORES(#)=SCORES(#) + V( (#-1)* 5 + ##)/5.
> END LOOP.
> END LOOP.
>
> But try explaining that to your manager when they say
> HEY WHAT THE HECK IS THIS 6 months from now.
> ie comment the heck out of your final version *.
> --
> Caveat: Please don't ask me to explain the above code.
> If it parses in your brain GREAT! Enjoy !!
> If not, type what you were going to have to type in the first place.
> ---
>
> Riya wrote
> > Hey SPSS Masters,
> >
> > I have a HR data set which contains more than 80 variables. These
> > variables fall into 20 categories.
> > Suppose a driver named *Manager*which has 5 questions under it. To
> > calculate a score for this variable, i have to compute mean of the 5
> > questions.
> >
> > Compute Manager = MEAN(V1, V2, V3, V4, V5).
> >
> > For the other 19 categories, i have to do the same thing. In other words,
> > i have to assign variable names for the respective category to calculate
> > averages for them.
> >
> > Can we do the same using DEFINE MACRO ?
> >
> > I tried to accomplish this using the following code :
> >
> > DEFINE Mgr ( ) var1,var2,var3,var4,var5 !ENDDEFINE.
> > Compute Manager = MEAN(Mgr).
> > Execute.
> >
> > But, this doesn't serve the purpose as one has to manually put "," comma
> > to seperate variables for each of the categories.I am looking for an
> > approach which is the most efficient for handling variables.
> >
> > * Something like *
> > * Define all categories and their sub categories in first part of the
> > syntax*
> > * Call macro here and compute mean for each of the categories*
>
...
Reply | Threaded
Open this post in threaded view
|

Re: Variable Handle

David Marso
Administrator
I like that too (look far enough back in the archives and you'll find something very similar under my name)!
Only thing I would change is move the comment block to the very top and maybe wrap the whole thing in a macro.  Maybe pass friendly names and the sizes along with the source array.
*NOTE to Riya*!!! remember both of these solutions assume the variables have been properly ordered so the variables for a given scale are contiguous in the file.
--
Rich Ulrich-2 wrote
Or, here is the same thing with more generality incorporated.


VECTOR SCORES(20).
RECODE SCORES1 TO SCORES20 (ELSE=0).
VECTOR V=V1 TO V100.
COMPUTE ## = 1.
* this scores the 100 items as continuous sets, of varying sizes.
* You can omit sizes and plug in 5, if they are all 5.
* I used to use the algorithm with additional vector of Item numbers,
*  (which could handily be defined as a set of fixed values, so instead
*   of SCORES(#) it would add in SCORES( itemlist(#))  )
*   for my general Fortran programming of computing scale scores.

DO REPEAT score= SCORES1 to SCORES20/ size= 5, 5, 5, 6, 4,
   3, 7, 4, 5, 6,  3, 4, 5, 6, 7,   5, 5, 5, 5, 5.
LOOP #=1 TO size.
+COMPUTE SCORES(#)=SCORES(#) +  V( (##) .
+COMPUTE ##= ##+1 .
END LOOP.
COMPUTE score = score/size.
END REPEAT.

--
Rich Ulrich

> Date: Thu, 14 Mar 2013 15:03:20 -0700
> From: [hidden email]
> Subject: Re: Variable Handle
> To: [hidden email]
>
> If you only have 20, then you really will only end up with 20 computes right?
> I don't see how macro buys you anything.
> If the variables are contiguous (or can be made to be such) for these
> 'categories' then you can use the following form
> COMPUTE newvar=MEAN(v1 TO v5).
> COMPUTE another=MEAN(v6 TO V10).
> Or if there are the same number of variables (5 in this case) you could
> blast away with a VECTOR and some loops and make it tight as a ...errr.
> <FILTERS ON >.
>
> Untested (but..tried and true):
>
> VECTOR SCORES(20).
> RECODE SCORES1 TO SCORES20 (ELSE=0).
> VECTOR V=V1 TO V100.
> LOOP #=1 TO 20.
> LOOP ##=1 TO 5.
> COMPUTE SCORES(#)=SCORES(#) +  V( (#-1)* 5 + ##)/5.
> END LOOP.
> END LOOP.
>
> But try explaining that to your manager when they say
> HEY WHAT THE HECK IS THIS 6 months from now.
> ie comment the heck out of your final version *.
> --
> Caveat: Please don't ask me to explain the above code.
> If it parses in your brain GREAT! Enjoy !!
> If not, type what you were going to have to type in the first place.
> ---
>
> Riya wrote
> > Hey SPSS Masters,
> >
> > I have a HR data set which contains more than 80 variables. These
> > variables fall into 20 categories.
> > Suppose a driver named *Manager*which has 5 questions under it. To
> > calculate a score for this variable, i have to compute mean of the 5
> > questions.
> >
> > Compute Manager = MEAN(V1, V2, V3, V4, V5).
> >
> > For the other 19 categories, i have to do the same thing. In other words,
> > i have to assign variable names for the respective category to calculate
> > averages for them.
> >
> > Can we do the same using DEFINE MACRO ?
> >
> > I tried to accomplish this using the following code :
> >
> > DEFINE Mgr ( ) var1,var2,var3,var4,var5 !ENDDEFINE.
> > Compute Manager = MEAN(Mgr).
> > Execute.
> >
> > But, this doesn't serve the purpose as one has to manually put "," comma
> > to seperate variables for each of the categories.I am looking for an
> > approach which is the most efficient for handling variables.
> >
> > * Something like *
> > * Define all categories and their sub categories in first part of the
> > syntax*
> > * Call macro here and compute mean for each of the categories*
>
...
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"