Hola Expertos de SPSS!
He intentado concatenar multples variables tipo numérico a una sola variable alfanumérica separando los datos con comas sin éxito, genere un sintaxis y no me funciona.
Lo que necesito es esto: Mis datos: numero v1 v2 v3 v4 v5 v6 1 1 69 41 . . . necesito pasarlo a:
numero datos 1 1,69,41 este es el sintaxis que genere sin exito, DATA LIST / ncuest 1-2 D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 3-14.
BEGIN DATA 1 1 41 14 69 . . 2 69 25 2 . . . 3 1 69 . . . . 4 1 41 . . . . 5 69 . . . . . . 6 1 69 2 . . . 7 2 99 . . . . 8 41 14 . . . .
9 2 14 25 . . . 10 41 . . . . . 11 41 98 . . . . 12 1 69 14 . . . 13 41 69 . . . . 14 1 2 69 14 25 . END DATA. VECTOR D1=D1_1a1 TO D1_6a1.
COMPUTE F1=0. COMPUTE F3=0. STRING DM_1 (A80). COMPUTE ncontador=0. LOOP #J=1 TO 6. - DO IF NOT MISSING(D1(#J)).
- COMPUTE ncontador=ncontador+1. - IF D1(#J)=ncontador F3=1. - END IF. - DO IF F3=1. - COMPUTE DM_1 = CONCAT(RTRIM(STRING(D1(#J),F2.0)),",",STRING(D1(#J),F2.0)).
- END IF. END LOOP. EXECUTE. me podrían ayudar con este problema, estaré muy agradecido. Atentamente, Javier Figueroa Procesamiento y Análisis de bases de datos |
At 06:14 PM 12/3/2013, Javier Figueroa wrote:
>He intentado concatenar multples variables tipo >numérico a una sola variable alfanumérica >separando los datos con comas sin éxito, genere un sintaxis y no me funciona. > >Lo que necesito es esto: > >Mis datos: >numero v1 v2 v3 v4 v5 v6 >1 1 69 41 . . . > >necesito pasarlo a: > >numero datos >1 1,69,41 Forgive my replying in English. I don't know Spanish nearly well enough to write it. Does this solve your problem? (It uses the data you supplied.) VECTOR D1=D1_1a1 TO D1_6a1. NUMERIC ncontador(F2). STRING DM_1 (A20). COMPUTE ncontador=0. LOOP #J=1 TO 6. - DO IF NOT MISSING(D1(#J)). - COMPUTE ncontador=ncontador+1. - DO IF LENGTH(RTRIM(DM_1)) EQ 0. - COMPUTE DM_1 = LTRIM(STRING(D1(#J),F2.0)). - ELSE. - COMPUTE DM_1 = CONCAT(RTRIM(DM_1), ',', LTRIM(STRING(D1(#J),F2.0))). - END IF. - END IF. END LOOP. LIST. List |-----------------------------|----------------------------| |Output Created |04-DEC-2013 14:59:28 | |-----------------------------|----------------------------| ncuest D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 ncontador DM_1 1 1 4 1 14 6 9 6 1,4,1,14,6,9 2 69 2 5 2 . . 4 69,2,5,2 3 1 6 9 . . . 3 1,6,9 4 1 4 1 . . . 3 1,4,1 5 69 . . . . . 1 69 6 1 6 9 2 . . 4 1,6,9,2 7 2 9 9 . . . 3 2,9,9 8 41 1 4 . . . 3 41,1,4 9 2 1 4 25 . . 4 2,1,4,25 10 4 1 . . . . 2 4,1 11 4 1 98 . . . 3 4,1,98 12 . 1 69 1 4 . 4 1,69,1,4 13 4 1 69 . . . 3 4,1,69 14 . 1 2 6 9 14 5 1,2,6,9,14 Number of cases read: 14 Number of cases listed: 14 ============================ APPENDIX: Test data and code ============================ DATA LIST / ncuest 1-2 D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 3-14. BEGIN DATA 1 1 41 14 69 . . 2 69 25 2 . . . 3 1 69 . . . . 4 1 41 . . . . 5 69 . . . . . . 6 1 69 2 . . . 7 2 99 . . . . 8 41 14 . . . . 9 2 14 25 . . . 10 41 . . . . . 11 41 98 . . . . 12 1 69 14 . . . 13 41 69 . . . . 14 1 2 69 14 25 . END DATA. LIST. VECTOR D1=D1_1a1 TO D1_6a1. NUMERIC ncontador(F2). STRING DM_1 (A20). COMPUTE ncontador=0. LOOP #J=1 TO 6. - DO IF NOT MISSING(D1(#J)). - COMPUTE ncontador=ncontador+1. - DO IF LENGTH(RTRIM(DM_1)) EQ 0. - COMPUTE DM_1 = LTRIM(STRING(D1(#J),F2.0)). - ELSE. - COMPUTE DM_1 = CONCAT(RTRIM(DM_1), ',', LTRIM(STRING(D1(#J),F2.0))). - END IF. - END IF. END LOOP. LIST. ===================== 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
|
Here's another approach, using Richard's data to illustrate.
NEW FILE. DATASET CLOSE all. DATA LIST / ncuest 1-2 D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 3-14. BEGIN DATA 1 1 41 14 69 . . 2 69 25 2 . . . 3 1 69 . . . . 4 1 41 . . . . 5 69 . . . . . . 6 1 69 2 . . . 7 2 99 . . . . 8 41 14 . . . . 9 2 14 25 . . . 10 41 . . . . . 11 41 98 . . . . 12 1 69 14 . . . 13 41 69 . . . . 14 1 2 69 14 25 . END DATA. STRING #copy1 to #copy6 (A3) DM_1(A20). DO REPEAT old = D1_1a1 to D1_6a1 / new = #copy1 to #copy6. - COMPUTE new = CONCAT( RTRIM(LTRIM(STRING(old,F3))),","). - COMPUTE DM_1 = CONCAT(RTRIM(DM_1),LTRIM(new)). END REPEAT. COMPUTE DM_1 = RTRIM(RTRIM(REPLACE(DM_1,".,","")),","). LIST. Output: ncuest D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 DM_1 1 1 4 1 14 6 9 1,4,1,14,6,9 2 69 2 5 2 . . 69,2,5,2 3 1 6 9 . . . 1,6,9 4 1 4 1 . . . 1,4,1 5 69 . . . . . 69 6 1 6 9 2 . . 1,6,9,2 7 2 9 9 . . . 2,9,9 8 41 1 4 . . . 41,1,4 9 2 1 4 25 . . 2,1,4,25 10 4 1 . . . . 4,1 11 4 1 98 . . . 4,1,98 12 . 1 69 1 4 . 1,69,1,4 13 4 1 69 . . . 4,1,69 14 . 1 2 6 9 14 1,2,6,9,14 Number of cases read: 14 Number of cases listed: 14
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
If all those RTRIMS make you dizzy, remember
that in Unicode mode, string values are automatically rtrimmed for blanks.
STRING #copy1 to #copy6 (A3) DM_1(A20). DO REPEAT old = D1_1a1 to D1_6a1 / new = #copy1 to #copy6. - COMPUTE new = CONCAT(LTRIM(STRING(old,F3)),","). - COMPUTE DM_1 = CONCAT(DM_1,LTRIM(new)). END REPEAT. COMPUTE DM_1 = RTRIM(REPLACE(DM_1,".,",""),","). Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: Bruce Weaver <[hidden email]> To: [hidden email], Date: 12/05/2013 09:29 AM Subject: Re: [SPSSX-L] Concatenar multiples variables numericas a una variable tipo alfanumerica (string) separados con comas Sent by: "SPSSX(r) Discussion" <[hidden email]> Here's another approach, using Richard's data to illustrate. NEW FILE. DATASET CLOSE all. DATA LIST / ncuest 1-2 D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 3-14. BEGIN DATA 1 1 41 14 69 . . 2 69 25 2 . . . 3 1 69 . . . . 4 1 41 . . . . 5 69 . . . . . . 6 1 69 2 . . . 7 2 99 . . . . 8 41 14 . . . . 9 2 14 25 . . . 10 41 . . . . . 11 41 98 . . . . 12 1 69 14 . . . 13 41 69 . . . . 14 1 2 69 14 25 . END DATA. STRING #copy1 to #copy6 (A3) DM_1(A20). DO REPEAT old = D1_1a1 to D1_6a1 / new = #copy1 to #copy6. - COMPUTE new = CONCAT( RTRIM(LTRIM(STRING(old,F3))),","). - COMPUTE DM_1 = CONCAT(RTRIM(DM_1),LTRIM(new)). END REPEAT. COMPUTE DM_1 = RTRIM(RTRIM(REPLACE(DM_1,".,","")),","). LIST. Output: ncuest D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 DM_1 1 1 4 1 14 6 9 1,4,1,14,6,9 2 69 2 5 2 . . 69,2,5,2 3 1 6 9 . . . 1,6,9 4 1 4 1 . . . 1,4,1 5 69 . . . . . 69 6 1 6 9 2 . . 1,6,9,2 7 2 9 9 . . . 2,9,9 8 41 1 4 . . . 41,1,4 9 2 1 4 25 . . 2,1,4,25 10 4 1 . . . . 4,1 11 4 1 98 . . . 4,1,98 12 . 1 69 1 4 . 1,69,1,4 13 4 1 69 . . . 4,1,69 14 . 1 2 6 9 14 1,2,6,9,14 Number of cases read: 14 Number of cases listed: 14 Richard Ristow wrote > At 06:14 PM 12/3/2013, Javier Figueroa wrote: > >>He intentado concatenar multples variables tipo >>numérico a una sola variable alfanumérica >>separando los datos con comas sin éxito, genere un sintaxis y no me funciona. >> >>Lo que necesito es esto: >> >>Mis datos: >>numero v1 v2 v3 v4 v5 v6 >>1 1 69 41 . . . >> >>necesito pasarlo a: >> >>numero datos >>1 1,69,41 > > Forgive my replying in English. I don't know > Spanish nearly well enough to write it. > > Does this solve your problem? (It uses the data you supplied.) > > > VECTOR D1=D1_1a1 TO D1_6a1. > > NUMERIC ncontador(F2). > STRING DM_1 (A20). > > COMPUTE ncontador=0. > > LOOP #J=1 TO 6. > - DO IF NOT MISSING(D1(#J)). > - COMPUTE ncontador=ncontador+1. > - DO IF LENGTH(RTRIM(DM_1)) EQ 0. > - COMPUTE DM_1 = LTRIM(STRING(D1(#J),F2.0)). > - ELSE. > - COMPUTE DM_1 = CONCAT(RTRIM(DM_1), > ',', > LTRIM(STRING(D1(#J),F2.0))). > - END IF. > - END IF. > END LOOP. > > LIST. > List > |-----------------------------|----------------------------| > |Output Created |04-DEC-2013 14:59:28 | > |-----------------------------|----------------------------| > ncuest D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 ncontador DM_1 > > 1 1 4 1 14 6 9 6 1,4,1,14,6,9 > 2 69 2 5 2 . . 4 69,2,5,2 > 3 1 6 9 . . . 3 1,6,9 > 4 1 4 1 . . . 3 1,4,1 > 5 69 . . . . . 1 69 > 6 1 6 9 2 . . 4 1,6,9,2 > 7 2 9 9 . . . 3 2,9,9 > 8 41 1 4 . . . 3 41,1,4 > 9 2 1 4 25 . . 4 2,1,4,25 > 10 4 1 . . . . 2 4,1 > 11 4 1 98 . . . 3 4,1,98 > 12 . 1 69 1 4 . 4 1,69,1,4 > 13 4 1 69 . . . 3 4,1,69 > 14 . 1 2 6 9 14 5 1,2,6,9,14 > > Number of cases read: 14 Number of cases listed: 14 > ============================ > APPENDIX: Test data and code > ============================ > DATA LIST > / ncuest 1-2 D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 3-14. > BEGIN DATA > 1 1 41 14 69 . . > 2 69 25 2 . . . > 3 1 69 . . . . > 4 1 41 . . . . > 5 69 . . . . . . > 6 1 69 2 . . . > 7 2 99 . . . . > 8 41 14 . . . . > 9 2 14 25 . . . > 10 41 . . . . . > 11 41 98 . . . . > 12 1 69 14 . . . > 13 41 69 . . . . > 14 1 2 69 14 25 . > END DATA. > LIST. > VECTOR D1=D1_1a1 TO D1_6a1. > > NUMERIC ncontador(F2). > STRING DM_1 (A20). > > COMPUTE ncontador=0. > > LOOP #J=1 TO 6. > - DO IF NOT MISSING(D1(#J)). > - COMPUTE ncontador=ncontador+1. > - DO IF LENGTH(RTRIM(DM_1)) EQ 0. > - COMPUTE DM_1 = LTRIM(STRING(D1(#J),F2.0)). > - ELSE. > - COMPUTE DM_1 = CONCAT(RTRIM(DM_1), > ',', > LTRIM(STRING(D1(#J),F2.0))). > - END IF. > - END IF. > END LOOP. > > LIST. > > ===================== > To manage your subscription to SPSSX-L, send a message to > LISTSERV@.UGA > (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 ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Concatenar-multiples-variables-numericas-a-una-variable-tipo-alfanumerica-string-separados-con-comas-tp5723408p5723458.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 |
Administrator
|
In reply to this post by Bruce Weaver
Here is yet another approach.
NEW FILE. DATASET CLOSE all. DATA LIST / ncuest 1-2 D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 3-14. BEGIN DATA 1 1 41 14 69 . . 2 69 25 2 . . . 3 1 69 . . . . 4 1 41 . . . . 5 69 . . . . . . 6 1 69 2 . . . 7 2 99 . . . . 8 41 14 . . . . 9 2 14 25 . . . 10 41 . . . . . 11 41 98 . . . . 12 1 69 14 . . . 13 41 69 . . . . 14 1 2 69 14 25 . END DATA. SAVE TRANSLATE OUTFILE='C:\TEMP\Comma.csv' /TYPE=CSV. DATA LIST FILE ='C:\TEMP\Comma.csv' / A (A200). COMPUTE A=REPLACE(A," ,","" ). COMPUTE #L=CHAR.LENGTH(A). IF CHAR.SUBSTR(A,#L,1)="," A=CHAR.SUBSTR(A,1,#L-1). EXECUTE. ERASE FILE='C:\TEMP\Comma.csv' . LIST.
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?" |
Muchas gracias, Bruce y David por sus aportes, y si bien es sabido en programación hay muchas formas para llegar a un mismo punto, es cuestión de enfoque, muchas gracias de nuevo, esto sirve también para aprender mas de este poderoso Software SPSS. Atentamente, Javier Figueroa procesamiento de datos Independiente. 2013/12/5 David Marso <[hidden email]> Here is yet another approach. Javier Figueroa Procesamiento y Análisis de bases de datos |
In reply to this post by David Marso
Pythonically,
begin program. def merge(*args): return(",".join([str(int(item)) for item in args if item is not None])) end program. spssinc trans result=DM_1 type=25 /variables D1_1a1 to D1_6a1 /formula merge(<>). Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: David Marso <[hidden email]> To: [hidden email], Date: 12/05/2013 10:11 AM Subject: Re: [SPSSX-L] Concatenar multiples variables numericas a una variable tipo alfanumerica (string) separados con comas Sent by: "SPSSX(r) Discussion" <[hidden email]> Here is yet another approach. NEW FILE. DATASET CLOSE all. DATA LIST / ncuest 1-2 D1_1a1 D1_1a2 D1_1a3 D1_1a4 D1_1a5 D1_6a1 3-14. BEGIN DATA 1 1 41 14 69 . . 2 69 25 2 . . . 3 1 69 . . . . 4 1 41 . . . . 5 69 . . . . . . 6 1 69 2 . . . 7 2 99 . . . . 8 41 14 . . . . 9 2 14 25 . . . 10 41 . . . . . 11 41 98 . . . . 12 1 69 14 . . . 13 41 69 . . . . 14 1 2 69 14 25 . END DATA. *SAVE TRANSLATE OUTFILE='C:\TEMP\Comma.csv' /TYPE=CSV.* DATA LIST FILE ='C:\TEMP\Comma.csv' / A (A200). COMPUTE A=REPLACE(A," ,","" ). COMPUTE #L=CHAR.LENGTH(A). IF CHAR.SUBSTR(A,#L,1)="," A=CHAR.SUBSTR(A,1,#L-1). EXECUTE. ERASE FILE='C:\TEMP\Comma.csv' . LIST. |
Administrator
|
In reply to this post by Javier Figueroa
I suspect the original intent might be to get the data to another SW which is expecting a comma delimited format. Hence the SAVE TRANSLATE. Hence, scratch everything following that first line.
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
|
D'oh! You're probably right. Oh well, it was a fun exercise! ;-)
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Free forum by Nabble | Edit this page |