SPSS macro parameters

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

SPSS macro parameters

Alex Huang
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: SPSS macro parameters

Jignesh Sutar
If you are vaguely familiar with Python (which I believe you are) you perhaps ought to be looking at setting this up in python, which would be much, much easier to do. 

If by "the length of the input" you mean the number of elements (space and comma delimited - as the alternative length of the string would be trivial to calculate using !LENGHT). See below demo which shows how to get the first (head), last (tail) and count of all the elements provided in the "options" parameter:


/***********************************************/
define !test(options= !cmdend)
title headOptions=!head(!options).
!let !counter1=!blanks(0)
!do !i !in (!options)
    !let !counter1=!concat(!counter1,!blanks(1))
    !let !nbOptions=!len(!counter1)
!doend
title nbOptions=!nbOptions.
!let !tailOptions=!options
!do !i =2 !to !nbOptions
    !let !tailOptions=!tail(!tailOptions)
!doend
title tailOptions=!tailOptions.
!enddefine.
/***********************************************/
!test options= 1.0 2.0 3.0.
!test options= 1.0 2.0 3.0 4.0 145,140.
/* Notice how "145,140" is interpreted as 3 elements and perhaps not one as might be intended with comma being used as a thousand separator */.


Now the equivalent of this in python would simply be:

/***********************************************/
begin program.
def test(options):
    nbOptions=len(options.split())
    firstOptions=options.split()[0]
    lastOptions=options.split()[-1]
    print "nbOptions=",nbOptions
    print "firstOptions=",firstOptions
    print "lastOptions=",lastOptions
test("1.0 2.0 3.0")
test("1.0 2.0 3.0 4.0 145,140") # correctly interprets 145,140 as a single value (and not 3 values), if indeed that is how you want to interpret comma between numbers
end program.
/***********************************************/








On 12 February 2015 at 03:58, Alex Huang <[hidden email]> wrote:
Hi,

I am designing a SPSS macro which takes several inputs as parameters. I have
the following:

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)

in which I would like to take the first parameter as var, and all the others
as options. I accept various number of inputs for options here.

!optionAN var = Q1 options = 35 8887 8888.
!optionAN var = Q2 options = 4 8888.

I would like to ask if it is possible to find out:

1. the value of the first or the last inputs for options,
2. the length of the input,

inside the macro so that I can input the values as parameters?

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)
VECTOR var(!length(options)).
VECTOR v = concat(!var, "_1") TO !concat(!var, "_99").
LOOP #i = 1 to (!length(options)).
COMPUTE var(#i) = v(#i).
END LOOP.
!ENDDEFINE.

Thank you.

Note: I have not yet test the code. I am not sure whether it is correct to
write (var = TOKENS(1), options = !CMDEND) there. Please correct it if I
have any mistakes.

Thanks much,
Alex



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/SPSS-macro-parameters-tp5728654.html
Sent from the SPSSX Discussion mailing list archive at 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

===================== 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: SPSS macro parameters

Alex Huang
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: SPSS macro parameters

John F Hall

If you’re thinking of R and SPSS Juanjo Medina (Manchester) just sent me this, which may be a source of useful advice:

http://www.r-bloggers.com/translate2r-and-translatespss2r-implanting-spss-functionality-into-r/

 

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

SPSS start page:  www.surveyresearch.weebly.com/1-survey-analysis-workshop

 

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Alex Huang
Sent: 13 February 2015 05:40
To: [hidden email]
Subject: Re: SPSS macro parameters

 

Dear Jignesh Sutar,

 

Terribly sorry that I am not familiar with Python but I am experienced with R (I think I am quite familiar, at least I would prefer R than Python). Your suggestion makes me think of an interesting alternative: Is it possible to set up R essentials in SPSS 19 32bit? If it is possible, I would do it in R as I am familiar with what I want to do in my previous question(vector[1], length(vector) vector[length(vector)]).

 

Thanks much,

Alex

 

 

2015-02-12 15:36 GMT+08:00 Jignesh Sutar <[hidden email]>:

If you are vaguely familiar with Python (which I believe you are) you perhaps ought to be looking at setting this up in python, which would be much, much easier to do. 

 

If by "the length of the input" you mean the number of elements (space and comma delimited - as the alternative length of the string would be trivial to calculate using !LENGHT). See below demo which shows how to get the first (head), last (tail) and count of all the elements provided in the "options" parameter:

 

 

/***********************************************/

define !test(options= !cmdend)

title headOptions=!head(!options).

!let !counter1=!blanks(0)

!do !i !in (!options)

    !let !counter1=!concat(!counter1,!blanks(1))

    !let !nbOptions=!len(!counter1)

!doend

title nbOptions=!nbOptions.

!let !tailOptions=!options

!do !i =2 !to !nbOptions

    !let !tailOptions=!tail(!tailOptions)

!doend

title tailOptions=!tailOptions.

!enddefine.

/***********************************************/

!test options= 1.0 2.0 3.0.

!test options= 1.0 2.0 3.0 4.0 145,140.

/* Notice how "145,140" is interpreted as 3 elements and perhaps not one as might be intended with comma being used as a thousand separator */.

 

 

Now the equivalent of this in python would simply be:

 

/***********************************************/

begin program.

def test(options):

    nbOptions=len(options.split())

    firstOptions=options.split()[0]

    lastOptions=options.split()[-1]

    print "nbOptions=",nbOptions

    print "firstOptions=",firstOptions

    print "lastOptions=",lastOptions

test("1.0 2.0 3.0")

test("1.0 2.0 3.0 4.0 145,140") # correctly interprets 145,140 as a single value (and not 3 values), if indeed that is how you want to interpret comma between numbers

end program.

/***********************************************/

 

 

 

 

 

 

 

 

On 12 February 2015 at 03:58, Alex Huang <[hidden email]> wrote:

Hi,

I am designing a SPSS macro which takes several inputs as parameters. I have
the following:

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)

