Quantcast

IF statements in SPSS macros

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

IF statements in SPSS macros

Sundance Kid
This post has NOT been accepted by the mailing list yet.
I read that conditional processing in SPSS macros takes this form:

!IF (expression) !THEN statements
!ELSE other statements
!IFEND

However when I try to apply this to my datasets - I can't make it work. I am processing a large number of data sets. They all have the same variable fields but for one variable in consideration for some data sets that variable field will not be populated. When this variable is populated I want the macro to run certain section of code then when the same variable is not populated in another data set the macro will run another section of code. So I have started simple by just testing out my macro using the frequencies command.

So I wrote this:

DEFINE macro23 ()

!IF (variable1 = !NULL) !THEN

FREQUENCIES VARIABLES=Age
  /ORDER=ANALYSIS.

!ELSE

FREQUENCIES VARIABLES=Gender
  /ORDER=ANALYSIS.

!IFEND

!ENDDEFINE.

macro23



variable1 is the variable that will be blank for some datasets and populated for others. So what my code says is that when variable1 is blank (null) run a frequency on Age otherwise if it is not blank run a frequency on Gender. I notice that when variable1 is system missing I get a frequency on Gender. I expect to get a frequency on age when variable1 is system missing. How can I make it work so I get a frequency on Age when variable1 is not populated. variable1 is a numeric field but I would be interested in seeing what the command would be (if it is any different) if variable1 were to be string.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: IF statements in SPSS macros

David Marso
Administrator
Nope.  It doesn't work that way and there is no reason to think it should.  Think about it.

!IF (some condition involving some !macro object) !THEN
<Do something>
!ELSE
<do something else>
!IFEND


DEFINE !blah1 ()
something
!ENDDEFINE.

DEFINE !blah2 ()
somethingelse
!ENDDEFINE.

DEFINE !Test (arg !TOKENS(1) )
!IF (!arg !EQ 'T') !THEN
!blah1 .
!ELSE
!blah2 .
!IFEND
!ENDDEFINE.


Besides things like FREQUENCIES operate on the entire active case set, you seem to think it would somehow be triggered by a value on a single case.


