Macro facility to concatenate fieldnames and add commas

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

Macro facility to concatenate fieldnames and add commas

Kevin longfield
Hello,

I tried summitting this post but it was returned to me unprocessed because
it  appears to have
already been distributed to the SPSSX-L list. That is, a message with
identical text (but  possibly with different  mail headers) has  been posted
to  the list recently, either by you  or by someone else.

So I am trying a different subject line and added the reason here to get
past the rules that are in place that I inadvertently tripped over. First
time sending this email. my question to the forum is below:

I have been trying many options for building this Macro. Part of the problem
is I do it once every 1 year or so hence my skills getting rusty.

I want to pass in the “root” name of variables I have in my SPSS file then
Concatenate them with the other parts to build up the full Variable name
then add commas between the variables and put them in a “Mean” function. The
number of variables can vary per macro call. Lets say I have 10 Brands
called “BrandA” “BrandB” “BrandC” “BrandD” “BrandE” “BrandF” “BrandG”
“BrandH” “BrandI” “BrandJ” and those are repeated for various “groups” of
Brands like
“Core” (Core_BrandA Core_BrandB… Core_BrandJ)
“Own” (Own_BrandA Own_BrandB… Own_BrandJ)
“Aware” (Aware_BrandA Aware_BrandB… Aware_BrandJ)
Etc.
My Code so far:

DEFINE !Brand_avg (Mkt=!TOKENS(1)
                              /NumBrand=!TOKENS(1)
                   /Brnd1=!TOKENS(1)
                   /Brnd2=!TOKENS(1)
                   /Brnd3=!TOKENS(1)
                   /Brnd4=!TOKENS(1)
                   /Brnd5=!TOKENS(1)
                   /Brnd6=!TOKENS(1)
                   /Brnd7=!TOKENS(1)
                   /Brnd8=!TOKENS(1)
                   /Brnd9=!TOKENS(1)
                   /Brnd10=!TOKENS(1))