in which I would like to take the first parameter as var, and all the others
as options. I accept various number of inputs for options here.

!optionAN var = Q1 options = 35 8887 8888.
!optionAN var = Q2 options = 4 8888.

I would like to ask if it is possible to find out:

1. the value of the first or the last inputs for options,
2. the length of the input,

inside the macro so that I can input the values as parameters?

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)
VECTOR var(!length(options)).
VECTOR v = concat(!var, "_1") TO !concat(!var, "_99").
LOOP #i = 1 to (!length(options)).
COMPUTE var(#i) = v(#i).
END LOOP.
!ENDDEFINE.

Thank you.

Note: I have not yet test the code. I am not sure whether it is correct to
write (var = TOKENS(1), options = !CMDEND) there. Please correct it if I
have any mistakes.

Thanks much,
Alex



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/SPSS-macro-parameters-tp5728654.html
Sent from the SPSSX Discussion mailing list archive at 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

 

 

===================== 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

===================== 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: SPSS macro parameters

Jon K Peck
In reply to this post by Alex Huang
Yes, it is certainly possible to set up the R Essentials for Statistics 19.  See the section for R Essentials on this page.

https://www.ibm.com/developerworks/community/wikis/home?lang=en#/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%20for%20IBM%C2%AE%20SPSS%C2%AE%20Statistics

You will need to install the corresponding back version of R to go with this, but you can have multiple versions of R installed if needed.  


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Alex Huang <[hidden email]>
To:        [hidden email]
Date:        02/12/2015 09:41 PM
Subject:        Re: [SPSSX-L] SPSS macro parameters
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Dear Jignesh Sutar,
 
Terribly sorry that I am not familiar with Python but I am experienced with R (I think I am quite familiar, at least I would prefer R than Python). Your suggestion makes me think of an interesting alternative: Is it possible to set up R essentials in SPSS 19 32bit? If it is possible, I would do it in R as I am familiar with what I want to do in my previous question(vector[1], length(vector) vector[length(vector)]).
 
Thanks much,
Alex
 

2015-02-12 15:36 GMT+08:00 Jignesh Sutar <jsutar@...>:
If you are vaguely familiar with Python (which I believe you are) you perhaps ought to be looking at setting this up in python, which would be much, much easier to do. 

If by "the length of the input" you mean the number of elements (space and comma delimited - as the alternative length of the string would be trivial to calculate using !LENGHT). See below demo which shows how to get the first (head), last (tail) and count of all the elements provided in the "options" parameter:


/***********************************************/
define !test(options= !cmdend)
title headOptions=!head(!options).
!let !counter1=!blanks(0)
!do !i !in (!options)
    !let !counter1=!concat(!counter1,!blanks(1))
    !let !nbOptions=!len(!counter1)
!doend
title nbOptions=!nbOptions.
!let !tailOptions=!options
!do !i =2 !to !nbOptions
    !let !tailOptions=!tail(!tailOptions)
!doend
title tailOptions=!tailOptions.
!enddefine.
/***********************************************/
!test options= 1.0 2.0 3.0.
!test options= 1.0 2.0 3.0 4.0 145,140.
/* Notice how "145,140" is interpreted as 3 elements and perhaps not one as might be intended with comma being used as a thousand separator */.


Now the equivalent of this in python would simply be:

/***********************************************/
begin program.
def test(options):
    nbOptions=len(options.split())
    firstOptions=options.split()[0]
    lastOptions=options.split()[-1]
    print "nbOptions=",nbOptions
    print "firstOptions=",firstOptions
    print "lastOptions=",lastOptions
test("1.0 2.0 3.0")
test("1.0 2.0 3.0 4.0 145,140") # correctly interprets 145,140 as a single value (and not 3 values), if indeed that is how you want to interpret comma between numbers
end program.
/***********************************************/








On 12 February 2015 at 03:58, Alex Huang <alexhuangah1@...> wrote:
Hi,

I am designing a SPSS macro which takes several inputs as parameters. I have
the following:

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)

in which I would like to take the first parameter as var, and all the others
as options. I accept various number of inputs for options here.

!optionAN var = Q1 options = 35 8887 8888.
!optionAN var = Q2 options = 4 8888.

I would like to ask if it is possible to find out:

1. the value of the first or the last inputs for options,
2. the length of the input,

