Macro Question: Conditionally Saving Outfiles

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

Macro Question: Conditionally Saving Outfiles

Jeanne Eidex

Hi Everyone,

 

I am writing a program that needs to parse out data into separate files in the end.  The syntax works, but, the problem is that it writes an empty data file if conditions are not met, so, if my master file for this month does not have anyone (a row) with group2=1 then it writes an empty data file called group2.sav.  There are a large number of groups, so, it’s very inconvenient to have the empty data files.

 

This would be in a loop or do repeat statement but you can’t combine it with “save outfile” I thought maybe a macro fix this, it might make it more efficient, possibly with the ERASE command which I have never used.  Please let me know if you have any suggestions. 

 

temporary.

select if GROUP1=1.

save outfile='e:\TEST\GROUP1.sav'

/KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.

 

temporary.

select if GROUP2=1.

save outfile='e:\TEST\GROUP2.sav'

/KEEP= RESPID GROUP2 Q1 Q2 Q3.

 

temporary.

select if GROUP3=1.

save outfile='e:\TEST\GROUP3.sav'

/KEEP= RESPID GROUP3 Q1 Q2 Q3.

 

Sincerely,

Jeanne Eidex

 

Eidex Group, LLC

www.eidexgroup.com

office: 770.614.6334

fax: 678.623.3931

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Macro Question: Conditionally Saving Outfiles

Albert-Jan Roskam
Hi,

How many groups are there? This untested code may work, but I believe only until 50 groups (the xsave limit, but that limit may have become higher in later spss versions).

do repeat #i = 1 to 50.
do if ( group eq #i ).
xsave outfile = "d:/temp/group" + #i + ".sav" /KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.
end if.
end repeat print.

 
Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: Jeanne Eidex <[hidden email]>
To: [hidden email]
Sent: Fri, January 14, 2011 3:11:19 PM
Subject: [SPSSX-L] Macro Question: Conditionally Saving Outfiles

Hi Everyone,

 

I am writing a program that needs to parse out data into separate files in the end.  The syntax works, but, the problem is that it writes an empty data file if conditions are not met, so, if my master file for this month does not have anyone (a row) with group2=1 then it writes an empty data file called group2.sav.  There are a large number of groups, so, it’s very inconvenient to have the empty data files.

 

This would be in a loop or do repeat statement but you can’t combine it with “save outfile” I thought maybe a macro fix this, it might make it more efficient, possibly with the ERASE command which I have never used.  Please let me know if you have any suggestions. 

 

temporary.

select if GROUP1=1.

save outfile='e:\TEST\GROUP1.sav'

/KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.

 

temporary.

select if GROUP2=1.

save outfile='e:\TEST\GROUP2.sav'

/KEEP= RESPID GROUP2 Q1 Q2 Q3.

 

temporary.

select if GROUP3=1.

save outfile='e:\TEST\GROUP3.sav'

/KEEP= RESPID GROUP3 Q1 Q2 Q3.

 

Sincerely,

Jeanne Eidex

 

Eidex Group, LLC

www.eidexgroup.com

office: 770.614.6334

fax: 678.623.3931

 

 


Reply | Threaded
Open this post in threaded view
|

Re: Macro Question: Conditionally Saving Outfiles

Jon K Peck
I don't think the code below will quite work the way it is manipulating the literals.

Another approach would be to use the SPSSINC SPLIT DATASET extension command.  It has some bells and whistles, but the basic command could just be
SPSSINC SPLIT DATASET SPLITVAR=group /OUTPUT DIRECTORY= "someplace".

There are options for how to name the output datasets and whether to clean out the target directory first.
It has a dialog box that appears on the Data menu once installed.

This command requires the Python plugin.  The command and the plugin or Essentials can be downloaded from the SPSS Community at
www.ibm.com/developerworks/spssdevcentral

HTH,

Jon Peck
Senior Software Engineer, IBM
[hidden email]
312-651-3435




From:        Albert-Jan Roskam <[hidden email]>
To:        [hidden email]
Date:        01/14/2011 10:26 AM
Subject:        Re: [SPSSX-L] Macro Question: Conditionally Saving Outfiles
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi,

How many groups are there? This untested code may work, but I believe only until 50 groups (the xsave limit, but that limit may have become higher in later spss versions).

do repeat #i = 1 to 50.
do if ( group eq #i ).
xsave outfile = "d:/temp/group" + #i + ".sav" /KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.
end if.
end repeat print.

 
Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




From: Jeanne Eidex <[hidden email]>
To:
[hidden email]
Sent:
Fri, January 14, 2011 3:11:19 PM
Subject:
[SPSSX-L] Macro Question: Conditionally Saving Outfiles


Hi Everyone,

 

I am writing a program that needs to parse out data into separate files in the end.  The syntax works, but, the problem is that it writes an empty data file if conditions are not met, so, if my master file for this month does not have anyone (a row) with group2=1 then it writes an empty data file called group2.sav.  There are a large number of groups, so, it’s very inconvenient to have the empty data files.

 

This would be in a loop or do repeat statement but you can’t combine it with “save outfile” I thought maybe a macro fix this, it might make it more efficient, possibly with the ERASE command which I have never used.  Please let me know if you have any suggestions.  

 

temporary.

select if GROUP1=1.

save outfile='e:\TEST\GROUP1.sav'

/KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.

 

temporary.

select if GROUP2=1.

save outfile='e:\TEST\GROUP2.sav'

/KEEP= RESPID GROUP2 Q1 Q2 Q3.

 

temporary.

select if GROUP3=1.

save outfile='e:\TEST\GROUP3.sav'

/KEEP= RESPID GROUP3 Q1 Q2 Q3.

 

Sincerely,

Jeanne Eidex

 

Eidex Group, LLC

www.eidexgroup.com

office: 770.614.6334

fax: 678.623.3931

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Macro Question: Conditionally Saving Outfiles

Albert-Jan Roskam
You're right, I just tried it and it doesn't work. XSAVE is not allowed within DO REPEAT. But using LOOP won't work either, because, elements that are concatenated with '+' must start with a quotation mark. So !DO-!DOEND,  !QUOTE!-CONCAT will work, but that's not nearly as flexible as using Python.
 
Will the macro language ever be entirely removed from Spss?
 
Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: Jon K Peck <[hidden email]>
To: [hidden email]
Sent: Fri, January 14, 2011 6:49:02 PM
Subject: Re: [SPSSX-L] Macro Question: Conditionally Saving Outfiles

I don't think the code below will quite work the way it is manipulating the literals.

Another approach would be to use the SPSSINC SPLIT DATASET extension command.  It has some bells and whistles, but the basic command could just be
SPSSINC SPLIT DATASET SPLITVAR=group /OUTPUT DIRECTORY= "someplace".

There are options for how to name the output datasets and whether to clean out the target directory first.
It has a dialog box that appears on the Data menu once installed.

This command requires the Python plugin.  The command and the plugin or Essentials can be downloaded from the SPSS Community at
www.ibm.com/developerworks/spssdevcentral

HTH,

Jon Peck
Senior Software Engineer, IBM
[hidden email]
312-651-3435




From:        Albert-Jan Roskam <[hidden email]>
To:        [hidden email]
Date:        01/14/2011 10:26 AM
Subject:        Re: [SPSSX-L] Macro Question: Conditionally Saving Outfiles
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi,

How many groups are there? This untested code may work, but I believe only until 50 groups (the xsave limit, but that limit may have become higher in later spss versions).

do repeat #i = 1 to 50.
do if ( group eq #i ).
xsave outfile = "d:/temp/group" + #i + ".sav" /KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.
end if.
end repeat print.

 
Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




From: Jeanne Eidex <[hidden email]>
To:
[hidden email]
Sent:
Fri, January 14, 2011 3:11:19 PM
Subject:
[SPSSX-L] Macro Question: Conditionally Saving Outfiles


Hi Everyone,

 

I am writing a program that needs to parse out data into separate files in the end.  The syntax works, but, the problem is that it writes an empty data file if conditions are not met, so, if my master file for this month does not have anyone (a row) with group2=1 then it writes an empty data file called group2.sav.  There are a large number of groups, so, it’s very inconvenient to have the empty data files.

 

This would be in a loop or do repeat statement but you can’t combine it with “save outfile” I thought maybe a macro fix this, it might make it more efficient, possibly with the ERASE command which I have never used.  Please let me know if you have any suggestions.  

 

temporary.

select if GROUP1=1.

save outfile='e:\TEST\GROUP1.sav'

/KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.

 

temporary.

select if GROUP2=1.

save outfile='e:\TEST\GROUP2.sav'

/KEEP= RESPID GROUP2 Q1 Q2 Q3.

 

temporary.

select if GROUP3=1.

save outfile='e:\TEST\GROUP3.sav'

/KEEP= RESPID GROUP3 Q1 Q2 Q3.

 

Sincerely,

Jeanne Eidex

 

Eidex Group, LLC

www.eidexgroup.com

office: 770.614.6334

fax: 678.623.3931

 

 


Reply | Threaded
Open this post in threaded view
|

Re: Macro Question: Conditionally Saving Outfiles

Bruce Weaver
Administrator
Albert-Jan Roskam wrote
--- snip ---
Will the macro language ever be entirely removed from Spss?

I certainly hope not.  That would make a lot of old code completely unusable.  

--
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: Macro Question: Conditionally Saving Outfiles

Jon K Peck
In reply to this post by Albert-Jan Roskam
There are no plans to remove the macro language.  While the Python technology is much superior, removing macros would just cause needless incompatibilities.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
312-651-3435




From:        Albert-Jan Roskam <[hidden email]>
To:        Jon K Peck/Chicago/IBM@IBMUS, [hidden email]
Date:        01/17/2011 05:09 AM
Subject:        Re: [SPSSX-L] Macro Question: Conditionally Saving Outfiles




You're right, I just tried it and it doesn't work. XSAVE is not allowed within DO REPEAT. But using LOOP won't work either, because, elements that are concatenated with '+' must start with a quotation mark. So !DO-!DOEND,  !QUOTE!-CONCAT will work, but that's not nearly as flexible as using Python.
 
Will the macro language ever be entirely removed from Spss?

Cheers!!
Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




From: Jon K Peck <[hidden email]>
To:
[hidden email]
Sent:
Fri, January 14, 2011 6:49:02 PM
Subject:
Re: [SPSSX-L] Macro Question: Conditionally Saving Outfiles


I don't think the code below will quite work the way it is manipulating the literals.


Another approach would be to use the SPSSINC SPLIT DATASET extension command.  It has some bells and whistles, but the basic command could just be

SPSSINC SPLIT DATASET SPLITVAR=group /OUTPUT DIRECTORY= "someplace".


There are options for how to name the output datasets and whether to clean out the target directory first.

It has a dialog box that appears on the Data menu once installed.


This command requires the Python plugin.  The command and the plugin or Essentials can be downloaded from the SPSS Community at

www.ibm.com/developerworks/spssdevcentral

HTH,


Jon Peck
Senior Software Engineer, IBM
[hidden email]
312-651-3435




From:        
Albert-Jan Roskam <[hidden email]>
To:        
[hidden email]
Date:        
01/14/2011 10:26 AM
Subject:        
Re: [SPSSX-L] Macro Question: Conditionally Saving Outfiles
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>




Hi,

How many groups are there? This untested code may work, but I believe only until 50 groups (the xsave limit, but that limit may have become higher in later spss versions).

do repeat #i = 1 to 50.
do if ( group eq #i ).
xsave outfile = "d:/temp/group" + #i + ".sav" /KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.
end if.
end repeat print.

 
Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: Jeanne Eidex <[hidden email]>
To:
[hidden email]
Sent:
Fri, January 14, 2011 3:11:19 PM
Subject:
[SPSSX-L] Macro Question: Conditionally Saving Outfiles


Hi Everyone,

 

I am writing a program that needs to parse out data into separate files in the end.  The syntax works, but, the problem is that it writes an empty data file if conditions are not met, so, if my master file for this month does not have anyone (a row) with group2=1 then it writes an empty data file called group2.sav.  There are a large number of groups, so, it’s very inconvenient to have the empty data files.

 

This would be in a loop or do repeat statement but you can’t combine it with “save outfile” I thought maybe a macro fix this, it might make it more efficient, possibly with the ERASE command which I have never used.  Please let me know if you have any suggestions.  

 

temporary.

select if GROUP1=1.

save outfile='e:\TEST\GROUP1.sav'

/KEEP= RESPID GROUP1_MEMBER Q1 Q2 Q3.

 

temporary.

select if GROUP2=1.

save outfile='e:\TEST\GROUP2.sav'

/KEEP= RESPID GROUP2 Q1 Q2 Q3.

 

temporary.

select if GROUP3=1.

save outfile='e:\TEST\GROUP3.sav'

/KEEP= RESPID GROUP3 Q1 Q2 Q3.

 

Sincerely,

Jeanne Eidex

 

Eidex Group, LLC

www.eidexgroup.com

office: 770.614.6334

fax: 678.623.3931