could you please tell me if macros in python have structure similar to SAS, or can do similar things?
i would like to implement some in spss thnx |
Administrator
|
Probably: You are presuming an intimacy with SAS macros. I have passing familiarity.
Perhaps describe some of the tasks you need implemented? python != macros in SPSS Awaiting Jon's soapbox.. ;-) --
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 progster
Python programmability is very powerful.
Download the Programming and Data Management book from the link for
extensive examples. The detailed programmability documentation is
available from the Help menu once the Python Essentials are installed.
https://www.ibm.com/developerworks/mydeveloperworks/files/form/anonymous/api/library/b5bb8a42-04d2-4503-93bb-dc45d7a145c2/document/ea95a2e5-88f9-404c-86ef-13f19951bd74/media/Programming%20and%20Data%20Management%20for%20IBM%20SPSS%20Statistics%2020.zip Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: progster <[hidden email]> To: [hidden email] Date: 01/27/2015 01:47 AM Subject: [SPSSX-L] Python macros in order to use in spss Sent by: "SPSSX(r) Discussion" <[hidden email]> could you please tell me if macros in python have structure similar to SAS, or can do similar things? i would like to implement some in spss thnx -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-macros-in-order-to-use-in-spss-tp5728491.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 ===================== 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 progster
A lot depend on what tasks you are attempting to do. Usually there are a variety of ways to do a task. If you post some example descriptions of what you might want to do, responders could be more specific.
There are several ways to use SPSS. 1) just use the GUI menus, etc. ILL ADVISED. 2) Use the GUI to create a first draft of syntax and refine you thinking over drafts of the syntax. BEST WAY TO GET STARTED. The syntax has a lot in common with SAS syntax, but is more like English and has simpler ways to do many of the data preparation/cleaning tasks. Some times people use the word "macros" when they just mean "syntax" 3) use SPLIT FILES in your syntax to do the same processes on subsets of your cases. 4) for procedures that are not yet mainstream use SPSS to prepare/clean your data and call R from within SPSS. The next 2 have a lot of overlap in what they can do, 5) use Macros within SPSS to generalize your syntax so that you only need to specify a few arguments to adapt your syntax, or to create ad hoc functions etc. 6) Use Python to wrap around your syntax to adapt to special situations OR use Python within SPSS for special functions, etc.
Art Kendall
Social Research Consultants |
In reply to this post by David Marso
i will check the stuff mentioned by Jon thanks.
"python != macros in SPSS" what do you mean, that python does not add feaures to the macros in spss? here's an example of what i'm thinking trying to aply a SAS logic to SPSS: a simple ctable: temp. sel if var2=1. CTABLES /VLABELS VARIABLES=var1 segment DISPLAY=LABEL /TABLE var1 [C] BY segment [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=var1 segment ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER. in SAS I could use some dynamic variables, I use &1 and &2 just to give you an idea temp. sel if var2=&1. CTABLES /VLABELS VARIABLES=&2 segment DISPLAY=LABEL /TABLE &2 [C] BY segment [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=&2 segment ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER. then i will call the macro and try a lot of combination. *calling the macro. &1=1, &2=var1 &1=2, &2=var5 I searched material on spss macros but it seemed to me that they aren't able to do this, so i guess that i need python. am i wrong? |
Administrator
|
"!="
! symbol for NOT. macros are not equal to python, but suffice for doing much of what might be necessary. MACROS are stupid string parsers and have no access to metadata or data values. OTOH, no need getting complicated with python to do what you requested. Macros can EASIELY do what you presented. Here two ways of parameterizing the required macro. Hard to say what you read/searched, but throw it in the trash ;-) ---- DEFINE !mytab (x1 !TOKENS(1)/x2 !TOKENS(1)) TEMPORARY. SELECT IF var2=!x1. CTABLES /VLABELS VARIABLES=!x2 segment DISPLAY=LABEL /TABLE !x2 [C] BY segment [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=!x2 segment ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER. !ENDDEFINE. !mytab x1=v1 x2=v2. OR: DEFINE !mytab2 (!POSITIONAL !TOKENS(1)/ !POSITIONAL !TOKENS(1) ) TEMPORARY. SELECT IF var2=!1. CTABLES /VLABELS VARIABLES=!2 segment DISPLAY=LABEL /TABLE !2 [C] BY segment [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=!2 segment ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER. !ENDDEFINE. mytab2 v1 v2
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 progster
While the macro language can do the simple
task below, Python programmability is immensely more powerful, so if you
are going to learn one or the other, you will go a lot farther with programmability.
The SPSS macro language is not nearly as powerful as the SAS macro
language, but Python programmability brings the power of a modern, object-oriented,
elegant, and easy to learn language with an immense collection of supplementary
modules to Statistics.
Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: progster <[hidden email]> To: [hidden email] Date: 01/27/2015 08:35 AM Subject: Re: [SPSSX-L] Python macros in order to use in spss Sent by: "SPSSX(r) Discussion" <[hidden email]> i will check the stuff mentioned by Jon thanks. "python != macros in SPSS" what do you mean, that python does not add feaures to the macros in spss? here's an example of what i'm thinking trying to aply a SAS logic to SPSS: a simple ctable: temp. sel if var2=1. CTABLES /VLABELS VARIABLES=var1 segment DISPLAY=LABEL /TABLE var1 [C] BY segment [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=var1 segment ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER. in SAS I could use some dynamic variables, I use &1 and &2 just to give you an idea temp. sel if var2=&1. CTABLES /VLABELS VARIABLES=&2 segment DISPLAY=LABEL /TABLE &2 [C] BY segment [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=&2 segment ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER. then i will call the macro and try a lot of combination. *calling the macro. &1=1, &2=var1 &1=2, &2=var5 I searched material on spss macros but it seemed to me that they aren't able to do this, so i guess that i need python. am i wrong? -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-macros-in-order-to-use-in-spss-tp5728491p5728498.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 ===================== 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 |
This post was updated on .
I prepared this macro in spss. now i would like to add a loop that could change COLPCT.COUNT to rowPCT.COUNT and tablePCT.COUNT.
I know that I could add the 3 parameters in a single table, but for analysis sake we prefer having 3 sepatare tables. Also I could add a new tokens that substitutes the COLPCT.COUNT, rowPCT.COUNT and tablePCT.COUNT, but it would generate a lot of macro calls, it's not as efficient as a nested macro. I tried to add a define into the define but it's not allowed. Is there a way with Python or spss to add a nested macro? DEFINE !dos (x1 !TOKENS(1)/x2 !TOKENS(1)) CTABLES /VLABELS VARIABLES=!x1 !x2 DISPLAY=LABEL /TABLE !x1[C] BY !x2 [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=!x1 !x2 ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=AFTER. !ENDDEFINE. !dos x1=p05 x2=log_segment. !dos x1=p02 x2=log_segment. |
Administrator
|
In Macros looping is achieved by use of !DO - !DOEND block!
!DO !var !IN (!parlist) something with !var .... !DOEND Search these archives for examples. ----
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 progster
You could do this with the traditional
macro language, but I'll show you an easy way to do this with Python, which
opens the door to doing more complicated things more conveniently. This
example is built using the SPSSINC PROGRAM extension command, which
allows a Python program to receive parameters using traditional syntax
without the need to make your program into an extension command.
The first step is to create a Python function to do the task. It is cleanest, if you might want to create a library of such functions, to put this in a separate module that might hold many such functions, but you can also just define it in a BEGIN/END PROGRAM block. I'll use the first approach here. Here is the code. It defines a function named dotables that gets two parameters and iterates three CTABLES commands with different statistics. (The indentation matters, so if the listserve mangles this, I can send it to you as an attachment.) The code uses the form %(x1)s to say substitute the value of (Python) variable x1 here. Then it iterates through statistics in the for loop below and runs the CTABLES commands via the Submit api. This version does no error checking, but that could easily be added. # run multiple tables with different statistics. import spss, sys def dotables(): template = """CTABLES /VLABELS VARIABLES=%(x1)s %(x2)s DISPLAY=LABEL /TABLE %(x1)s[C] BY %(x2)s [C][COUNT %(dimension)sPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=%(x1)s %(x2)s ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=AFTER.""" x1 = sys.argv[1] x2 = sys.argv[2] for dimension in ["COL", "ROW", "TABLE"]: spss.Submit(template % locals()) I saved this in a module utilities.py. It contains only Python code, so it should have extension py, and the code should not include BEGIN/END PROGRAM. The module needs to be somewhere on your Python search path. If you are unsure what the search path includes, run this program from a syntax window. It will list all the locations that will be searched. You can extend this list if these locations are convenient, but I'll assume this is okay for now. begin program. import sys for loc in sys.path: print loc end program. Now, when you want to use this, you just do something like this. SPSSINC PROGRAM utilities.dotables var1 var2. SPSSINC PROGRAM will automatically load the utilities module and pass the parameters to the dotables function through a special variable named sys.argv. SPSSINC PROGRAM is an exension command. You can install it from the Utilities menu in V22 or later. Otherwise, get it from the extensions command collection on the SPSS Community site and install it from Utilities. You need to restart Statistics after the installation. https://www.ibm.com/developerworks/community/files/app?lang=en#/file/4cdcebc8-e6be-4b39-8a67-8ccd9a50656f Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: progster <[hidden email]> To: [hidden email] Date: 02/26/2015 03:12 AM Subject: Re: [SPSSX-L] Python macros in order to use in spss Sent by: "SPSSX(r) Discussion" <[hidden email]> I prepared this macro in spss. now i would like to add a loop that could change COLPCT.COUNT to rowPCT.COUNT and tablePCT.COUNT. I know that I could add the 3 parameters in a single table, but analysis sake we prefer having 3 sepatare tables. Also I could add a new tokens that substitute the COLPCT.COUNT, rowPCT.COUNT and tablePCT.COUNT, but it would generate a lot of macro calls, it's not as efficient as a nested macro. I tried to add a define into the define but it's not allowed. Is there a way with Python or spss to add a nested macro? DEFINE !dos (x1 !TOKENS(1)/x2 !TOKENS(1)) CTABLES /VLABELS VARIABLES=!x1 !x2 DISPLAY=LABEL /TABLE !x1[C] BY !x2 [C][COUNT F40.0, COLPCT.COUNT PCT40.1] /CATEGORIES VARIABLES=!x1 !x2 ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=AFTER. !ENDDEFINE. !dos x1=p05 x2=log_segment. !dos x1=p02 x2=log_segment. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-macros-in-order-to-use-in-spss-tp5728491p5728819.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 ===================== 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 |
Administrator
|
In reply to this post by David Marso
Something like (untested)...building on your previous post.
DEFINE !dos (x1 !TOKENS(1)/x2 !TOKENS(1)/stats !CMDEND) !DO !S !IN (!stats) CTABLES /VLABELS VARIABLES=!x1 !x2 DISPLAY=LABEL /TABLE !x1[C] BY !x2 [C][COUNT F40.0, !S PCT40.1] /CATEGORIES VARIABLES=!x1 !x2 ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=AFTER. !DOEND !ENDDEFINE. !dos x1=p05 x2=log_segment stats COLPCT.COUNT rowPCT.COUNT tablePCT.COUNT. !dos x1=p02 x2=log_segment stats COLPCT.COUNT rowPCT.COUNT tablePCT.COUNT.
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?" |
Free forum by Nabble | Edit this page |