|
Hello,
I've only recently started working with SPSS macros but at the same time been actively listening to comments by other on the list regarding the use of Python language instead of SPSS macros. Therefore I have a question - how to convert the SPSS macro below to Python language? Below is a small SPSS macro followed by a Python equivalent. The python equivalent is the best/only way I could think of doing this. I would like to know if there is a better way of doing this without the use of SPSS macros within the Python program. I'd like to get out of the habit of using SPSS macros (where possibly) when it can all be done with Python (trying to half my workload in learning only 1 programming language rather than 2). Is this a sensible approach to take? *==========*. *SPSS macro*. *==========*. *Start new session*. DATASET CLOSE ALL. NEW FILE. OUTPUT CLOSE ALL. GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'. DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= !DEFAULT (aggData) !ENCLOSE('[',']')). DATASET DECLARE !datan. COMPUTE NoBreak=1. AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. !ENDDEFINE. SET MPRINT ON. !agg1 vars=[salary jobtime prevexp] datan=[aggDataA]. SET MPRINT OFF. *==========*. *Python Solution*. *==========*. *Start new session*. DATASET CLOSE ALL. NEW FILE. OUTPUT CLOSE ALL. GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'. BEGIN PROGRAM. import spss def aggpy (pyvar): spss.Submit(r""" DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= !DEFAULT (aggData) !ENCLOSE('[',']')). DATASET DECLARE !datan. COMPUTE NoBreak=1. AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. !ENDDEFINE. SET MPRINT ON. !agg1 vars=[%(pyvar)s] datan=[aggDataA]. SET MPRINT OFF.""" % locals()) aggpy(pyvar= 'salary jobtime prevexp') END PROGRAM. ===================== 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 |
|
Hello,
I'm not a python expert, but this works Greetz Frans -------------------- DATASET CLOSE ALL. NEW FILE. OUTPUT CLOSE ALL. GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'. BEGIN PROGRAM. import spss def aggpy (pyvar,datan='AggdataA'): l='' for v in pyvar: l=l+"/%s=sum(%s)"%(v,v) l="AGGREGATE OUTFILE='%s'/BREAK=NoBreak %s ."%(datan,l) spss.Submit("COMPUTE NoBreak=1"); spss.Submit(l) aggpy(['salary', 'jobtime', 'prevexp']) END PROGRAM. ------------------------------------------- Frans Marcelissen email [hidden email] -----Oorspronkelijk bericht----- Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens J Sutar Verzonden: donderdag 18 december 2008 14:59 Aan: [hidden email] Onderwerp: Converting SPSS macros to Python Hello, I've only recently started working with SPSS macros but at the same time been actively listening to comments by other on the list regarding the use of Python language instead of SPSS macros. Therefore I have a question - how to convert the SPSS macro below to Python language? Below is a small SPSS macro followed by a Python equivalent. The python equivalent is the best/only way I could think of doing this. I would like to know if there is a better way of doing this without the use of SPSS macros within the Python program. I'd like to get out of the habit of using SPSS macros (where possibly) when it can all be done with Python (trying to half my workload in learning only 1 programming language rather than 2). Is this a sensible approach to take? *==========*. *SPSS macro*. *==========*. *Start new session*. DATASET CLOSE ALL. NEW FILE. OUTPUT CLOSE ALL. GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'. DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= !DEFAULT (aggData) !ENCLOSE('[',']')). DATASET DECLARE !datan. COMPUTE NoBreak=1. AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. !ENDDEFINE. SET MPRINT ON. !agg1 vars=[salary jobtime prevexp] datan=[aggDataA]. SET MPRINT OFF. *==========*. *Python Solution*. *==========*. *Start new session*. DATASET CLOSE ALL. NEW FILE. OUTPUT CLOSE ALL. GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'. BEGIN PROGRAM. import spss def aggpy (pyvar): spss.Submit(r""" DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= !DEFAULT (aggData) !ENCLOSE('[',']')). DATASET DECLARE !datan. COMPUTE NoBreak=1. AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. !ENDDEFINE. SET MPRINT ON. !agg1 vars=[%(pyvar)s] datan=[aggDataA]. SET MPRINT OFF.""" % locals()) aggpy(pyvar= 'salary jobtime prevexp') END PROGRAM. ===================== 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 |
|
In reply to this post by Jignesh Sutar
Hi,
You can't mix macro language and Python language. Python is usually much easier and more readable, en certainly more powerful than this arcane macro language. Untested, and without proper indentation: set mprint = on. begin program. import spss def agg (vars, datan='whatever'): spss.Submit("dataset declare %s." % (datan)) for k, var in enumerate(vars): spss.Submit(""" aggr out = %(datan)s / break= %(var)s / %(var)s = sum( %(var)s ).""" % locals()) # function call. agg (['var1', 'var2', 'varx'], 'somename') end program. set mprint = off. Cheers!! Albert-Jan DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= !DEFAULT (aggData) !ENCLOSE('[',']')). DATASET DECLARE !datan. COMPUTE NoBreak=1. AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. !ENDDEFINE. SET MPRINT ON. !agg1 vars=[salary jobtime prevexp] datan=[aggDataA]. SET MPRINT OFF. --- On Thu, 12/18/08, J Sutar <[hidden email]> wrote: > From: J Sutar <[hidden email]> > Subject: Converting SPSS macros to Python > To: [hidden email] > Date: Thursday, December 18, 2008, 2:58 PM > Hello, > I've only recently started working with SPSS macros but > at the same time > been actively listening to comments by other on the list > regarding the use > of Python language instead of SPSS macros. > > Therefore I have a question - how to convert the SPSS macro > below to Python > language? > > Below is a small SPSS macro followed by a Python > equivalent. The python > equivalent is the best/only way I could think of doing > this. I would like to > know if there is a better way of doing this without the use > of SPSS macros > within the Python program. I'd like to get out of the > habit of using SPSS > macros (where possibly) when it can all be done with Python > (trying to half > my workload in learning only 1 programming language rather > than 2). Is this > a sensible approach to take? > > *==========*. > *SPSS macro*. > *==========*. > *Start new session*. > DATASET CLOSE ALL. > NEW FILE. > OUTPUT CLOSE ALL. > > GET FILE='C:\Program > Files\SPSSInc\Statistics17\Samples\English\employee > data.sav'. > > > DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= > !DEFAULT (aggData) > !ENCLOSE('[',']')). > DATASET DECLARE !datan. > COMPUTE NoBreak=1. > AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak > !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. > !ENDDEFINE. > > SET MPRINT ON. > !agg1 vars=[salary jobtime prevexp] datan=[aggDataA]. > SET MPRINT OFF. > > > *==========*. > *Python Solution*. > *==========*. > *Start new session*. > DATASET CLOSE ALL. > NEW FILE. > OUTPUT CLOSE ALL. > > GET FILE='C:\Program > Files\SPSSInc\Statistics17\Samples\English\employee > data.sav'. > > > BEGIN PROGRAM. > import spss > def aggpy (pyvar): > spss.Submit(r""" > DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= > !DEFAULT (aggData) > !ENCLOSE('[',']')). > DATASET DECLARE !datan. > COMPUTE NoBreak=1. > AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak > !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. > !ENDDEFINE. > > SET MPRINT ON. > !agg1 vars=[%(pyvar)s] datan=[aggDataA]. > SET MPRINT OFF.""" % locals()) > > aggpy(pyvar= 'salary jobtime prevexp') > END PROGRAM. > > ===================== > 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 |
|
There is a section in the Programming and Data Management for SPSS Statistics 17.0 book, downloadable in PDF form from a link on Developer Central (www.spss.com/devcentral) entitled Migrating Macros to Python in the chapter, Tips on Migrating Command Syntax and Macro Jobs to Python. (This chapter also appears in older editions of the book.)
HTH, Jon Peck -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Albert-jan Roskam Sent: Friday, December 19, 2008 1:38 AM To: [hidden email] Subject: Re: [SPSSX-L] Converting SPSS macros to Python Hi, You can't mix macro language and Python language. Python is usually much easier and more readable, en certainly more powerful than this arcane macro language. Untested, and without proper indentation: set mprint = on. begin program. import spss def agg (vars, datan='whatever'): spss.Submit("dataset declare %s." % (datan)) for k, var in enumerate(vars): spss.Submit(""" aggr out = %(datan)s / break= %(var)s / %(var)s = sum( %(var)s ).""" % locals()) # function call. agg (['var1', 'var2', 'varx'], 'somename') end program. set mprint = off. Cheers!! Albert-Jan DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= !DEFAULT (aggData) !ENCLOSE('[',']')). DATASET DECLARE !datan. COMPUTE NoBreak=1. AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. !ENDDEFINE. SET MPRINT ON. !agg1 vars=[salary jobtime prevexp] datan=[aggDataA]. SET MPRINT OFF. --- On Thu, 12/18/08, J Sutar <[hidden email]> wrote: > From: J Sutar <[hidden email]> > Subject: Converting SPSS macros to Python > To: [hidden email] > Date: Thursday, December 18, 2008, 2:58 PM > Hello, > I've only recently started working with SPSS macros but > at the same time > been actively listening to comments by other on the list > regarding the use > of Python language instead of SPSS macros. > > Therefore I have a question - how to convert the SPSS macro > below to Python > language? > > Below is a small SPSS macro followed by a Python > equivalent. The python > equivalent is the best/only way I could think of doing > this. I would like to > know if there is a better way of doing this without the use > of SPSS macros > within the Python program. I'd like to get out of the > habit of using SPSS > macros (where possibly) when it can all be done with Python > (trying to half > my workload in learning only 1 programming language rather > than 2). Is this > a sensible approach to take? > > *==========*. > *SPSS macro*. > *==========*. > *Start new session*. > DATASET CLOSE ALL. > NEW FILE. > OUTPUT CLOSE ALL. > > GET FILE='C:\Program > Files\SPSSInc\Statistics17\Samples\English\employee > data.sav'. > > > DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= > !DEFAULT (aggData) > !ENCLOSE('[',']')). > DATASET DECLARE !datan. > COMPUTE NoBreak=1. > AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak > !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. > !ENDDEFINE. > > SET MPRINT ON. > !agg1 vars=[salary jobtime prevexp] datan=[aggDataA]. > SET MPRINT OFF. > > > *==========*. > *Python Solution*. > *==========*. > *Start new session*. > DATASET CLOSE ALL. > NEW FILE. > OUTPUT CLOSE ALL. > > GET FILE='C:\Program > Files\SPSSInc\Statistics17\Samples\English\employee > data.sav'. > > > BEGIN PROGRAM. > import spss > def aggpy (pyvar): > spss.Submit(r""" > DEFINE !agg1(vars=!ENCLOSE('[',']') /datan= > !DEFAULT (aggData) > !ENCLOSE('[',']')). > DATASET DECLARE !datan. > COMPUTE NoBreak=1. > AGGREGATE OUTFILE=!QUOTE(!datan) /BREAK=NoBreak > !DO !i !IN (!vars) /!i =SUM(!i) !DOEND. > !ENDDEFINE. > > SET MPRINT ON. > !agg1 vars=[%(pyvar)s] datan=[aggDataA]. > SET MPRINT OFF.""" % locals()) > > aggpy(pyvar= 'salary jobtime prevexp') > END PROGRAM. > > ===================== > 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 |
| Free forum by Nabble | Edit this page |