Do if Market = !Mkt.
COMPUTE Core_avg=MEAN(
!DO !i=1 !TO !NumBrand
!Concat('Core_',"Brnd",!i,",")
!DOEND
End If.

EXECUTE.
!ENDDEFINE.
*/////////////////////.

SET MPRINT=YES MEXPAND = YES.
!Brand_avg Mkt=1 NumBrand=5 Brnd1=BrandA Brnd2=BrandB Brnd3=BrandC
Brnd4=BrandD Brnd5=BrandE Brnd6= Brnd7= Brnd8= Brnd9= Brnd10=  .
!Brand_avg Mkt=2 NumBrand=4 Brnd1=BrandA Brnd2=BrandC Brnd3=BrandD
Brnd4=BrandH Brnd5= Brnd6= Brnd7= Brnd8= Brnd9= Brnd10=  .
!Brand_avg Mkt=3 NumBrand=6 Brnd1=BrandG Brnd2=BrandH Brnd3=BrandA
Brnd4=BrandB Brnd5=BrandI Brnd6=BrandJ Brnd7= Brnd8= Brnd9= Brnd10=  .


DATA LIST LIST /Market Core_BrandA Core_BrandB Core_BrandC Core_BrandD
Core_BrandE Core_BrandF Core_BrandG Core_BrandH Core_BrandI Core_BrandJ.
BEGIN DATA
1 1 2 3 4 5 1 2 3 4 5
2 1 . 3 . 5 1 . 3 . 5
3 . 2 . 4 . 1 2 3 4 5
END DATA.
LIST.



**** Code I want generated:.
Do if Market=1.
COMPUTE
Core_avg=MEAN(Core_BrandA,Core_BrandB,Core_BrandC,Core_BrandD,Core_BrandE).
END IF.

Do if Market=2
COMPUTE Core_avg=MEAN(Core_BrandA,Core_BrandC,Core_BrandD,Core_BrandH).
END IF.

Do if Market=3
COMPUTE
Core_avg=MEAN(Core_BrandG,Core_BrandH,Core_BrandA,Core_BrandB,Core_BrandI,Core_BrandJ).
END IF.


Thanks for the pointers and help!
~Kevin.



--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Macro facility to concatenate fieldnames and add commas

Jon Peck
Your first post did appear on the SPSS-X list.

Here is another way to tackle this that might be cleaner and easier.  It uses the SPSSINC SELECT VARIABLES extension command, which can be installed from the Extensions > ExtensionHub menu.

First it uses a pattern in the names to define a comma-separated list of variables as a macro.
SPSSINC SELECT VARIABLES MACRONAME="!Mkt1" 
/PROPERTIES PATTERN = "Core_Brand(A|B|C|D|E)"
/OPTIONS SEPARATOR=", ".
The PATTERN is a regular expression that here selects all variables starting with Core_Brand followed by any of the letters listed.  The separator is specified as , so that this will work with a function.
Then the mean calculation is just this.
if Market eq 1 Core_avg = mean(!Mkt1).

You would repeat this for the other markets.
SPSSINC SELECT VARIABLES MACRONAME="!Mkt2" 
/PROPERTIES PATTERN = "Core_Brand(A|C|D|H)"
/OPTIONS SEPARATOR=", ".
if Market eq 2 Core_avg = mean(!Mkt2). 

etc. 

You could, of course, just redefine the same macro rather than creating three separate macros.

On Thu, Feb 21, 2019 at 11:50 AM Kevin longfield <[hidden email]> wrote:
Hello,

I tried summitting this post but it was returned to me unprocessed because
it  appears to have
already been distributed to the SPSSX-L list. That is, a message with
identical text (but  possibly with different  mail headers) has  been posted
to  the list recently, either by you  or by someone else.

So I am trying a different subject line and added the reason here to get
past the rules that are in place that I inadvertently tripped over. First
time sending this email. my question to the forum is below:

I have been trying many options for building this Macro. Part of the problem
is I do it once every 1 year or so hence my skills getting rusty.

I want to pass in the “root” name of variables I have in my SPSS file then
Concatenate them with the other parts to build up the full Variable name
then add commas between the variables and put them in a “Mean” function. The
number of variables can vary per macro call. Lets say I have 10 Brands
called “BrandA” “BrandB” “BrandC” “BrandD” “BrandE” “BrandF” “BrandG”
“BrandH” “BrandI” “BrandJ” and those are repeated for various “groups” of
Brands like
“Core” (Core_BrandA Core_BrandB… Core_BrandJ)
“Own” (Own_BrandA Own_BrandB… Own_BrandJ)
“Aware” (Aware_BrandA Aware_BrandB… Aware_BrandJ)
Etc.
My Code so far:

DEFINE !Brand_avg (Mkt=!TOKENS(1)
                              /NumBrand=!TOKENS(1)
                   /Brnd1=!TOKENS(1)
                   /Brnd2=!TOKENS(1)
                   /Brnd3=!TOKENS(1)
                   /Brnd4=!TOKENS(1)
                   /Brnd5=!TOKENS(1)
                   /Brnd6=!TOKENS(1)
                   /Brnd7=!TOKENS(1)
                   /Brnd8=!TOKENS(1)
                   /Brnd9=!TOKENS(1)
                   /Brnd10=!TOKENS(1))
Do if Market = !Mkt.
COMPUTE Core_avg=MEAN(
!DO !i=1 !TO !NumBrand
!Concat('Core_',"Brnd",!i,",")
!DOEND
End If.

EXECUTE.
!ENDDEFINE.
*/////////////////////.

SET MPRINT=YES MEXPAND = YES.
!Brand_avg Mkt=1 NumBrand=5 Brnd1=BrandA Brnd2=BrandB Brnd3=BrandC
Brnd4=BrandD Brnd5=BrandE Brnd6= Brnd7= Brnd8= Brnd9= Brnd10=  .
!Brand_avg Mkt=2 NumBrand=4 Brnd1=BrandA Brnd2=BrandC Brnd3=BrandD
Brnd4=BrandH Brnd5= Brnd6= Brnd7= Brnd8= Brnd9= Brnd10=  .
!Brand_avg Mkt=3 NumBrand=6 Brnd1=BrandG Brnd2=BrandH Brnd3=BrandA
Brnd4=BrandB Brnd5=BrandI Brnd6=BrandJ Brnd7= Brnd8= Brnd9= Brnd10=  .


DATA LIST LIST /Market Core_BrandA Core_BrandB Core_BrandC Core_BrandD
Core_BrandE Core_BrandF Core_BrandG Core_BrandH Core_BrandI Core_BrandJ.
BEGIN DATA
1 1 2 3 4 5 1 2 3 4 5
2 1 . 3 . 5 1 . 3 . 5
3 . 2 . 4 . 1 2 3 4 5
END DATA.
LIST.



**** Code I want generated:.
Do if Market=1.
COMPUTE
Core_avg=MEAN(Core_BrandA,Core_BrandB,Core_BrandC,Core_BrandD,Core_BrandE).
END IF.

Do if Market=2
COMPUTE Core_avg=MEAN(Core_BrandA,Core_BrandC,Core_BrandD,Core_BrandH).
END IF.

Do if Market=3
COMPUTE
Core_avg=MEAN(Core_BrandG,Core_BrandH,Core_BrandA,Core_BrandB,Core_BrandI,Core_BrandJ).
END IF.


Thanks for the pointers and help!
~Kevin.



--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD