SPSS 17 / Vista
is there a python command to read out the number of variable <lables> (!) of a variable? I would like to split a file into subfiles according to the numer of >values< of one variable. The number of values are dynamic, not static. There is a command in python that counts the number of variables, but I couldn't find a command to count the numer of variables like: for i in range(spss.GetVariableCount()): thank you
Dr. Frank Gaeth
|
yes, the number of distinct values (for example: male - female = 2) in variable: sex
so I understand there is no prepackaged python api for this My question: If I understand that correctly, I would have to use the aggregate function into a file and would then count the number of rows in that file (probably)? And the number of rows matches the number of values in that particular variable (?) Thank you
Dr. Frank Gaeth
|
Using AGGREGATE would be a good and efficient
way to do this assuming that you only need to count one or a few variables.
If you need to do this for many variables, there would be other more
efficient ways. You can, of course, generate the AGGREGATE command(s)
from your program.
Use a dataset rather than file with AGGREGATE so that you do not have to worry about directories. Then activate that dataset and use spss.GetCaseCount() to find the size. Jon Peck Senior Software Engineer, IBM [hidden email] 312-651-3435 From: drfg2008 <[hidden email]> To: [hidden email] Date: 02/21/2011 09:59 AM Subject: Re: [SPSSX-L] Python Sent by: "SPSSX(r) Discussion" <[hidden email]> yes, the number of distinct values (for example: male - female = 2) in variable: sex so I understand there is no prepackaged python api for this My question: If I understand that correctly, I would have to use the aggregate function into a file and would then count the number of rows in that file (probably)? And the number of rows matches the number of values in that particular variable (?) Thank you ----- FUB -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-tp3394344p3394416.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 drfg2008
Thank you Mario!
I simply have a problem with the following line: file= r"H:\test\var_"+varname #include path here especially var_"+varname you connect a path with the variable name? and what does var_ stand for? -------------------------------------------- Hi, maybe sth. like that is what you want? /*============================================*/ BEGIN PROGRAM PYTHON. import spss,spssaux varname = "s01" # insert variable name here file= r"H:\test\var_"+varname #include path here for i in xrange(spss.GetVariableCount()): if spss.GetVariableName(i)==varname: position=i break syntax = "" k=spssaux.GetValueLabels(position).iteritems() for i in k: syntax = syntax + "TEMPORARY.\nSELECT IF(" + varname + "=" + str(i[0]) +").\n" syntax = syntax + "SAVE OUTFILE = '"+file+"_"+str(i[0])+".sav'.\n" print syntax END PROGRAM. /*============================================*/ Viel Erfolg! Mario
Dr. Frank Gaeth
|
In reply to this post by drfg2008
Thank you again!
I checked it, but still it will not run. These 'spssaux programming' isn't that easy to understand, as I presume (for example I don't quite understand how the source-file is read, so I simply put it at the beginning of the syntax. I checked the "Programming and Data Management for SPSS® Statistics 17.0", but couldn't find no help. I also don't know if it works with SPSS 17 - and python plugin (no extra plugin for 'aux-programs'?). You see, I'm a total aux-beginner! ;-) this is my syntax now (that doesn't even start) GET FILE='C:\user\frank\spss-seminar\FILE_SPLITTEN.sav'. DATASET NAME DatenSet1 WINDOW=FRONT. BEGIN PROGRAM PYTHON. import spss,spssaux varname = "gruppe" file= r"C:\user\frank\spss-seminar\var_"+varname for i in xrange(spss.GetVariableCount()): if spss.GetVariableName(i)==varname: position=i break syntax = "" k=spssaux.GetValueLabels(position).iteritems() for i in k: syntax = syntax + "TEMPORARY.\nSELECT IF(" + varname + "=" + str(i[0]) +").\n" syntax = syntax + "SAVE OUTFILE = '"+file+"_"+str(i[0])+".sav'.\n" print syntax END PROGRAM.
Dr. Frank Gaeth
|
Hi, I'm having trouble understanding what you want to achieve with your syntax. Do you want to save a separate file for each value of a given variable ('gruppe', in your case)? This is untested as I don't have spss here: import spss, spssaux, os def splitIt(path, varname): syntax = """ temporary. select if(%s eq %s). save outfile = '%s'.""" vd = spssaux.VariableDict()[varname] for k, v in vd.ValueLabels.iteritems(): f = os.path.join(path, "var_" + varname + str(k).zfill(2) + ".sav") spss.Submit(syntax % (varname, k, f)) splitIt(path = r"C:\user\frank\spss-seminar", varname = "gruppe") 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: drfg2008 <[hidden email]> To: [hidden email] Sent: Tue, February 22, 2011 6:27:48 PM Subject: Re: [SPSSX-L] Python Thank you again! I checked it, but still it will not run. These 'spssaux programming' isn't that easy to understand, as I presume (for example I don't quite understand how the source-file is read, so I simply put it at the beginning of the syntax. I checked the "Programming and Data Management for SPSS® Statistics 17.0", but couldn't find no help. I also don't know if it works with SPSS 17 - and python plugin (no extra plugin for 'aux-programs'?). You see, I'm a total aux-beginner! ;-) this is my syntax now (that doesn't even start) GET FILE='C:\user\frank\spss-seminar\FILE_SPLITTEN.sav'. DATASET NAME DatenSet1 WINDOW=FRONT. BEGIN PROGRAM PYTHON. import spss,spssaux varname = "gruppe" file= r"C:\user\frank\spss-seminar\var_"+varname for i in xrange(spss.GetVariableCount()): if spss.GetVariableName(i)==varname: position=i break syntax = "" k=spssaux.GetValueLabels(position).iteritems() for i in k: syntax = syntax + "TEMPORARY.\nSELECT IF(" + varname + "=" + str(i[0]) +").\n" syntax = syntax + "SAVE OUTFILE = '"+file+"_"+str(i[0])+".sav'.\n" print syntax END PROGRAM. ----- FUB -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-tp3394344p3395865.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 drfg2008
Thank you, everybody!
Somehow my first reply must have been dropped (I can't find it, so I post it again). I already have a ( at least for me) easy to understand solution, which is fully sufficient input program. loop a =1 to 100000 by 1. end case. end loop. end file. end input program. exe. comp gruppe=RV.BINOM(10,0.5). COMP av_1=RV.NORMAL(1,10). COMP av_2=RV.NORMAL(1,10). COMP av_3=RV.NORMAL(1,10). COMP av_4=RV.NORMAL(1,10). EXECUTE . SAVE OUTFILE='C:\<path>\FILE_SPLITTEN.sav'. *-----End: generate Data --------------------. *---------- Start ------------------------------. GET FILE='C:\<path>\FILE_SPLITTEN.sav'. DATASET NAME DatenSet1 WINDOW=FRONT. AUTORECODE VARIABLES=gruppe /INTO gruppe_codiert /PRINT. DATASET DECLARE anzahl_gruppen. AGGREGATE /OUTFILE='anzahl_gruppen' /BREAK=v3 /N_BREAK=N. DATASET ACTIVATE anzahl_gruppen. BEGIN PROGRAM. import spss FileGROUPS=spss.GetCaseCount() spss.Submit("DATASET ACTIVATE DatenSet1.") for i in range(FileGROUPS): spss.Submit("temporary.") spss.Submit(" SELECT IF gruppe_codiert = " +str(i+1) +".") spss.Submit("SAVE OUTFILE='C:/<path>/gruppe_ " +str( i+1) + ".sav'") END PROGRAM. Thank you!
Dr. Frank Gaeth
|
In reply to this post by drfg2008
What is the character r supposed to be doing after the equals sign on the file = statement? Try removing it.
Garry Gelade Business Analytic Ltd. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of drfg2008 Sent: 22 February 2011 17:28 To: [hidden email] Subject: Re: Python Thank you again! I checked it, but still it will not run. These 'spssaux programming' isn't that easy to understand, as I presume (for example I don't quite understand how the source-file is read, so I simply put it at the beginning of the syntax. I checked the "Programming and Data Management for SPSS® Statistics 17.0", but couldn't find no help. I also don't know if it works with SPSS 17 - and python plugin (no extra plugin for 'aux-programs'?). You see, I'm a total aux-beginner! ;-) this is my syntax now (that doesn't even start) GET FILE='C:\user\frank\spss-seminar\FILE_SPLITTEN.sav'. DATASET NAME DatenSet1 WINDOW=FRONT. BEGIN PROGRAM PYTHON. import spss,spssaux varname = "gruppe" file= r"C:\user\frank\spss-seminar\var_"+varname for i in xrange(spss.GetVariableCount()): if spss.GetVariableName(i)==varname: position=i break syntax = "" k=spssaux.GetValueLabels(position).iteritems() for i in k: syntax = syntax + "TEMPORARY.\nSELECT IF(" + varname + "=" + str(i[0]) +").\n" syntax = syntax + "SAVE OUTFILE = '"+file+"_"+str(i[0])+".sav'.\n" print syntax END PROGRAM. ----- FUB -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-tp3394344p3395865.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 |
It's a raw string announcement and necessary with these backslashes.
Be sure that
+").\n"
part is in the same line as the syntax command Be also sure to have spssaux installed.
GL
Mario
Von: Garry Gelade <[hidden email]> An: [hidden email] Gesendet: Mittwoch, den 23. Februar 2011, 13:19:59 Uhr Betreff: Re: Python What is the character r supposed to be doing after the equals sign on the file = statement? Try removing it. Garry Gelade Business Analytic Ltd. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of drfg2008 Sent: 22 February 2011 17:28 To: [hidden email] Subject: Re: Python Thank you again! I checked it, but still it will not run. These 'spssaux programming' isn't that easy to understand, as I presume (for example I don't quite understand how the source-file is read, so I simply put it at the beginning of the syntax. I checked the "Programming and Data Management for SPSS® Statistics 17.0", but couldn't find no help. I also don't know if it works with SPSS 17 - and python plugin (no extra plugin for 'aux-programs'?). You see, I'm a total aux-beginner! ;-) this is my syntax now (that doesn't even start) GET FILE='C:\user\frank\spss-seminar\FILE_SPLITTEN.sav'. DATASET NAME DatenSet1 WINDOW=FRONT. BEGIN PROGRAM PYTHON. import spss,spssaux varname = "gruppe" file= r"C:\user\frank\spss-seminar\var_"+varname for i in xrange(spss.GetVariableCount()): if spss.GetVariableName(i)==varname: position=i break syntax = "" k=spssaux.GetValueLabels(position).iteritems() for i in k: syntax = syntax + "TEMPORARY.\nSELECT IF(" + varname + "=" + str(i[0]) +").\n" syntax = syntax + "SAVE OUTFILE = '"+file+"_"+str(i[0])+".sav'.\n" print syntax END PROGRAM. ----- FUB -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-tp3394344p3395865.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
Mario Giesel
Munich, Germany |
Free forum by Nabble | Edit this page |