inside the macro so that I can input the values as parameters?

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)
VECTOR var(!length(options)).
VECTOR v = concat(!var, "_1") TO !concat(!var, "_99").
LOOP #i = 1 to (!length(options)).
COMPUTE var(#i) = v(#i).
END LOOP.
!ENDDEFINE.

Thank you.

Note: I have not yet test the code. I am not sure whether it is correct to
write (var = TOKENS(1), options = !CMDEND) there. Please correct it if I
have any mistakes.

Thanks much,
Alex



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/SPSS-macro-parameters-tp5728654.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to

LISTSERV@... (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



===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@... (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
===================== 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: SPSS macro parameters

David Marso
Administrator
In reply to this post by Alex Huang

FWIW,
the , in the DEFINE line needs to be replaced by a / !
---------------------------------------------------------------
Alex Huang wrote
Hi,

I am designing a SPSS macro which takes several inputs as parameters. I have the following:

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)

in which I would like to take the first parameter as var, and all the others as options. I accept various number of inputs for options here.

!optionAN var = Q1 options = 35 8887 8888.
!optionAN var = Q2 options = 4 8888.

I would like to ask if it is possible to find out:

1. the value of the first or the last inputs for options,
2. the length of the input,

inside the macro so that I can input the values as parameters?

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)
VECTOR var(!length(options)).
VECTOR v = concat(!var, "_1") TO !concat(!var, "_99").
LOOP #i = 1 to (!length(options)).
COMPUTE var(#i) = v(#i).
END LOOP.
!ENDDEFINE.

Thank you.

Note: I have not yet test the code. I am not sure whether it is correct to write (var = TOKENS(1), options = !CMDEND) there. Please correct it if I have any mistakes.

Thanks much,
Alex
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: SPSS macro parameters

Richard Ristow
In reply to this post by Alex Huang
At 10:58 PM 2/11/2015, Alex Huang wrote:

>I have  the following:
>
>DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)

You mean, I think,

DEFINE !optionAN (var = !TOKENS(1)/ options = !CMDEND)

i.e., adding "!" before "TOKENS(1)", and putting "/" instead of "," after it.

>I would like to take the first parameter as var [or rather, macro
>value !var], and all the others as options [or macro value
>!options]. I accept various number of inputs for options here.
>
>!optionAN var = Q1 options = 35 8887 8888.
>!optionAN var = Q2 options = 4 8888.
>
>I would like to ask if it is possible to find out:
>
>1. the value of the first or the last inputs for options,
>2. the length of the input,

This depends on what you mean by the 'first' and 'last' inputs.  If
you mean the first and last characters, they are respectively

    !LET !FrstChr = (!SUBSTR(!options,1,1))
and
    !LET !LastChr = (!SUBSTR(!options,!LENGTH(!options),1))

If you mean the first and last *tokens*, they are respectively

    !LET !FrstTok = !HEAD(!options)

and, for the last token, a slightly longer calculation (David Marso,
or somebody, may know a better way):

    !DO  !i !IN (!options)
    !LET !LastTok = !i
    !DOEND

Then, using ECHO commands within the macro to print values, you have
the following.  (The macro is defined in the Appendix at the end of this note):

*  Demo I:                                                       ... .
*  Test calls, from original posting:                            ... .
*                                                                ... .
!optionAN var = Q1 options = 35 8887 8888.

var    =Q1
options=35 8887 8888
Within options:
First char =3
Last  char =8
First token=35
Last  token=8888
*                                                                ... .
!optionAN var = Q2 options = 4 8888.

var    =Q2
options=4 8888
Within options:
First char =4
Last  char =8
First token=4
Last  token=8888

But it looks like that's not exactly what you want to do. You say you want,

>DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)
>VECTOR var(!length(options)).
>VECTOR v = concat(!var, "_1") TO !concat(!var, "_99").
>LOOP #i = 1 to (!length(options)).
>COMPUTE var(#i) = v(#i).
>END LOOP.
>!ENDDEFINE.

What is the idea here? You're defining a vector whose root name is
"var", and which has as many member as there are characters in
"options", and then you're assigning its values from the
corresponding members of the vector whose root name is given by
parameter "var". (Sorry, this is confusing, since you use "var" to
mean two different things.)  And the only thing you use from
"options" is its length; why not just pass the integer value as a parameter?

So, clearly I'm not understanding what you're trying to do. Can you
explain a little further?

-Best wishes,
  Richard Ristow
====================
APPENDIX:  Test code
====================
*  C:\Documents and Settings\Richard\My Documents  .
*    \Technical\spssx-l\Z-2015\                    .
*    2011-02-11 Huang-SPSS macro parameters.SPS    .

*  In response to posting                          .
*  Date:    Wed, 11 Feb 2015 20:58:02 -0700        .
*  From:    Alex Huang <[hidden email]>    .
*  Subject: SPSS macro parameters                  .
*  To:      [hidden email]               .

*  "I have  the following:                                           .
*                                                                    .
*  DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)             .
*                                                                    .
*  in which I would like to take the first parameter as var [or      .
*  rather, !var], and all the others as options [or !options]. I     .
*  accept various number of inputs for options here.                 .
*                                                                    .
*  !optionAN var = Q1 options = 35 8887 8888.                        .
*  !optionAN var = Q2 options = 4 8888.                              .
*                                                                    .
*  "I would like to ask if it is possible to find out:               .
*                                                                    .
*  1. the value of the first or the last inputs for options,         .
*  2. the length of the input"                                       .