i.e.  Macro has absolutely NO clue about contents/values of variables, metadata etc.
in fact that damned thing doesn't even know how to add 2 + 2 without having to pull off some silly shit.
Of course there is NO way to deliver a return value so it needs to be inline ;-(.

DEFINE !add (!pos !TOKENS(1) / !pos !TOKENS(1) )
!LET !sum =!CONCAT(!LENGTH(!BLANKS(!1),!BLANKS(!1))
ECHO !QUOTE(!sum)
!ENDDEFINE.

!add 2 2 .


HTH, DMM

Sundance Kid wrote
I read that conditional processing in SPSS macros takes this form:

!IF (expression) !THEN statements
!ELSE other statements
!IFEND

However when I try to apply this to my datasets - I can't make it work. I am processing a large number of data sets. They all have the same variable fields but for one variable in consideration for some data sets that variable field will not be populated. When this variable is populated I want the macro to run certain section of code then when the same variable is not populated in another data set the macro will run another section of code. So I have started simple by just testing out my macro using the frequencies command.

So I wrote this:

DEFINE macro23 ()

!IF (variable1 = !NULL) !THEN

FREQUENCIES VARIABLES=Age
  /ORDER=ANALYSIS.

!ELSE

FREQUENCIES VARIABLES=Gender
  /ORDER=ANALYSIS.

!IFEND

!ENDDEFINE.

macro23



variable1 is the variable that will be blank for some datasets and populated for others. So what my code says is that when variable1 is blank (null) run a frequency on Age otherwise if it is not blank run a frequency on Gender. I notice that when variable1 is system missing I get a frequency on Gender. I expect to get a frequency on age when variable1 is system missing. How can I make it work so I get a frequency on Age when variable1 is not populated. variable1 is a numeric field but I would be interested in seeing what the command would be (if it is any different) if variable1 were to be string.
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
|  
Report Content as Inappropriate

Re: IF statements in SPSS macros

Jon Peck
As many know, the Python programmability plugin enables this sort of logic while macro does not.  The Programming and Data Management book downloadable from

has extensive information on using Python with Statistics (among other topics).

On Thu, Mar 9, 2017 at 6:50 AM, David Marso <[hidden email]> wrote:
Nope.  It doesn't work that way and there is no reason to think it should.
Think about it.

!IF (some condition involving some !macro object) !THEN
<Do something>
!ELSE
<do something else>
!IFEND


DEFINE !blah1 ()
something
!ENDDEFINE.

DEFINE !blah2 ()
somethingelse
!ENDDEFINE.

DEFINE !Test (arg !TOKENS(1) )
!IF (!arg !EQ 'T') !THEN
!blah1 .
!ELSE
!blah2 .
!IFEND
!ENDDEFINE.


Besides things like FREQUENCIES operate on the entire active case set, you
seem to think it would somehow be triggered by a value on a single case.


i.e.  Macro has absolutely NO clue about contents/values of variables,
metadata etc.
in fact that damned thing doesn't even know how to add 2 + 2 without having
to pull off some silly shit.
Of course there is NO way to deliver a return value so it needs to be inline
;-(.

DEFINE !add (!pos !TOKENS(1) / !pos !TOKENS(1) )
!LET !sum =!CONCAT(!LENGTH(!BLANKS(!1),!BLANKS(!1))
ECHO !QUOTE(!sum)
!ENDDEFINE.

!add 2 2 .


HTH, DMM


Sundance Kid wrote
> I read that conditional processing in SPSS macros takes this form:
>
> !IF (expression) !THEN statements
> !ELSE other statements
> !IFEND
>
> However when I try to apply this to my datasets - I can't make it work. I
> am processing a large number of data sets. They all have the same variable
> fields but for one variable in consideration for some data sets that
> variable field will not be populated. When this variable is populated I
> want the macro to run certain section of code then when the same variable
> is not populated in another data set the macro will run another section of
> code. So I have started simple by just testing out my macro using the
> frequencies command.
>
> So I wrote this:
>
> DEFINE macro23 ()
>
> !IF (variable1 = !NULL) !THEN
>
> FREQUENCIES VARIABLES=Age
>   /ORDER=ANALYSIS.
>
> !ELSE
>
> FREQUENCIES VARIABLES=Gender
>   /ORDER=ANALYSIS.
>
> !IFEND
>
> !ENDDEFINE.
>
> macro23
>
>
>
> variable1 is the variable that will be blank for some datasets and
> populated for others. So what my code says is that when variable1 is blank
> (null) run a frequency on Age otherwise if it is not blank run a frequency
> on Gender. I notice that when variable1 is system missing I get a
> frequency on Gender. I expect to get a frequency on age when variable1 is
> system missing. How can I make it work so I get a frequency on Age when
> variable1 is not populated. variable1 is a numeric field but I would be
> interested in seeing what the command would be (if it is any different) if
> variable1 were to be string.





-----
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?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/IF-statements-in-SPSS-macros-tp5733975p5733976.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



--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: IF statements in SPSS macros

David Marso
Administrator
Utterly trivial to employ the 'horrible hack' to aggregate the files and build macro code for insertion contextually WRT the variable state.  You would have to do something within python to determine that the variable is empty.  Is there a simple function for that?
I was addressing the metaproblem of building procedural logic around individual case states.
Really a mixup of levels.
--
Jon Peck wrote
As many know, the Python programmability plugin enables this sort of logic
while macro does not.  The Programming and Data Management book
downloadable from
https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityUuid=ab16c38e-2f7b-4912-a47e-85682d124d32#fullpageWidgetId=W7de6c01e7f8d_483f_820f_d9d2108adc61&file=4070fa78-70d2-40bd-9908-77a856cfd7c8

has extensive information on using Python with Statistics (among other
topics).

On Thu, Mar 9, 2017 at 6:50 AM, David Marso <[hidden email]> wrote:

> Nope.  It doesn't work that way and there is no reason to think it should.
> Think about it.
>
> !IF (some condition involving some !macro object) !THEN
> <Do something>
> !ELSE
> <do something else>
> !IFEND
>
>
> DEFINE !blah1 ()
> something
> !ENDDEFINE.
>
> DEFINE !blah2 ()
> somethingelse
> !ENDDEFINE.
>
> DEFINE !Test (arg !TOKENS(1) )
> !IF (!arg !EQ 'T') !THEN
> !blah1 .
> !ELSE
> !blah2 .
> !IFEND
> !ENDDEFINE.
>
>
> Besides things like FREQUENCIES operate on the entire active case set, you
> seem to think it would somehow be triggered by a value on a single case.
>
>
> i.e.  Macro has absolutely NO clue about contents/values of variables,
> metadata etc.
> in fact that damned thing doesn't even know how to add 2 + 2 without having
> to pull off some silly shit.
> Of course there is NO way to deliver a return value so it needs to be
> inline
> ;-(.
>
> DEFINE !add (!pos !TOKENS(1) / !pos !TOKENS(1) )
> !LET !sum =!CONCAT(!LENGTH(!BLANKS(!1),!BLANKS(!1))
> ECHO !QUOTE(!sum)
> !ENDDEFINE.
>
> !add 2 2 .
>
>
> HTH, DMM
>
>
> Sundance Kid wrote
> > I read that conditional processing in SPSS macros takes this form:
> >
> > !IF (expression) !THEN statements
> > !ELSE other statements
> > !IFEND
> >
> > However when I try to apply this to my datasets - I can't make it work. I
> > am processing a large number of data sets. They all have the same
> variable
> > fields but for one variable in consideration for some data sets that
> > variable field will not be populated. When this variable is populated I
> > want the macro to run certain section of code then when the same variable
> > is not populated in another data set the macro will run another section
> of
> > code. So I have started simple by just testing out my macro using the
> > frequencies command.
> >
> > So I wrote this:
> >
> > DEFINE macro23 ()
> >
> > !IF (variable1 = !NULL) !THEN
> >
> > FREQUENCIES VARIABLES=Age
> >   /ORDER=ANALYSIS.
> >
> > !ELSE
> >
> > FREQUENCIES VARIABLES=Gender
> >   /ORDER=ANALYSIS.
> >
> > !IFEND
> >
> > !ENDDEFINE.
> >
> > macro23
> >
> >
> >
> > variable1 is the variable that will be blank for some datasets and
> > populated for others. So what my code says is that when variable1 is
> blank
> > (null) run a frequency on Age otherwise if it is not blank run a
> frequency
> > on Gender. I notice that when variable1 is system missing I get a
> > frequency on Gender. I expect to get a frequency on age when variable1 is
> > system missing. How can I make it work so I get a frequency on Age when
> > variable1 is not populated. variable1 is a numeric field but I would be
> > interested in seeing what the command would be (if it is any different)
> if
> > variable1 were to be string.
>
>
>
>
>
> -----
> 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?"
> --
> View this message in context: http://spssx-discussion.
> 1045642.n5.nabble.com/IF-statements-in-SPSS-macros-tp5733975p5733976.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
>



--
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
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
|  
Report Content as Inappropriate

Re: IF statements in SPSS macros

Jon Peck
spssaux2.FindEmptyVars("variablename")

From the docstring:
Scan specified or all variables and determine which are missing or blank for all cases. 
    Return list of names and optionally delete those variables.

So something like (not run)

begin program.
import spss, spssaux2

if spssaux2.FindEmptyVars('variable1'', alpha=True):
    spss.Submit("""FREQ Age""")
else:
    spss.Submit("""FREQ Gender""")
end program.

The alpha parameter says to include string variables in the scan, so if works whether variable1 is numeric or string.  The return value from FindEmptyVars will test as true if the variable is empty and otherwise False.

On Thu, Mar 9, 2017 at 9:04 AM, David Marso <[hidden email]> wrote:
Utterly trivial to employ the 'horrible hack' to aggregate the files and
build macro code for insertion contextually WRT the variable state.  You
would have to do something within python to determine that the variable is
empty.  Is there a simple function for that?
I was addressing the metaproblem of building procedural logic around
individual case states.
Really a mixup of levels.
--

Jon Peck wrote
> As many know, the Python programmability plugin enables this sort of logic
> while macro does not.  The Programming and Data Management book
> downloadable from
> https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityUuid=ab16c38e-2f7b-4912-a47e-85682d124d32#fullpageWidgetId=W7de6c01e7f8d_483f_820f_d9d2108adc61&file=4070fa78-70d2-40bd-9908-77a856cfd7c8
>
> has extensive information on using Python with Statistics (among other
> topics).
>
> On Thu, Mar 9, 2017 at 6:50 AM, David Marso &lt;

> david.marso@

> &gt; wrote:
>
>> Nope.  It doesn't work that way and there is no reason to think it
>> should.
>> Think about it.
>>
>> !IF (some condition involving some !macro object) !THEN
>>
> <Do something>
>> !ELSE
>>
> <do something else>
>> !IFEND
>>
>>
>> DEFINE !blah1 ()
>> something
>> !ENDDEFINE.
>>
>> DEFINE !blah2 ()
>> somethingelse
>> !ENDDEFINE.
>>
>> DEFINE !Test (arg !TOKENS(1) )
>> !IF (!arg !EQ 'T') !THEN
>> !blah1 .
>> !ELSE
>> !blah2 .
>> !IFEND
>> !ENDDEFINE.
>>
>>
>> Besides things like FREQUENCIES operate on the entire active case set,
>> you
>> seem to think it would somehow be triggered by a value on a single case.
>>
>>
>> i.e.  Macro has absolutely NO clue about contents/values of variables,
>> metadata etc.
>> in fact that damned thing doesn't even know how to add 2 + 2 without
>> having
>> to pull off some silly shit.
>> Of course there is NO way to deliver a return value so it needs to be
>> inline
>> ;-(.
>>
>> DEFINE !add (!pos !TOKENS(1) / !pos !TOKENS(1) )
>> !LET !sum =!CONCAT(!LENGTH(!BLANKS(!1),!BLANKS(!1))
>> ECHO !QUOTE(!sum)
>> !ENDDEFINE.
>>
>> !add 2 2 .
>>
>>
>> HTH, DMM
>>
>>
>> Sundance Kid wrote
>> > I read that conditional processing in SPSS macros takes this form:
>> >
>> > !IF (expression) !THEN statements
>> > !ELSE other statements
>> > !IFEND
>> >
>> > However when I try to apply this to my datasets - I can't make it work.
>> I
>> > am processing a large number of data sets. They all have the same
>> variable
>> > fields but for one variable in consideration for some data sets that
>> > variable field will not be populated. When this variable is populated I
>> > want the macro to run certain section of code then when the same
>> variable
>> > is not populated in another data set the macro will run another section
>> of
>> > code. So I have started simple by just testing out my macro using the
>> > frequencies command.
>> >
>> > So I wrote this:
>> >
>> > DEFINE macro23 ()
>> >
>> > !IF (variable1 = !NULL) !THEN
>> >
>> > FREQUENCIES VARIABLES=Age
>> >   /ORDER=ANALYSIS.
>> >
>> > !ELSE
>> >
>> > FREQUENCIES VARIABLES=Gender
>> >   /ORDER=ANALYSIS.
>> >
>> > !IFEND
>> >
>> > !ENDDEFINE.
>> >
>> > macro23
>> >
>> >
>> >
>> > variable1 is the variable that will be blank for some datasets and
>> > populated for others. So what my code says is that when variable1 is
>> blank
>> > (null) run a frequency on Age otherwise if it is not blank run a
>> frequency
>> > on Gender. I notice that when variable1 is system missing I get a
>> > frequency on Gender. I expect to get a frequency on age when variable1
>> is
>> > system missing. How can I make it work so I get a frequency on Age when
>> > variable1 is not populated. variable1 is a numeric field but I would be
>> > interested in seeing what the command would be (if it is any different)
>> if
>> > variable1 were to be string.
>>
>>
>>
>>
>>
>> -----
>> 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?"
>> --
>> View this message in context: http://spssx-discussion.
>> 1045642.n5.nabble.com/IF-statements-in-SPSS-macros-tp5733975p5733976.html
>> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

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

> jkpeck@

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

> LISTSERV@.UGA

>  (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?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/IF-statements-in-SPSS-macros-tp5733975p5733978.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



--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: IF statements in SPSS macros

Kirill Orlov
In reply to this post by David Marso
In addition to David's:
Of course, MATRIX-END MATRIX could be effectively used instead of Python to do the OP's task. MATRIX, however, supports variable names (as well as string values) up to 8 bytes (8 characters) length only. And MATRIX has no access to metadata.


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