|
Hello, I have a macro that produces tables for numerous entities, all from same data file. Right now I have the macro arguments listed in the syntax file. I would prefer to read them in from a data file. It seems this is possible as I have found mention of doing so, but have had no luck myself. here is hte macro ans a snippet of the arguments (2 arguments). Thanks in advance, John DEFINE tables7 (arg1= !TOKENS(1) / arg2= !TOKENS(1)) TEMPORARY. SELECT IF (trnidT7=!arg1). CTABLES /FORMAT EMPTY=ZERO MISSING='.' MINCOLWIDTH=36 MAXCOLWIDTH=55 UNITS=POINTS /PCOMPUTE &ALD=EXPR([3]+[4]) /PPROPERTIES &ALD LABEL = 'ALD' /TABLE (q1 + q2 + q3 + q4 + q6 + q7 + q8 + q9 + q10 + q11 + q12 + q13 ) [ ROWPCT.COUNT PCT40.1, TOTALS[COUNT F40.0] ] /CLABELS ROWLABELS = OPPOSITE /CATEGORIES VARIABLES= q1 q2 q3 q4 q6 q7 q8 q9 q10 q11 q12 q13 [1, 2, 3, 4, &ALD] EMPTY=INCLUDE TOTAL=YES POSITION=AFTER /TITLES TITLE=!QUOTE(!CONCAT('Table', !arg1,'A. ', !UNQUOTE(!arg2))) . TEMPORARY. SELECT IF (trnidT7=!arg1). !ENDDEFINE. * Arguments . tables7 arg1= 7 arg2="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" . tables7 arg1= 8 arg2="BBBBBBBBB" . tables7 arg1= 9 arg2="CCCCCCCCCCCCCCCCCCCCC" . |
|
Administrator
|
You could roll old school and read your arguments using data list or get file.
Use WRITE to generate a text file (maccall.sps) containing your macro calls. GET your raw data. INSERT maccall.sps. -- OTOH: Some Pythonista might charm in with a better approach.
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?" |
|
In reply to this post by J P-6
Dear John and others,
This reminds me of this post (hope this link won't be stripped out or something). It produces ctables separately for different groups (groups are contained in string variable) and fancy titles containing the group names. Reasons for writing it this way are that 1) the syntax length does not increase as the number of groups increases, 2) group names don't have to be typed in manually (too much work and a risk of spelling mistakes) and 3) which groups are actually present in the data doesn't have to be inspected, no idle syntax will be produced for groups that aren't there. I'm not sure to what extent the situation at hand could be approached similarly but insofar as time permits, I'd be happy to assist! Best, Ruben Date: Fri, 20 Jan 2012 11:51:31 -0800 From: [hidden email] Subject: macro that reads arguments from data file To: [hidden email] Hello, I have a macro that produces tables for numerous entities, all from same data file. Right now I have the macro arguments listed in the syntax file. I would prefer to read them in from a data file. It seems this is possible as I have found mention of doing so, but have had no luck myself. here is hte macro ans a snippet of the arguments (2 arguments). Thanks in advance, John DEFINE tables7 (arg1= !TOKENS(1) / arg2= !TOKENS(1)) TEMPORARY. SELECT IF (trnidT7=!arg1). CTABLES /FORMAT EMPTY=ZERO MISSING='.' MINCOLWIDTH=36 MAXCOLWIDTH=55 UNITS=POINTS /PCOMPUTE &ALD=EXPR([3]+[4]) /PPROPERTIES &ALD LABEL = 'ALD' /TABLE (q1 + q2 + q3 + q4 + q6 + q7 + q8 + q9 + q10 + q11 + q12 + q13 ) [ ROWPCT.COUNT PCT40.1, TOTALS[COUNT F40.0] ] /CLABELS ROWLABELS = OPPOSITE /CATEGORIES VARIABLES= q1 q2 q3 q4 q6 q7 q8 q9 q10 q11 q12 q13 [1, 2, 3, 4, &ALD] EMPTY=INCLUDE TOTAL=YES POSITION=AFTER /TITLES TITLE=!QUOTE(!CONCAT('Table', !arg1,'A. ', !UNQUOTE(!arg2))) . TEMPORARY. SELECT IF (trnidT7=!arg1). !ENDDEFINE. * Arguments . tables7 arg1= 7 arg2="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" . tables7 arg1= 8 arg2="BBBBBBBBB" . tables7 arg1= 9 arg2="CCCCCCCCCCCCCCCCCCCCC" . |
|
In reply to this post by David Marso
Here is a general solution using Python.
You would need to install the Python Essentials from the SPSS Community
website (www.ibm.com/developerworks/spssdevcentral)
first.
In the code below, indentation and case matter. First, define the syntax you want to execute with parameters substitution like this. begin program. def mysyntax(**kwargs): cmd=r"""CTABLES /TABLE %(y)s BY %(x)s /TITLES TITLE = "%(title)s".""" spss.Submit(cmd % kwargs) end program. The cmd and spss.Submit lines are indented. In the Statistics syntax portion, refer to each parameter by name using notation like this: %(y)s. That means to substitute the value of y at that point in the string of commands. In this example, the parameters are y, x, and title. You can have as many commands as you like defined in cmd. You would just start each one on a new line inside the triple quotes (and terminate each with a period). This is analogous to your definition of the macro. Second, create a file holding the parameters and their values. E.g., [Parameters] y=jobcat x=educ title=this is the title I named this file c:/temp/parms.ini. [Parameters] is a section name. Note that quotes are not used on the title line. Each parameter variable should be defined in this file. Now you read the parameters from the file. You just do this once unless you need to read new contents. begin program. import ConfigParser config=ConfigParser.ConfigParser() config.readfp(open("c:/temp/parms.ini")) end program. Use forward slashes in the path. Then you call the function containing the syntax like this. This is analogous to invoking the macro, but you don't name the parameters explicitly. begin program. mysyntax(**dict(config.items("Parameters")) end program. Your ini file could have many sections: you just specify the relevant section name in the call to mysyntax. The only things that change are the contents of the ini file and any changes you want to make in the Statistics syntax. These begin/end programs could all be combined. I've written them separately for clarity and because it is likely that you would change the different parts at different times. HTH, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: David Marso <[hidden email]> To: [hidden email] Date: 01/20/2012 01:19 PM Subject: Re: [SPSSX-L] macro that reads arguments from data file Sent by: "SPSSX(r) Discussion" <[hidden email]> You could roll old school and read your arguments using data list or get file. Use WRITE to generate a text file (maccall.sps) containing your macro calls. GET your raw data. INSERT maccall.sps. -- OTOH: Some Pythonista might charm in with a better approach. J P-6 wrote > > Hello, > > I have a macro that produces tables for numerous entities, all from same > data file. > > Right now I have the macro arguments listed in the syntax file. I would > prefer to read them in from a data file. It seems this is possible as I > have found mention of doing so, but have had no luck myself. > > here is hte macro ans a snippet of the arguments (2 arguments). > > Thanks in advance, > John > > DEFINE tables7 (arg1= !TOKENS(1) > / arg2= !TOKENS(1)) > TEMPORARY. > SELECT IF (trnidT7=!arg1). > CTABLES > /FORMAT EMPTY=ZERO MISSING='.' MINCOLWIDTH=36 MAXCOLWIDTH=55 > UNITS=POINTS > /PCOMPUTE &ALD=EXPR([3]+[4]) > /PPROPERTIES &ALD LABEL = 'ALD' > /TABLE (q1 + q2 + q3 + q4 + q6 + q7 + q8 + q9 + q10 + q11 + q12 + q13 ) > [ ROWPCT.COUNT PCT40.1, TOTALS[COUNT F40.0] ] > /CLABELS ROWLABELS = OPPOSITE > /CATEGORIES VARIABLES= q1 q2 q3 q4 q6 q7 q8 q9 q10 q11 q12 q13 [1, 2, > 3, 4, &ALD] EMPTY=INCLUDE TOTAL=YES POSITION=AFTER > /TITLES TITLE=!QUOTE(!CONCAT('Table', !arg1,'A. ', !UNQUOTE(!arg2))) . > TEMPORARY. > SELECT IF (trnidT7=!arg1). > !ENDDEFINE. > > * Arguments . > tables7 arg1= 7 arg2="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" . > tables7 arg1= 8 arg2="BBBBBBBBB" . > tables7 arg1= 9 arg2="CCCCCCCCCCCCCCCCCCCCC" . > -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/macro-that-reads-arguments-from-data-file-tp5161545p5161570.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 |
|
Is there any benefit for using ConfigParser, compared to using Yaml (pyYaml)? It seems to me that a .yaml file could easily be read by other applications (for example, yaml under R). And yaml is quite readable, but so is the .ini file. 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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
YAML or a YAML subset is supported in the
Python Standard Library as of Python 2.6 via the json module, but I don't
see any advantage to that for this purpose, and the json interface is a
bit more complicated. R has a json package available, but it is not
part of the standard R distribution.
Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: Albert-Jan Roskam <[hidden email]> To: [hidden email] Date: 01/22/2012 02:15 AM Subject: Re: [SPSSX-L] macro that reads arguments from data file Sent by: "SPSSX(r) Discussion" <[hidden email]> Is there any benefit for using ConfigParser, compared to using Yaml (pyYaml)? It seems to me that a .yaml file could easily be read by other applications (for example, yaml under R). And yaml is quite readable, but so is the .ini file. 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: Sunday, January 22, 2012 4:21 AM Subject: Re: [SPSSX-L] macro that reads arguments from data file Here is a general solution using Python. You would need to install the Python Essentials from the SPSS Community website (www.ibm.com/developerworks/spssdevcentral) first. In the code below, indentation and case matter. First, define the syntax you want to execute with parameters substitution like this. begin program. def mysyntax(**kwargs): cmd=r"""CTABLES /TABLE %(y)s BY %(x)s /TITLES TITLE = "%(title)s".""" spss.Submit(cmd % kwargs) end program. The cmd and spss.Submit lines are indented. In the Statistics syntax portion, refer to each parameter by name using notation like this: %(y)s. That means to substitute the value of y at that point in the string of commands. In this example, the parameters are y, x, and title. You can have as many commands as you like defined in cmd. You would just start each one on a new line inside the triple quotes (and terminate each with a period). This is analogous to your definition of the macro. Second, create a file holding the parameters and their values. E.g., [Parameters] y=jobcat x=educ title=this is the title I named this file c:/temp/parms.ini. [Parameters] is a section name. Note that quotes are not used on the title line. Each parameter variable should be defined in this file. Now you read the parameters from the file. You just do this once unless you need to read new contents. begin program. import ConfigParser config=ConfigParser.ConfigParser() config.readfp(open("c:/temp/parms.ini")) end program. Use forward slashes in the path. Then you call the function containing the syntax like this. This is analogous to invoking the macro, but you don't name the parameters explicitly. begin program. mysyntax(**dict(config.items("Parameters")) end program. Your ini file could have many sections: you just specify the relevant section name in the call to mysyntax. The only things that change are the contents of the ini file and any changes you want to make in the Statistics syntax. These begin/end programs could all be combined. I've written them separately for clarity and because it is likely that you would change the different parts at different times. HTH, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: David Marso <[hidden email]> To: [hidden email] Date: 01/20/2012 01:19 PM Subject: Re: [SPSSX-L] macro that reads arguments from data file Sent by: "SPSSX(r) Discussion" <[hidden email]> You could roll old school and read your arguments using data list or get file. Use WRITE to generate a text file (maccall.sps) containing your macro calls. GET your raw data. INSERT maccall.sps. -- OTOH: Some Pythonista might charm in with a better approach. J P-6 wrote > > Hello, > > I have a macro that produces tables for numerous entities, all from same > data file. > > Right now I have the macro arguments listed in the syntax file. I would > prefer to read them in from a data file. It seems this is possible as I > have found mention of doing so, but have had no luck myself. > > here is hte macro ans a snippet of the arguments (2 arguments). > > Thanks in advance, > John > > DEFINE tables7 (arg1= !TOKENS(1) > / arg2= !TOKENS(1)) > TEMPORARY. > SELECT IF (trnidT7=!arg1). > CTABLES > /FORMAT EMPTY=ZERO MISSING='.' MINCOLWIDTH=36 MAXCOLWIDTH=55 > UNITS=POINTS > /PCOMPUTE &ALD=EXPR([3]+[4]) > /PPROPERTIES &ALD LABEL = 'ALD' > /TABLE (q1 + q2 + q3 + q4 + q6 + q7 + q8 + q9 + q10 + q11 + q12 + q13 ) > [ ROWPCT.COUNT PCT40.1, TOTALS[COUNT F40.0] ] > /CLABELS ROWLABELS = OPPOSITE > /CATEGORIES VARIABLES= q1 q2 q3 q4 q6 q7 q8 q9 q10 q11 q12 q13 [1, 2, > 3, 4, &ALD] EMPTY=INCLUDE TOTAL=YES POSITION=AFTER > /TITLES TITLE=!QUOTE(!CONCAT('Table', !arg1,'A. ', !UNQUOTE(!arg2))) . > TEMPORARY. > SELECT IF (trnidT7=!arg1). > !ENDDEFINE. > > * Arguments . > tables7 arg1= 7 arg2="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" . > tables7 arg1= 8 arg2="BBBBBBBBB" . > tables7 arg1= 9 arg2="CCCCCCCCCCCCCCCCCCCCC" . > -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/macro-that-reads-arguments-from-data-file-tp5161545p5161570.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 |
|
In reply to this post by Jon K Peck
I've just installed a trial version of V20 and come across a piece of code that doesn't work as it did in V18. I have a file with monthly data from agencies. I want to split the files by variable ('agency') and save the output into separate Excel files. I've installed the Python essentials and copied the spssaux2.py file from the c:\python26 directory into the c:\python27 directory. The code that worked previously is: >begin program. >import spss, spssaux2 >cmd = "SAVE TRANSLATE OUTFILE='U:/cru_data/SummaryReports/monthly0011/%(splitvalue)s.xls' /TYPE=XLS /VERSION=8 /replace /fieldnames /replace " >spssaux2.generalizedSplit('Agency', cmd) >end program. The output I get now is like this: begin program. import spss, spssaux2 cmd = "SAVE TRANSLATE OUTFILE='U:/cru_data/SummaryReports/monthly0011/%(splitvalue)s.xls' /TYPE=XLS /VERSION=8 /replace /fieldnames /replace " spssaux2.generalizedSplit('Agency', cmd) end program. SPLIT: Agency = Abington >Error # 9016 in column 28. Text: U:/cru_data/SummaryReports/monthly0011/"Abington".xls >The file specification is invalid, and the output file cannot be opened. >Execution of this command stops. Any suggestions greatly appreciated! Dan Bibel Massachusetts State Police |
|
See below
Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: "Bibel, Daniel (POL)" <[hidden email]> To: [hidden email] Date: 03/13/2012 08:13 AM Subject: [SPSSX-L] Question about spssaux2 and V20 Sent by: "SPSSX(r) Discussion" <[hidden email]> I've just installed a trial version of V20 and come across a piece of code that doesn't work as it did in V18. I have a file with monthly data from agencies. I want to split the files by variable ('agency') and save the output into separate Excel files. I've installed the Python essentials and copied the spssaux2.py file from the c:\python26 directory into the c:\python27 directory. >>>I would always recommend getting the latest version of a supplementary file from the SPSS Community website rather than copying an old version, although in this case, it makes the behavior comparison simpler. The code that worked previously is: >begin program. >import spss, spssaux2 >cmd = "SAVE TRANSLATE OUTFILE='U:/cru_data/SummaryReports/monthly0011/%(splitvalue)s.xls' /TYPE=XLS /VERSION=8 /replace /fieldnames /replace " >spssaux2.generalizedSplit('Agency', cmd) >end program. The output I get now is like this: begin program. import spss, spssaux2 cmd = "SAVE TRANSLATE OUTFILE='U:/cru_data/SummaryReports/monthly0011/%(splitvalue)s.xls' /TYPE=XLS /VERSION=8 /replace /fieldnames /replace " spssaux2.generalizedSplit('Agency', cmd) end program. SPLIT: Agency = Abington >Error # 9016 in column 28. Text: U:/cru_data/SummaryReports/monthly0011/"Abington".xls >The file specification is invalid, and the output file cannot be opened. >Execution of this command stops. Any suggestions greatly appreciated! >>>I don't think this code could have worked in V18 with a string variable. The split values for strings are quoted, and the quotes are causing the problem in the output file name. In fact, I tried a similar example with V18 and got the same failure. You might be interested in the SPSSINC SPLIT DATASET extension command, which provides a more general way to do this type of operation and doesn't require you to write any Python code. generalizedsplit could be modified to allow a reference to the unquoted value, but SPLIT DATASET provides a more powerful tool. HTH, Jon Peck Dan Bibel Massachusetts State Police |
| Free forum by Nabble | Edit this page |