*  Test I:   First and last inputs, by two definitions:              .

DEFINE !optionAN (var = !TOKENS(1)/ options = !CMDEND)

.  ECHO ' '.
.  ECHO !QUOTE(!CONCAT('var    =',!var)).
.  ECHO !QUOTE(!CONCAT('options=',!options)).

    !LET !FrstChr = (!SUBSTR(!options,1,1))
    !LET !LastChr = (!SUBSTR(!options,!LENGTH(!options),1))

    !LET !FrstTok = !HEAD(!options)

    !DO  !i !IN (!options)
    !LET !LastTok = !i
    !DOEND

.  ECHO 'Within options:'.
.  ECHO !QUOTE(!CONCAT('First char =',!FrstChr)).
.  ECHO !QUOTE(!CONCAT('Last  char =',!LastChr)).
.  ECHO !QUOTE(!CONCAT('First token=',!FrstTok)).
.  ECHO !QUOTE(!CONCAT('Last  token=',!LastTok)).

!ENDDEFINE.

*  Demo I:                                                       ... .
*  Test calls, from original posting:                            ... .
*                                                                ... .
!optionAN var = Q1 options = 35 8887 8888.
*                                                                ... .
!optionAN var = Q2 options = 4 8888.

=====================
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: SPSS macro parameters

Alex Huang
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: SPSS macro parameters

Anton Balabanov-2
Hi, Alex,
 
Doing calculation with macro variables is a long journey as macro language was not designed in this way. But addition is manageable. 
 
You could find here detailed explanations and examples of addition and more complex operations:
 
 
Best regards,
Anton Balabanov
 
16.02.2015, 14:05, "Alex Huang" <[hidden email]>:
Dear all,
 
Thank you very very much for your detailed explanation, especially for Richard. Your corrections on my code have been very helpful and your suggestions have been enlightening.
 
I am doing some option analysis. I would like to count how many options are chosen in extra when that answer is chosen in a multiple respond survey question.
 
With your suggestions, I have got the Macro appended at the end of the message. But the macro call would fail because of the line inside a DO loop:
 
!LET !x = !FirstTok + 1.
 
Here I would like to do a simple addition on macro variables. If !FirstTok is 35, then I would like the first !x inside the loop be 36. Also after the operation:
 
RENAME VARIABLES !concat("var", !x) = !concat(!var, "_ex", !i).
I would like !x increased by 1.
 
So my question is, how to do simple mathematic calculation(addition) on macro variables inside a macro?
 
Any other suggestion on the macro are also welcome! I would thank with appreciation.
 
Thank you very much again.
 
Alex
 
*//////////////////////////////////////////////////////////////////////.
DEFINE !optionAN (var = !TOKENS(1)/ options = !CMDEND)
MISSING VALUES !concat(!var, "_1") TO !concat(!var, "_99") (0).
COMPUTE !concat(!var, "_total") = NVALID(!concat(!var, "_1") TO !concat(!var, "_8888")).
MISSING VALUES !concat(!var, "_99") ().
IF (!concat(!var, "_99") = -99) !concat(!var, "_total") = -99.
EXECUTE .
VARIABLE LABELS !concat(!var, "_total") !concat("Total number of options in ", !var).
!meanfreq var = !concat(!var, "_total").
MISSING VALUES !concat(!var, "_1") TO !concat(!var, "_99") (0).
!LET !FirstTok = !HEAD(!options).
!LET !counter1 = !blanks(!FirstTok).
!DO !i !IN (!options)
!LET !LastTok = !i.
!LET !counter1 = !concat(!counter1,!blanks(1)).
!DOEND.
!LET !numbopts = !len(!counter1).
VECTOR var(!numbopts).
VECTOR v = !concat(!var, "_1") TO !concat(!var, "_99").
LOOP #i = 1 to !numbopts.
COMPUTE var(#i) = v(#i).
END LOOP.
DO REPEAT rep = var1 TO !concat("var", !numbopts).
IF NOT MISSING(rep) rep = NVALID(!concat(!var, "_1") TO !concat(!var, "_99")) - 1.
END REPEAT.
!DO !i = 1 !TO !FirstTok.
RENAME VARIABLES !concat("var", !i) = !concat(!var, "_ex", !i).
!DOEND.
!DO !i !IN (!TAIL(!Options)).
!LET !x = !FirstTok + 1.
RENAME VARIABLES !concat("var", !x) = !concat(!var, "_ex", !i).
!LET !x = !x + 1.
!DOEND.
RENAME VARIABLES !concat("var", !numbopts) = !concat(!var, "_ex99").
EXECUTE.
!meanfreq var = !concat(!var, "_ex1") TO !concat(!var, "_ex99").
DELETE VARIABLES !concat(!var, "_ex1") TO !concat(!var, "_ex99").
EXECUTE.
VARIABLE LEVEL !concat(!var, "_total") (NOMINAL).
VARIABLE WIDTH !concat(!var, "_total") (4).
FORMATS !concat(!var, "_total") (F4.0).
MISSING VALUES !concat(!var, "_total") (-1 -99).
RECODE !concat(!var, "_total") (sysmis = -99).
EXECUTE.
!ENDDEFINE.
*//////////////////////////////////////////////////////////.
 
Note: !freq and !meanfreq are macros written for some descriptive statistics.
 
 

2015-02-16 7:15 GMT+08:00 Richard Ristow <[hidden email]>:
At 10:58 PM 2/11/2015, Alex Huang wrote:

I have  the following:

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)
You mean, I think,

