CONDITIONAL MACRO

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

CONDITIONAL MACRO

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

Re: CONDITIONAL MACRO

David Marso
Administrator
You need to indicate the nature of your difficulty.  You do not appear to have attempted to incorporate your conditional situation into the macro.  I solved your previous question to get you started.  Make some attempt yourself and post whatever errors you might encounter.
Meanwhile, see !DO !DOEND.  Note the control variable in your !DO !IN loop is called !D.  That will be relevant.
---
deepanshu bhalla wrote
Hi Team,

I have a macro which converts excel files into sav format and then combine cases with the standard template using Match FILES and then save the spss file with the name of demographics .

My requirement is computing and recoding differently in each files. I found difficulty of creating the code for the specific.

For example :

Do If filename = Book1
recode Age ( 1 2 =5 ).
comp SNO =1.

ELSE IF filename = Book2
recode Gender ( 1 2 =5 ).
comp SNO =2.

ENDIF.

Below the existing spss macro code :

DEFINE !demo (demogs=!CMDEND)
!DO !D !IN (!demogs)

****Define paths here********.
!LET !fname=!QUOTE(!CONCAT('C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\',!D,'.SAV'))
!LET !excel=!QUOTE(!CONCAT('C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\',!D,'.xlsx'))


GET DATA /TYPE=XLSX
  /FILE=!excel.


SAVE OUTFILE=!fname.

MATCH Files file = "C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\Template.sav"
/file = !fname.

compute FIRST = Q1-5.
compute Second = FIRST.
execute.

 
SAVE OUTFILE=!QUOTE(!CONCAT('C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\',!D,'_ENG.SAV')).
!DOEND
!ENDDEFINE.

*****Define File Names**************.
!demo demogs = Book1 Book2 Book3.


Thanks in advance !!
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: CONDITIONAL MACRO

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

Re: CONDITIONAL MACRO

David Marso
Administrator
That should tell you something rather subtle about how SPSS operates?
Consider the following abstraction:
It raises error for the DO IF () .. But not for the !IF..()
Basically the first RECODE is *PARSED* and analyzed/evaluated independently of any transformational conditional statement.
Within a MACRO !IF-!IFEND  block all code is simply *ignored* if the *STRING COMPARISON* fails.
I emphasize *STRING COMPARISON* because if you do this long enough you will arrive at the startling revelation that !IF (12 !LT 2) ...will evaluate to *TRUE!!!*.
Moral:  Either live with the errors for the files which don't have the variable present or use the !IF-!IFEND version.
**PLEASE USE**
SET MPRINT ON PRINTBACK ON.
when attempting to degub macros!!!
---
DATA LIST FREE /X.
BEGIN DATA
1
END DATA.

DEFINE BLAH ( Xblah !TOKENS(1)).
PRINT "1".
DO IF (!XBlah EQ "1X").
RECODE SEX(1=2).
END IF.

PRINT "2".
!IF (!XBlah !EQ "1X") !THEN.
RECODE SEX=(1=2).
!IFEND.
!ENDDEFINE.
SET MPRINT ON PRINTBACK ON.
BLAH Xblah "XX".
deepanshu bhalla wrote
When i am using DO IF command instead of !IF !THEN in my macro i am getting the below error 


>Error # 4631 in column 8.  Text: Age 
>On the RECODE command, the list of variables to be recoded includes the name 
>of a nonexistent variable. 
>This command not executed

Note: Age variable exists in the file and the command has been executed but still the error showing
It seems SPSS is unable to read the specific file so it checks all the files and throws errors when age variable not appear in the file so it is throwing the same error twice because Age variable does not exist in the other 2 files .

BELOW THE CODE i.e NOT WORKING :

DO IF !D EQ '2014_Deep_10457'. 
recode Age ( 1 2 =5 ).
comp SNO =1.
END IF.

BELOW THE CODE i.e WORKING :

!IF (!D = !QUOTE('2014_Deep_10457')) !THEN
recode Age ( 1 2 =5 ).
comp SNO =1.
!IFEND.


BELOW MY EXISTING MACRO :

DEFINE !demo (demogs=!CMDEND)
!DO !D !IN (!demogs)

****Define paths here********.
!LET !fname=!QUOTE(!CONCAT('C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\',!UNQUOTE(!D),'.SAV'))
!LET !excel=!QUOTE(!CONCAT('C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\',!UNQUOTE(!D),'.xlsx'))


GET DATA /TYPE=XLSX
  /FILE=!excel.

DO IF !D EQ '2014_Deep_10457'. 
recode Age ( 1 2 =5 ).
comp SNO =1.
END IF.

SAVE OUTFILE=!fname.

MATCH Files file = "C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\Template.sav"
/file = !fname.

compute FIRST = Q1-5.
compute Second = FIRST.
execute.

 
SAVE OUTFILE=!QUOTE(!CONCAT('C:\Documents and Settings\Deepanshu\Desktop\SPSS FILES\',!UNQUOTE(!D),'_ENG.SAV')).
!DOEND
!ENDDEFINE.

*****Define File Names**************.
!demo demogs = '2014_Deep_1015' '2014_Deep_10457' '2014_Deep_101584'.



________________________________
 From: David Marso <[hidden email]>
To: deepanshu bhalla <[hidden email]>
Sent: Thursday, 8 March 2012 1:53 AM
Subject: Re: CONDITIONAL MACRO
 
Hi,
Glad of be of help.
Best to post replies to the list so everyone can benefit.
Note:  There is no need to use macro !IF.  In this case can just use
standard transformational DO IF.
DO IF (!QUOTE(!D) EQ "Book1").
....
This form also permits the use of the ever handy ANY statement.
DO IF (!QUOTE(!D),"Book1","Book5",......).
...
HTH, David


On Wed, Mar 7, 2012 at 2:14 PM, deepanshu bhalla
<[hidden email]> wrote:
> Thanks DM once again...Believe me i attempted to do the task myself before
> posting and used the below code and it was not working that time and it has
> now started working perfectly fine ( I know i sound stupid ).
>
> !IF (!D = Book3) !THEN
> recode Age ( 1 2 =5 ).
> comp SNO =1.
> !IFEND.
>
>
> If you have any better code on your mind please suggest me ..Thanks in
> advance !!
>
>
>
>
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: CONDITIONAL MACRO

Deepanshu Bhalla
CONTENTS DELETED
The author has deleted this message.