Hey All,
I have been reading through the archives at http://www.listserv.uga.edu/archives/spssx-l.html in search of a method for CONJOINT analysis via syntax without the Conjoint Module described in the syntax reference.
Looks like Discovering Statistics Using IBM SPSS Statistics by Andy Field keeps coming up as a reference. Is anyone familiar with this method (Conjoint) or a resource for it using SPSS?
Peter
|
Administrator
|
I know of no such existing syntax/macro/?? . Maybe contact AF and have him give you the 411 (pass it back here if you get an answer).
AFAIK: You would have to either buy the module or research the maths (see the algorithms on line) and program it yourself in MATRIX or hire someone to do it for you (it would likely be cheaper to buy the module than the latter). --
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?" |
Hello to everybody:
I have data that looks liket this: DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. I need to create a new variable like this: STRING secciones (A8). COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). EXECUTE. But, here is the problem: For cases with inly one letter, I don,t want the comma, for cases with two letters, only one comma, like: "B,E" And i need to appply this to many datasets, eacyh wich has a diferent number of seccion_e variables (some has only one, other could have a maximun of 12). I think that some phyton code could do this, but I'm only begining to lean it: Ive try something like: BEGIN PROGRAM. import spssdata dataCursor=spss.Cursor([2,3,4]) secciones=dataCursor.fetchall() dataCursor.close() print "Secciones:", secciones data=spssdata.Spssdata(accessType='w') data.append(spssdata.vdef('detectadas', vlabel='Secciones detectadas',vtype=20)) data.commitdict() for i,row in enumerate(data): data.casevalues([secciones]) data.CClose() END PROGRAM. But it did'nt gave me the results I was specting Any help would be apreciated Thank you veru much Andres |
Hi Andres Why not try reading it in as a fixed-format file. Read in the first 8 characters as one variable, skip the following comma, read in the next field, skip the comma, and then read everything from column 9 to column 31? It won’t matter what number of alphabetic characters you have there and you can use the same spec for each file. Low tech solution with no programming needed. The solution depends on the files ALL, ALWAYS having the required comma between characters though Regards, Adrian Adrian Barnett Project Officer Educational Measurement and Analysis Data and Information Systems Department for Education and Child Development “Children and young people are at the centre of everything we do” From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of ANDRES ALBERTO BURGA LEON Hello to everybody: |
In reply to this post by ANDRES ALBERTO BURGA LEON
You could do this with traditional syntax
that builds up the required concatenation, but here is an easy solution
with Python. It uses the SPSSINC TRANS extension command to handle
passing the data and creating the dictionary update, but you could do that
directly in the Python code. The main part is the function, joiner,
that does the calculation. Note that I changed the variable names
slightly, because "." is not valid in a Python variable name.
DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e_1 (A) seccion_e_2 (A) seccion_e_3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. dataset name data. begin program. def joiner(*arg): return ",".join([item.rstrip() for item in arg if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /formula "joiner(seccion_e_1,seccion_e_2,seccion_e_3)". SPSSINC TRANS is available from the SPSS Community site if you don't already have it. If you get the latest version of this command from the Community site, you use TO to ease editing. spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_3 /formula "joiner(<>)" Regards, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/06/2013 06:59 PM Subject: [SPSSX-L] Create new variables concatening string variables (Phyton solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Hello to everybody: I have data that looks liket this: DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. I need to create a new variable like this: STRING secciones (A8). COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). EXECUTE. But, here is the problem: For cases with inly one letter, I don,t want the comma, for cases with two letters, only one comma, like: "B,E" And i need to appply this to many datasets, eacyh wich has a diferent number of seccion_e variables (some has only one, other could have a maximun of 12). I think that some phyton code could do this, but I'm only begining to lean it: Ive try something like: BEGIN PROGRAM. import spssdata dataCursor=spss.Cursor([2,3,4]) secciones=dataCursor.fetchall() dataCursor.close() print "Secciones:", secciones data=spssdata.Spssdata(accessType='w') data.append(spssdata.vdef('detectadas', vlabel='Secciones detectadas',vtype=20)) data.commitdict() for i,row in enumerate(data): data.casevalues([secciones]) data.CClose() END PROGRAM. But it did'nt gave me the results I was specting Any help would be apreciated Thank you veru much Andres |
Administrator
|
In reply to this post by ANDRES ALBERTO BURGA LEON
PLEASE START A NEW THREAD RATHER THAN HIJACKING SOMEONE ELSE'S!!!
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?" |
Administrator
|
In reply to this post by Barnett, Adrian (DECD)
Indeed!
phyton (sic )is so overkill for such an issue! --
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?" |
Administrator
|
In reply to this post by Jon K Peck
Wow Jon, really???
---
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 Barnett, Adrian (DECD)
The problem is that I dont have the data
as fixed format.
The DATA LIST is only an exambple of my databases, thata are all SPSS files. Andres
Hi Andres Why not try reading it in as a fixed-format file. Read in the first 8 characters as one variable, skip the following comma, read in the next field, skip the comma, and then read everything from column 9 to column 31? It won’t matter what number of alphabetic characters you have there and you can use the same spec for each file. Low tech solution with no programming needed. The solution depends on the files ALL, ALWAYS having the required comma between characters though Regards, Adrian Adrian Barnett Project Officer Educational Measurement and Analysis Data and Information Systems Department for Education and Child Development “Children and young people are at the centre of everything we do” From: SPSSX(r) Discussion [[hidden email]] On Behalf Of ANDRES ALBERTO BURGA LEON Sent: Thursday, 7 March 2013 12:25 PM To: [hidden email] Subject: Create new variables concatening string variables (Phyton solution?) Hello to everybody: I have data that looks liket this: DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. I need to create a new variable like this: STRING secciones (A8). COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). EXECUTE. But, here is the problem: For cases with inly one letter, I don,t want the comma, for cases with two letters, only one comma, like: "B,E" And i need to appply this to many datasets, eacyh wich has a diferent number of seccion_e variables (some has only one, other could have a maximun of 12). I think that some phyton code could do this, but I'm only begining to lean it: Ive try something like: BEGIN PROGRAM. import spssdata dataCursor=spss.Cursor([2,3,4]) secciones=dataCursor.fetchall() dataCursor.close() print "Secciones:", secciones data=spssdata.Spssdata(accessType='w') data.append(spssdata.vdef('detectadas', vlabel='Secciones detectadas',vtype=20)) data.commitdict() for i,row in enumerate(data): data.casevalues([secciones]) data.CClose() END PROGRAM. But it did'nt gave me the results I was specting Any help would be apreciated Thank you veru much Andres |
Administrator
|
Well then: The following can be generalized readily to your situation.
It helps to provide a picture of the hill(s) you are attempting to scale!!!! A simple cobbled example leads contributors to second guess the variations on the theme. -- Look ma, no phyton (sic). Using phyton (sic) for such a basic issue is like going after a fly on the wall with a sledge hammer. I pick the buggers off with rubber bands from about 6 ft away (in the dark)! -- DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. STRING CatDog (A30). VECTOR food=seccion_e.1 TO seccion_e.3 . LOOP #bowl=1 TO 3. IF food(#bowl) NE "" CatDog=CONCAT(RTRIM(CatDog),food(#bowl),","). END LOOP. COMPUTE CatDog=SUBSTR(CatDog,1,RINDEX(CatDog,",")-1). LIST. cod_mod7 anexo seccion_e.1 seccion_e.2 seccion_e.3 CatDog 0126546 0 A C D A,C,D 0342876 0 B B 0928651 0 A A 0987126 0 A A 1298653 0 B E B,E 1298653 1 E E 2876543 0 C C 3928366 0 C C Number of cases read: 8 Number of cases listed: 8
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 Jon K Peck
Thanks Jon
This works fine, but the problem is that I have multiple databases (all in .sav), and they have diferent numbre of seccion_e variables. For example, one database coul only have two variables (seccion_e_1 and seccion_e_2). Other could have only one (seccion_e) Other, three (seccion_e_1, seccion_e_2, seccion_e_3), and so on... I was looking for a solution that could be implemented redarsless the number or seccion_e variables I I only have two variables, the sintax that sould be modified is: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_2 /formula "joiner(<>)" for six variables, would be: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_6 /formula "joiner(<>)" I don't know if i'm beeing clear, but the diferent databases to wich i need to do this tranformation could have any numbre of seccio_e variables, between 1 and 12, and I was looking for a solutions other tha having 12 diferent sintax codes, eachy one for ma diferent numbre of seccion_e variables Any help would be apreciated
You could do this with traditional syntax that builds up the required concatenation, but here is an easy solution with Python. It uses the SPSSINC TRANS extension command to handle passing the data and creating the dictionary update, but you could do that directly in the Python code. The main part is the function, joiner, that does the calculation. Note that I changed the variable names slightly, because "." is not valid in a Python variable name. DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e_1 (A) seccion_e_2 (A) seccion_e_3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. dataset name data. begin program. def joiner(*arg): return ",".join([item.rstrip() for item in arg if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /formula "joiner(seccion_e_1,seccion_e_2,seccion_e_3)". SPSSINC TRANS is available from the SPSS Community site if you don't already have it. If you get the latest version of this command from the Community site, you use TO to ease editing. spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_3 /formula "joiner(<>)" Regards, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/06/2013 06:59 PM Subject: [SPSSX-L] Create new variables concatening string variables (Phyton solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Hello to everybody: I have data that looks liket this: DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. I need to create a new variable like this: STRING secciones (A8). COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). EXECUTE. But, here is the problem: For cases with inly one letter, I don,t want the comma, for cases with two letters, only one comma, like: "B,E" And i need to appply this to many datasets, eacyh wich has a diferent number of seccion_e variables (some has only one, other could have a maximun of 12). I think that some phyton code could do this, but I'm only begining to lean it: Ive try something like: BEGIN PROGRAM. import spssdata dataCursor=spss.Cursor([2,3,4]) secciones=dataCursor.fetchall() dataCursor.close() print "Secciones:", secciones data=spssdata.Spssdata(accessType='w') data.append(spssdata.vdef('detectadas', vlabel='Secciones detectadas',vtype=20)) data.commitdict() for i,row in enumerate(data): data.casevalues([secciones]) data.CClose() END PROGRAM. But it did'nt gave me the results I was specting Any help would be apreciated Thank you veru much Andres |
Piece of cake with one more line of Python
code. This code generates a macro definition for the list of variable
names that begin with
seccion_e_ and then uses that in the SPSSINC TRANS command. The joiner function is revised to sort the values since the macro definition will have the qualifying variables listed in random order. This is an example of the power of Python programmability to generalize code by abstracting away from details of individual datasets such as, in this case, the number of seccion variables. begin program. import spss, spssaux vardict = spssaux.VariableDict(pattern="seccion_e_.*") spss.SetMacroValue("!seccion", " ".join(vardict.variables)) def joiner(*arg): return ",".join([item.rstrip() for item in sorted(arg) if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /variables !seccion /formula "joiner(<>)" Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/07/2013 08:32 AM Subject: Re: [SPSSX-L] Create new variables concatenating string variables (Python solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Thanks Jon This works fine, but the problem is that I have multiple databases (all in .sav), and they have diferent numbre of seccion_e variables. For example, one database coul only have two variables (seccion_e_1 and seccion_e_2). Other could have only one (seccion_e) Other, three (seccion_e_1, seccion_e_2, seccion_e_3), and so on... I was looking for a solution that could be implemented redarsless the number or seccion_e variables I I only have two variables, the sintax that sould be modified is: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_2 /formula "joiner(<>)" for six variables, would be: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_6 /formula "joiner(<>)" I don't know if i'm beeing clear, but the diferent databases to wich i need to do this tranformation could have any numbre of seccio_e variables, between 1 and 12, and I was looking for a solutions other tha having 12 diferent sintax codes, eachy one for ma diferent numbre of seccion_e variables Any help would be apreciated
You could do this with traditional syntax that builds up the required concatenation, but here is an easy solution with Python. It uses the SPSSINC TRANS extension command to handle passing the data and creating the dictionary update, but you could do that directly in the Python code. The main part is the function, joiner, that does the calculation. Note that I changed the variable names slightly, because "." is not valid in a Python variable name. DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e_1 (A) seccion_e_2 (A) seccion_e_3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. dataset name data. begin program. def joiner(*arg): return ",".join([item.rstrip() for item in arg if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /formula "joiner(seccion_e_1,seccion_e_2,seccion_e_3)". SPSSINC TRANS is available from the SPSS Community site if you don't already have it. If you get the latest version of this command from the Community site, you use TO to ease editing. spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_3 /formula "joiner(<>)" Regards, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/06/2013 06:59 PM Subject: [SPSSX-L] Create new variables concatening string variables (Phyton solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Hello to everybody: I have data that looks liket this: DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. I need to create a new variable like this: STRING secciones (A8). COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). EXECUTE. But, here is the problem: For cases with inly one letter, I don,t want the comma, for cases with two letters, only one comma, like: "B,E" And i need to appply this to many datasets, eacyh wich has a diferent number of seccion_e variables (some has only one, other could have a maximun of 12). I think that some phyton code could do this, but I'm only begining to lean it: Ive try something like: BEGIN PROGRAM. import spssdata dataCursor=spss.Cursor([2,3,4]) secciones=dataCursor.fetchall() dataCursor.close() print "Secciones:", secciones data=spssdata.Spssdata(accessType='w') data.append(spssdata.vdef('detectadas', vlabel='Secciones detectadas',vtype=20)) data.commitdict() for i,row in enumerate(data): data.casevalues([secciones]) data.CClose() END PROGRAM. But it did'nt gave me the results I was specting Any help would be apreciated Thank you veru much Andres |
Sorry to botter you Jon, this gave me the following error: The formula syntax given is invalid: invalid syntax (<string>, line 1) I don't get were the problem is...
Piece of cake with one more line of Python code. This code generates a macro definition for the list of variable names that begin with seccion_e_ and then uses that in the SPSSINC TRANS command. The joiner function is revised to sort the values since the macro definition will have the qualifying variables listed in random order. This is an example of the power of Python programmability to generalize code by abstracting away from details of individual datasets such as, in this case, the number of seccion variables. begin program. import spss, spssaux vardict = spssaux.VariableDict(pattern="seccion_e_.*") spss.SetMacroValue("!seccion", " ".join(vardict.variables)) def joiner(*arg): return ",".join([item.rstrip() for item in sorted(arg) if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /variables !seccion /formula "joiner(<>)" Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/07/2013 08:32 AM Subject: Re: [SPSSX-L] Create new variables concatenating string variables (Python solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Thanks Jon This works fine, but the problem is that I have multiple databases (all in .sav), and they have diferent numbre of seccion_e variables. For example, one database coul only have two variables (seccion_e_1 and seccion_e_2). Other could have only one (seccion_e) Other, three (seccion_e_1, seccion_e_2, seccion_e_3), and so on... I was looking for a solution that could be implemented redarsless the number or seccion_e variables I I only have two variables, the sintax that sould be modified is: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_2 /formula "joiner(<>)" for six variables, would be: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_6 /formula "joiner(<>)" I don't know if i'm beeing clear, but the diferent databases to wich i need to do this tranformation could have any numbre of seccio_e variables, between 1 and 12, and I was looking for a solutions other tha having 12 diferent sintax codes, eachy one for ma diferent numbre of seccion_e variables Any help would be apreciated
You could do this with traditional syntax that builds up the required concatenation, but here is an easy solution with Python. It uses the SPSSINC TRANS extension command to handle passing the data and creating the dictionary update, but you could do that directly in the Python code. The main part is the function, joiner, that does the calculation. Note that I changed the variable names slightly, because "." is not valid in a Python variable name. DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e_1 (A) seccion_e_2 (A) seccion_e_3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. dataset name data. begin program. def joiner(*arg): return ",".join([item.rstrip() for item in arg if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /formula "joiner(seccion_e_1,seccion_e_2,seccion_e_3)". SPSSINC TRANS is available from the SPSS Community site if you don't already have it. If you get the latest version of this command from the Community site, you use TO to ease editing. spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_3 /formula "joiner(<>)" Regards, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/06/2013 06:59 PM Subject: [SPSSX-L] Create new variables concatening string variables (Phyton solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Hello to everybody: I have data that looks liket this: DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. I need to create a new variable like this: STRING secciones (A8). COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). EXECUTE. But, here is the problem: For cases with inly one letter, I don,t want the comma, for cases with two letters, only one comma, like: "B,E" And i need to appply this to many datasets, eacyh wich has a diferent number of seccion_e variables (some has only one, other could have a maximun of 12). I think that some phyton code could do this, but I'm only begining to lean it: Ive try something like: BEGIN PROGRAM. import spssdata dataCursor=spss.Cursor([2,3,4]) secciones=dataCursor.fetchall() dataCursor.close() print "Secciones:", secciones data=spssdata.Spssdata(accessType='w') data.append(spssdata.vdef('detectadas', vlabel='Secciones detectadas',vtype=20)) data.commitdict() for i,row in enumerate(data): data.casevalues([secciones]) data.CClose() END PROGRAM. But it did'nt gave me the results I was specting Any help would be apreciated Thank you veru much Andres |
Looks like the email mangled the indentation.
This line
return ",".join([item.rstrip() for item in sorted(arg) if item.rstrip() != ""]) needs to be indented. If that doesn't fix it, send me the code directly as a file attachment. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/07/2013 09:53 AM Subject: Re: [SPSSX-L] Create new variables concatenating string variables (Python solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Sorry to botter you Jon, this gave me the following error: The formula syntax given is invalid: invalid syntax (<string>, line 1) I don't get were the problem is...
Piece of cake with one more line of Python code. This code generates a macro definition for the list of variable names that begin with seccion_e_ and then uses that in the SPSSINC TRANS command. The joiner function is revised to sort the values since the macro definition will have the qualifying variables listed in random order. This is an example of the power of Python programmability to generalize code by abstracting away from details of individual datasets such as, in this case, the number of seccion variables. begin program. import spss, spssaux vardict = spssaux.VariableDict(pattern="seccion_e_.*") spss.SetMacroValue("!seccion", " ".join(vardict.variables)) def joiner(*arg): return ",".join([item.rstrip() for item in sorted(arg) if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /variables !seccion /formula "joiner(<>)" Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/07/2013 08:32 AM Subject: Re: [SPSSX-L] Create new variables concatenating string variables (Python solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Thanks Jon This works fine, but the problem is that I have multiple databases (all in .sav), and they have diferent numbre of seccion_e variables. For example, one database coul only have two variables (seccion_e_1 and seccion_e_2). Other could have only one (seccion_e) Other, three (seccion_e_1, seccion_e_2, seccion_e_3), and so on... I was looking for a solution that could be implemented redarsless the number or seccion_e variables I I only have two variables, the sintax that sould be modified is: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_2 /formula "joiner(<>)" for six variables, would be: spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_6 /formula "joiner(<>)" I don't know if i'm beeing clear, but the diferent databases to wich i need to do this tranformation could have any numbre of seccio_e variables, between 1 and 12, and I was looking for a solutions other tha having 12 diferent sintax codes, eachy one for ma diferent numbre of seccion_e variables Any help would be apreciated
You could do this with traditional syntax that builds up the required concatenation, but here is an easy solution with Python. It uses the SPSSINC TRANS extension command to handle passing the data and creating the dictionary update, but you could do that directly in the Python code. The main part is the function, joiner, that does the calculation. Note that I changed the variable names slightly, because "." is not valid in a Python variable name. DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e_1 (A) seccion_e_2 (A) seccion_e_3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. dataset name data. begin program. def joiner(*arg): return ",".join([item.rstrip() for item in arg if item.rstrip() != ""]) end program. spssinc trans result=secciones type=8 /formula "joiner(seccion_e_1,seccion_e_2,seccion_e_3)". SPSSINC TRANS is available from the SPSS Community site if you don't already have it. If you get the latest version of this command from the Community site, you use TO to ease editing. spssinc trans result=secciones type=8 /variables seccion_e_1 to seccion_e_3 /formula "joiner(<>)" Regards, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: ANDRES ALBERTO BURGA LEON <[hidden email]> To: [hidden email], Date: 03/06/2013 06:59 PM Subject: [SPSSX-L] Create new variables concatening string variables (Phyton solution?) Sent by: "SPSSX(r) Discussion" <[hidden email]> Hello to everybody: I have data that looks liket this: DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 (A) seccion_e.3 (A). BEGIN DATA 0126546,0,A,C,D 0342876,0,B 0928651,0,A 0987126,0,A 1298653,0,B,E 1298653,1,E 2876543,0,C 3928366,0,C END DATA. I need to create a new variable like this: STRING secciones (A8). COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). EXECUTE. But, here is the problem: For cases with inly one letter, I don,t want the comma, for cases with two letters, only one comma, like: "B,E" And i need to appply this to many datasets, eacyh wich has a diferent number of seccion_e variables (some has only one, other could have a maximun of 12). I think that some phyton code could do this, but I'm only begining to lean it: Ive try something like: BEGIN PROGRAM. import spssdata dataCursor=spss.Cursor([2,3,4]) secciones=dataCursor.fetchall() dataCursor.close() print "Secciones:", secciones data=spssdata.Spssdata(accessType='w') data.append(spssdata.vdef('detectadas', vlabel='Secciones detectadas',vtype=20)) data.commitdict() for i,row in enumerate(data): data.casevalues([secciones]) data.CClose() END PROGRAM. But it did'nt gave me the results I was specting Any help would be apreciated Thank you veru much Andres |
In reply to this post by ANDRES ALBERTO BURGA LEON
Well, you could always do an ADD FILES to combine all the files, then write it out as fixed format, and read it back in with that final block from column 9 onwards as one chunk. If there’s anything important differentiating each file from the other, that could be incorporated in the combined file. Probably quicker than debugging Python Regards, Adrian Adrian Barnett Project Officer Educational Measurement and Analysis Data and Information Systems Department for Education and Child Development “Children and young people are at the centre of everything we do” From: ANDRES ALBERTO BURGA LEON [mailto:[hidden email]] The problem is that I dont have the data as fixed format.
|
Administrator
|
That had occurred to me as well but I refrained from further comment because there will undoubtedly be additional 'data anomalies' which preclude such simplicity.
If I were in charge I would : 1. Be sure the seccion_e.XX variables are at the end of the file. 2. Create a 'fake' tossaway variable at the end and use my Looping code with the vector declared as: VECTOR v=seccion_e.1 TO @. Etc... debugging python is likely a walk in the park compared to debugging matrix macros (tempted to post before/after pics of my hair from 2 months ago compared to 2 months from now ;-) I have finally acquired v 21 so will be partying with python and other new toys as soon as I finish this current project (maybe in the middle, but then I invite the temptation of this going out of scope. YO! Another snakehead on the way ;-)) --
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?" |
Thank you very much Jon,
The Phyton code works well and is exactly what I was looking for, It helped me a lot Thanks to all other persons for they suggestions Kindly Andres
That had occurred to me as well but I refrained from further comment because there will undoubtedly be additional 'data anomalies' which preclude such simplicity. If I were in charge I would : 1. Be sure the seccion_e.XX variables are at the end of the file. 2. Create a 'fake' tossaway variable at the end and use my Looping code with the vector declared as: VECTOR v=seccion_e.1 TO @. Etc... debugging python is likely a walk in the park compared to debugging matrix macros (tempted to post before/after pics of my hair from 2 months ago compared to 2 months from now ;-) I have finally acquired v 21 so will be partying with python and other new toys as soon as I finish this current project (maybe in the middle, but then I invite the temptation of this going out of scope. YO! Another snakehead on the way ;-)) -- Barnett, Adrian (DECD) wrote > Well, you could always do an ADD FILES to combine all the files, then > write it out as fixed format, and read it back in with that final block > from column 9 onwards as one chunk. If there’s anything important > differentiating each file from the other, that could be incorporated in > the combined file. > Probably quicker than debugging Python > > Regards, > > Adrian > > > Adrian Barnett > Project Officer > Educational Measurement and Analysis > Data and Information Systems > Department for Education and Child Development > > “Children and young people are at the centre of everything we do” > > From: ANDRES ALBERTO BURGA LEON [mailto: > aburgal@.gob > ] > Sent: Friday, 8 March 2013 1:31 AM > To: Barnett, Adrian (DECD) > Cc: > SPSSX-L@.UGA > Subject: Re: Create new variables concatening string variables (Phyton > solution?) > > The problem is that I dont have the data as fixed format. > > The DATA LIST is only an exambple of my databases, thata are all SPSS > files. > > > Andres > > "Barnett, Adrian (DECD)" < > Adrian.Barnett2@.gov > <mailto: > Adrian.Barnett2@.gov > >> > Enviado por: "SPSSX(r) Discussion" < > SPSSX-L@.UGA > <mailto: > SPSSX-L@.UGA > >> > > 06/03/2013 09:15 p.m. > Por favor, responda a > "Barnett, Adrian (DECD)" < > Adrian.Barnett2@.gov > <mailto: > Adrian.Barnett2@.gov > >> > > > Para > SPSSX-L@.UGA > <mailto: > SPSSX-L@.UGA > > > > cc > > Asunto > > Re: Create new variables concatening string variables (Phyton > solution?) > > > > > > > > Hi Andres > Why not try reading it in as a fixed-format file. Read in the first 8 > characters as one variable, skip the following comma, read in the next > field, skip the comma, and then read everything from column 9 to column > 31? > It won’t matter what number of alphabetic characters you have there and > you can use the same spec for each file. > > Low tech solution with no programming needed. > > The solution depends on the files ALL, ALWAYS having the required comma > between characters though > > Regards, > > Adrian > > > Adrian Barnett > Project Officer > Educational Measurement and Analysis > Data and Information Systems > Department for Education and Child Development > > “Children and young people are at the centre of everything we do” > > From: SPSSX(r) Discussion [mailto: > SPSSX-L@.UGA > ] On Behalf Of ANDRES ALBERTO BURGA LEON > Sent: Thursday, 7 March 2013 12:25 PM > To: > SPSSX-L@.UGA > <mailto: > SPSSX-L@.UGA > > > Subject: Create new variables concatening string variables (Phyton > solution?) > > Hello to everybody: > > > I have data that looks liket this: > > DATA LIST LIST (",") /cod_mod7 (A8) anexo (A) seccion_e.1 (A) seccion_e.2 > (A) seccion_e.3 (A). > BEGIN DATA > 0126546,0,A,C,D > 0342876,0,B > 0928651,0,A > 0987126,0,A > 1298653,0,B,E > 1298653,1,E > 2876543,0,C > 3928366,0,C > END DATA. > > I need to create a new variable like this: > > STRING secciones (A8). > COMPUTE secciones=CONCAT(seccion_e.1,',',seccion_e.2,',',seccion_e.3). > EXECUTE. > > But, here is the problem: > > For cases with inly one letter, I don,t want the comma, for cases with two > letters, only one comma, like: "B,E" > > And i need to appply this to many datasets, eacyh wich has a diferent > number of seccion_e variables (some has only one, other could have a > maximun of 12). > > I think that some phyton code could do this, but I'm only begining to lean > it: > > Ive try something like: > > BEGIN PROGRAM. > import spssdata > dataCursor=spss.Cursor([2,3,4]) > secciones=dataCursor.fetchall() > dataCursor.close() > print "Secciones:", secciones > data=spssdata.Spssdata(accessType='w') > data.append(spssdata.vdef('detectadas', > vlabel='Secciones detectadas',vtype=20)) > data.commitdict() > for i,row in enumerate(data): > data.casevalues([secciones]) > data.CClose() > END PROGRAM. > > But it did'nt gave me the results I was specting > > Any help would be apreciated > > Thank you veru much > > Andres ----- Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/CONJOINT-tp5718414p5718471.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 |
Free forum by Nabble | Edit this page |