DEFINE !optionAN (var = !TOKENS(1)/ options = !CMDEND)

i.e., adding "!" before "TOKENS(1)", and putting "/" instead of "," after it.

I would like to take the first parameter as var [or rather, macro value !var], and all the others as options [or macro value !options]. I accept various number of inputs for options here.

!optionAN var = Q1 options = 35 8887 8888.
!optionAN var = Q2 options = 4 8888.

I would like to ask if it is possible to find out:

1. the value of the first or the last inputs for options,
2. the length of the input,

This depends on what you mean by the 'first' and 'last' inputs.  If you mean the first and last characters, they are respectively

   !LET !FrstChr = (!SUBSTR(!options,1,1))
and
   !LET !LastChr = (!SUBSTR(!options,!LENGTH(!options),1))

If you mean the first and last *tokens*, they are respectively

   !LET !FrstTok = !HEAD(!options)

and, for the last token, a slightly longer calculation (David Marso, or somebody, may know a better way):

   !DO  !i !IN (!options)
   !LET !LastTok = !i
   !DOEND

Then, using ECHO commands within the macro to print values, you have the following.  (The macro is defined in the Appendix at the end of this note):

*  Demo I:                                                       ... .
*  Test calls, from original posting:                            ... .
*                                                                ... .
!optionAN var = Q1 options = 35 8887 8888.

var    =Q1
options=35 8887 8888
Within options:
First char =3
Last  char =8
First token=35
Last  token=8888
*                                                                ... .
!optionAN var = Q2 options = 4 8888.

var    =Q2
options=4 8888
Within options:
First char =4
Last  char =8
First token=4
Last  token=8888

But it looks like that's not exactly what you want to do. You say you want,

DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)
VECTOR var(!length(options)).
VECTOR v = concat(!var, "_1") TO !concat(!var, "_99").
LOOP #i = 1 to (!length(options)).
COMPUTE var(#i) = v(#i).
END LOOP.
!ENDDEFINE.
What is the idea here? You're defining a vector whose root name is "var", and which has as many member as there are characters in "options", and then you're assigning its values from the corresponding members of the vector whose root name is given by parameter "var". (Sorry, this is confusing, since you use "var" to mean two different things.)  And the only thing you use from "options" is its length; why not just pass the integer value as a parameter?

So, clearly I'm not understanding what you're trying to do. Can you explain a little further?

-Best wishes,
 Richard Ristow
====================
APPENDIX:  Test code
====================
*  C:\Documents and Settings\Richard\My Documents  .
*    \Technical\spssx-l\Z-2015\                    .
*    2011-02-11 Huang-SPSS macro parameters.SPS    .

*  In response to posting                          .
*  Date:    Wed, 11 Feb 2015 20:58:02 -0700        .
*  From:    Alex Huang <[hidden email]>    .
*  Subject: SPSS macro parameters                  .
*  To:      [hidden email]               .

*  "I have  the following:                                           .
*                                                                    .
*  DEFINE !optionAN (var = TOKENS(1), options = !CMDEND)             .
*                                                                    .
*  in which I would like to take the first parameter as var [or      .
*  rather, !var], and all the others as options [or !options]. I     .
*  accept various number of inputs for options here.                 .
*                                                                    .
*  !optionAN var = Q1 options = 35 8887 8888.                        .
*  !optionAN var = Q2 options = 4 8888.                              .
*                                                                    .
*  "I would like to ask if it is possible to find out:               .
*                                                                    .
*  1. the value of the first or the last inputs for options,         .
*  2. the length of the input"                                       .


*  Test I:   First and last inputs, by two definitions:              .

DEFINE !optionAN (var = !TOKENS(1)/ options = !CMDEND)

.  ECHO ' '.
.  ECHO !QUOTE(!CONCAT('var    =',!var)).
.  ECHO !QUOTE(!CONCAT('options=',!options)).

   !LET !FrstChr = (!SUBSTR(!options,1,1))
   !LET !LastChr = (!SUBSTR(!options,!LENGTH(!options),1))

   !LET !FrstTok = !HEAD(!options)

   !DO  !i !IN (!options)
   !LET !LastTok = !i
   !DOEND

.  ECHO 'Within options:'.
.  ECHO !QUOTE(!CONCAT('First char =',!FrstChr)).
.  ECHO !QUOTE(!CONCAT('Last  char =',!LastChr)).
.  ECHO !QUOTE(!CONCAT('First token=',!FrstTok)).
.  ECHO !QUOTE(!CONCAT('Last  token=',!LastTok)).

!ENDDEFINE.

*  Demo I:                                                       ... .
*  Test calls, from original posting:                            ... .
*                                                                ... .
!optionAN var = Q1 options = 35 8887 8888.
*                                                                ... .

!optionAN var = Q2 options = 4 8888.
===================== 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
===================== 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: SPSS macro parameters

Kirill Orlov
Anton,
Do the documents you link to have this one way to do subtraction?

define !subtract(!pos= !token(1) /!pos= !token(1))
!let !x= !blanks(!1)
!let !y= !blanks(!2)
!let !result= !len(!substr(!x, !len(!conc(!y,' '))))
echo !quote(!result).
!enddefine.

!subtract 11 6.


16.02.2015 14:18, Anton Balabanov пишет:
Hi, Alex,
 
Doing calculation with macro variables is a long journey as macro language was not designed in this way. But addition is manageable. 
 
You could find here detailed explanations and examples of addition and more complex operations:
 
 
Best regards,
Anton Balabanov


===================== 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: SPSS macro parameters

David Marso
Administrator

The following should work as well (no need to set up !x and !y).
No SPSS on this box.

define !subtract(!pos= !token(1) /!pos= !token(1))
!let !result= !len(!substr(!blanks(!1), !len(!conc(!blanks(!2),' '))))
echo !quote(!result).
!enddefine.

!subtract 11 6.
Kirill Orlov wrote
Anton,
Do the documents you link to have this one way to do subtraction?

define !subtract(!pos= !token(1) /!pos= !token(1))
!let !x= !blanks(!1)
!let !y= !blanks(!2)
!let !result= !len(!substr(!x, !len(!conc(!y,' '))))
echo !quote(!result).
!enddefine.

!subtract 11 6.


16.02.2015 14:18, Anton Balabanov пишет:
> Hi, Alex,
> Doing calculation with macro variables is a long journey as macro
> language was not designed in this way. But addition is manageable.
> You could find here detailed explanations and examples of addition and
> more complex operations:
> http://spsstools.net/MacroTutorial.htm#DoingArithmetic
> http://spsstools.net/Macros/MacrosVariable/ArithmeticWithMacroVariables.sps
> Best regards,
> Anton Balabanov
>


=====================
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
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: SPSS macro parameters

Anton Balabanov-2
In reply to this post by Alex Huang
The latter, by Paul H. Cook, 2002, does. It suggests method even for integer division and modulus, for braves :)


> Anton,
>
> Do the documents you link to have this one way to do subtraction?
>
> define !subtract(!pos= !token(1) /!pos= !token(1))
>
> !let !x= !blanks(!1)
>
> !let !y= !blanks(!2)
>
> !let !result= !len(!substr(!x, !len(!conc(!y,' '))))
>
> echo !quote(!result).
>
> !enddefine.
>
> !subtract 11 6.
>
> 16.02.2015 14:18, Anton Balabanov
> пишет:
>
>> Hi, Alex,
>>
>> Doing calculation with macro variables is a long journey as
>> macro language was not designed in this way. But addition is
>> manageable.
>>
>> You could find here detailed explanations and examples of
>> addition and more complex operations:
>>
>> http://spsstools.net/MacroTutorial.htm#DoingArithmetic
>>
>> http://spsstools.net/Macros/MacrosVariable/ArithmeticWithMacroVariables.sps
>>
>> Best regards,
>>
>> Anton Balabanov

=====================
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: SPSS macro parameters

Art Kendall
In reply to this post by Alex Huang
Maybe I did not understand your post.  But it seems that you could just use the MULT RESPONSE procedure.

Depending what you are trying to do  use MULT RESPONSE and crosstab a set by itself  or
treat the first response variable as a single variable and the rest of the variables as a set; then cross tab the variable by the set.

It is also likely that you could use TABLES or CTABLES without writing any macros.
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: SPSS macro parameters

Alex Huang
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: SPSS macro parameters

David Marso
Administrator
Suggestions:
1.  Get rid of the EXECUTE commands.
2.  Add a BLANK line between the macros.
3.  Add comments to the code so it is easily understandable with respect to intention, assumptions, side effects....
/* This macro does blah blah blah.
It requires the following blah blah blah as INPUT.  The first blah blah blah denotes ... the second blah blah blah.....
It creates the following <output>
It has the following side blah blah blah effects on the data .
If you don't intend to actually create any new variables in the data file consider using TEMPORARY.  Then you can delete the DELETE VARIABLES command and indeed ALL execute statements.
After you comment the code, I may have further suggestions.  At this point I don't have the time to spend to try to guess why/what you are doing/trying to do.

Alex Huang wrote
Dear all,

Deep thanks to everyone, now my macro is ready to go. (I append my macro at
the end.) The output is perfect now.

I have no further questions now. But I agree that the macro can be
improved. So further suggestions are welcome and are appreciated. We can
discuss and make things perfect.

Dear Art,

Thank you for your suggestions. This is my pleasure that I could explain
further for you.

I usually do option analysis after conducting MULT RESPONSE. The point is,
I count the number of extra options only if that particular choice is
valid.

For example, in a multiple response question, one of the responses is 1, 2,
6 among 1, 2, 3, 4, 5, 6, 8887, 8888, -99. I would count 'total' as 3 and
'ex1', 'ex2', 'ex6' (extra # options) as 2 while all the others are missing.

Also, I do it for all 1, 2, ... , -99. That's why the analysis seems
complicated. I wonder if it is possible to construct a more
simple syntax for this?

Thanks everyone again.

Alex

append: MAoptAN.sps
*//////////////////////////////////////////////////.
DATA LIST LIST
 / Q1_1 Q1_2 Q1_3 Q1_8887 Q1_8888 Q1_99.
BEGIN DATA.
1,0,3,8887,0,0
0,2,3,0,0,0
0,2,3,0,0,0
0,0,3,8887,0,0
0,0,0,0,8888,0
0,0,0,0,0,-99
END DATA.
EXECUTE.
DEFINE !othersfreq (var=!CMDEND)
OMS /select tables   /destination viewer = no /if labels='Statistics' /tag=
'dsSuppress'.
!DO !i !IN (!var)
FREQUENCIES VARIABLES=!i
  /ORDER=ANALYSIS.
!DOEND.
OMSEND TAG = ['dsSuppress'].
!ENDDEFINE.
DEFINE !freq (var=!CMDEND)
missing values !var (-99).
execute.
!othersfreq var = !var.
!ENDDEFINE.
DEFINE !meanfreq (var=!CMDEND)
missing values !var (-99 8888).
FREQUENCIES VARIABLES=!var
 /STATISTICS=STDDEV MINIMUM MAXIMUM SEMEAN MEAN MEDIAN MODE SUM
 /FORMAT=NOTABLE
 /ORDER=ANALYSIS.
!ENDDEFINE.
DEFINE !optionAN (var = !TOKENS(1)/ options = !CMDEND)
MISSING VALUES !concat(!var, "_1") TO !concat(!var, "_99") (0).
COMPUTE !concat(!var, "_total") = NVALID(!concat(!var, "_1") TO
!concat(!var, "_8888")).
MISSING VALUES !concat(!var, "_99") ().
IF (!concat(!var, "_99") = -99) !concat(!var, "_total") = -99.
EXECUTE .
VARIABLE LABELS !concat(!var, "_total") !concat("Total number of options in
", !var).
!meanfreq var = !concat(!var, "_total").
MISSING VALUES !concat(!var, "_1") TO !concat(!var, "_99") (0).
!LET !FirstTok = !HEAD(!options).
!LET !counter1 = !blanks(!FirstTok).
!DO !i !IN (!options)
!LET !LastTok = !i.
!LET !counter1 = !concat(!counter1,!blanks(1)).
!DOEND.
!LET !numbopts = !len(!counter1).
VECTOR var(!numbopts).
VECTOR v = !concat(!var, "_1") TO !concat(!var, "_99").
LOOP #i = 1 to !numbopts.
COMPUTE var(#i) = v(#i).
END LOOP.
DO REPEAT rep = var1 TO !concat("var", !numbopts).
IF NOT MISSING(rep) rep = NVALID(!concat(!var, "_1") TO !concat(!var,
"_99")) - 1.
END REPEAT.
!DO !i = 1 !TO !FirstTok.
RENAME VARIABLES !concat("var", !i) = !concat(!var, "_ex", !i).
!DOEND.
!LET !x = !len(!concat(!blanks(!FirstTok), !blanks(1))).
!DO !i !IN (!TAIL(!Options)).
RENAME VARIABLES !concat("var", !x) = !concat(!var, "_ex", !i).
!LET !x = !len(!concat(!blanks(!x), !blanks(1))).
!DOEND.
RENAME VARIABLES !concat("var", !numbopts) = !concat(!var, "_ex99").
EXECUTE.
!meanfreq var = !concat(!var, "_ex1") TO !concat(!var, "_ex99").
DELETE VARIABLES !concat(!var, "_ex1") TO !concat(!var, "_ex99").
EXECUTE.
VARIABLE LEVEL !concat(!var, "_total") (NOMINAL).
VARIABLE WIDTH !concat(!var, "_total") (4).
FORMATS !concat(!var, "_total") (F4.0).
MISSING VALUES !concat(!var, "_total") (-1 -99).
RECODE !concat(!var, "_total") (sysmis = -99).
EXECUTE.
!ENDDEFINE.
!optionAN var = Q1 options = 3 8887 8888.
*/////////////////////////////////////////////////////////////////////////////////////.


2015-02-17 19:06 GMT+08:00 Alex Huang <[hidden email]>:

> Dear all,
>
> Deep thanks to everyone, now my macro is ready to go. (I append my macro
> at the end.)
>
> I attached also an example dataset for illustration. The output is perfect
> now.
>
> I have no further questions now. But I agree that the macro can be
> improved. So further suggestions are welcome and are appreciated. We can
> discuss and make things perfect.
>
> Dear Art,
>
> Thank you for your suggestions. This is my pleasure that I could explain
> further for you.
>
> I usually do option analysis after conducting MULT RESPONSE. The point is,
> I count the number of extra options only if that particular choice is
> valid.
>
> For example, in a multiple response question, one of the responses is 1,
> 2, 6 among 1, 2, 3, 4, 5, 6, 8887, 8888, -99. I would count 'total' as 3
> and 'ex1', 'ex2', 'ex6' (extra # options) as 2 while all the others are
> missing.
>
> Also, I do it for all 1, 2, ... , -99. That's why the analysis seems
> complicated. I wonder if it is possible to construct a more
> simple syntax for this?
>
> Thanks everyone again.
>
> Alex
>
> append: MAoptAN.sps
> *//////////////////////////////////////////////////.
> DEFINE !othersfreq (var=!CMDEND)
> OMS /select tables   /destination viewer = no /if labels='Statistics'
> /tag= 'dsSuppress'.
> !DO !i !IN (!var)
> FREQUENCIES VARIABLES=!i
>   /ORDER=ANALYSIS.
> !DOEND.
> OMSEND TAG = ['dsSuppress'].
> !ENDDEFINE.
> DEFINE !freq (var=!CMDEND)
> missing values !var (-99).
> execute.
> !othersfreq var = !var.
> !ENDDEFINE.
> DEFINE !meanfreq (var=!CMDEND)
> missing values !var (-99 8888).
> FREQUENCIES VARIABLES=!var
>  /STATISTICS=STDDEV MINIMUM MAXIMUM SEMEAN MEAN MEDIAN MODE SUM
>  /FORMAT=NOTABLE
>  /ORDER=ANALYSIS.
> !ENDDEFINE.
> DEFINE !optionAN (var = !TOKENS(1)/ options = !CMDEND)
> MISSING VALUES !concat(!var, "_1") TO !concat(!var, "_99") (0).
> COMPUTE !concat(!var, "_total") = NVALID(!concat(!var, "_1") TO
> !concat(!var, "_8888")).
> MISSING VALUES !concat(!var, "_99") ().
> IF (!concat(!var, "_99") = -99) !concat(!var, "_total") = -99.
> EXECUTE .
> VARIABLE LABELS !concat(!var, "_total") !concat("Total number of options
> in ", !var).
> !meanfreq var = !concat(!var, "_total").
> MISSING VALUES !concat(!var, "_1") TO !concat(!var, "_99") (0).
> !LET !FirstTok = !HEAD(!options).
> !LET !counter1 = !blanks(!FirstTok).
> !DO !i !IN (!options)
> !LET !LastTok = !i.
> !LET !counter1 = !concat(!counter1,!blanks(1)).
> !DOEND.
> !LET !numbopts = !len(!counter1).
> VECTOR var(!numbopts).
> VECTOR v = !concat(!var, "_1") TO !concat(!var, "_99").
> LOOP #i = 1 to !numbopts.
> COMPUTE var(#i) = v(#i).
> END LOOP.
> DO REPEAT rep = var1 TO !concat("var", !numbopts).
> IF NOT MISSING(rep) rep = NVALID(!concat(!var, "_1") TO !concat(!var,
> "_99")) - 1.
> END REPEAT.
> !DO !i = 1 !TO !FirstTok.
> RENAME VARIABLES !concat("var", !i) = !concat(!var, "_ex", !i).
> !DOEND.
> !LET !x = !len(!concat(!blanks(!FirstTok), !blanks(1))).
> !DO !i !IN (!TAIL(!Options)).
> RENAME VARIABLES !concat("var", !x) = !concat(!var, "_ex", !i).
> !LET !x = !len(!concat(!blanks(!x), !blanks(1))).
> !DOEND.
> RENAME VARIABLES !concat("var", !numbopts) = !concat(!var, "_ex99").
> EXECUTE.
> !meanfreq var = !concat(!var, "_ex1") TO !concat(!var, "_ex99").
> DELETE VARIABLES !concat(!var, "_ex1") TO !concat(!var, "_ex99").
> EXECUTE.
> VARIABLE LEVEL !concat(!var, "_total") (NOMINAL).
> VARIABLE WIDTH !concat(!var, "_total") (4).
> FORMATS !concat(!var, "_total") (F4.0).
> MISSING VALUES !concat(!var, "_total") (-1 -99).
> RECODE !concat(!var, "_total") (sysmis = -99).
> EXECUTE.
> !ENDDEFINE.
> !optionAN var = Q1 options = 3 8887 8888.
>
> */////////////////////////////////////////////////////////////////////////////////////.
>
>
> 2015-02-17 5:52 GMT+08:00 Art Kendall <[hidden email]>:
>
>> Maybe I did not understand your post.  But it seems that you could just
>> use
>> the MULT RESPONSE procedure.
>>
>> Depending what you are trying to do  use MULT RESPONSE and crosstab a set
>> by
>> itself  or
>> treat the first response variable as a single variable and the rest of the
>> variables as a set; then cross tab the variable by the set.
>>
>> It is also likely that you could use TABLES or CTABLES without writing any
>> macros.
>>
>>
>>
>> -----
>> Art Kendall
>> Social Research Consultants
>> --
>> View this message in context:
>> http://spssx-discussion.1045642.n5.nabble.com/SPSS-macro-parameters-tp5728654p5728708.html
>> Sent from the SPSSX Discussion mailing list archive at 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
>>
>
>

=====================
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
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?"