multiple response Several answers variable string and separated into different variables need

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

multiple response Several answers variable string and separated into different variables need

Javier Figueroa
Hello experts SPSS

Good morning, I hope that all their activities ARE realizadon with hits.

I need your help, I have a variable of type character with several response options one digit every answer and are not separated by commas or by white, now I need to transfer these responses to different variables, for example.
the string is called resp variabla and contine 123498, what I need is to generate six variables with var1 = 1 data, var2 = 2, var3 = 3, var4 = 4, var5 = 9, var6 = 8.

I found this macro is very good but I only work if the answers are separated by commas or white, I could help to change this macro and use it.

DATA LIST LIST /problem(A12) race(A12) dajavi(A12).
BEGIN DATA
'1 2' '4 5 6 9 10' '12348765'
'10 11 3 15' '5 2 3' '12548763'
END DATA.
LIST.


* Define a macro to do the job.
DEFINE !parse (var=!TOKENS(1) /nbval=!TOKENS(1))
COMPUTE !var=CONCAT(RTRIM(!var),' ').
STRING #str(A8).
VECTOR !var (!nbval F8.0).
COMPUTE #beg=1.
LOOP #cnt=1 TO !nbval.
+COMPUTE #str=SUBSTR(!var,#beg).
+COMPUTE #end=INDEX(#str,' ')-1.
+DO IF #end=-1.
+ BREAK.
+END IF.
+COMPUTE !var(#cnt)=NUMBER(SUBSTR(#str,1,#end),F8.0).
+COMPUTE #beg=#beg+#end+1.
END LOOP IF #end=-1.
EXECUTE.
!ENDDEFINE.

* Now call the macro.
!parse var=problem nbval=5.
!parse var=race nbval=5.
!parse var=dajavi nbval=8.

The attempt to modify this macro for my use but could

I look forward to your support

Thank you very much in advance.

--
Javier Figueroa
Procesamiento y Análisis de bases de datos

===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: multiple response Several answers variable string and separated into different variables need

Rick Oliver-3
data list list /stringvar (a6).
begin data
123456
123498
end data.
do repeat x=var1 to var6
  /y=1 2 3 4 5 6.
compute x=number(char.substr(stringvar,y,1),f1).
end repeat.
list.

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        Javier Figueroa <[hidden email]>
To:        [hidden email]
Date:        07/02/2015 11:25 AM
Subject:        multiple response Several answers variable string and separated into different variables need
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hello experts SPSS

Good morning, I hope that all their activities ARE realizadon with hits.

I need your help, I have a variable of type character with several response options one digit every answer and are not separated by commas or by white, now I need to transfer these responses to different variables, for example.
the string is called resp variabla and contine 123498, what I need is to generate six variables with var1 = 1 data, var2 = 2, var3 = 3, var4 = 4, var5 = 9, var6 = 8.

I found this macro is very good but I only work if the answers are separated by commas or white, I could help to change this macro and use it.

DATA LIST LIST /problem(A12) race(A12) dajavi(A12).
BEGIN DATA
'1 2' '4 5 6 9 10' '12348765'
'10 11 3 15' '5 2 3' '12548763'
END DATA.
LIST.


* Define a macro to do the job.
DEFINE !parse (var=!TOKENS(1) /nbval=!TOKENS(1))
COMPUTE !var=CONCAT(RTRIM(!var),' ').
STRING #str(A8).
VECTOR !var (!nbval F8.0).
COMPUTE #beg=1.
LOOP #cnt=1 TO !nbval.
+COMPUTE #str=SUBSTR(!var,#beg).
+COMPUTE #end=INDEX(#str,' ')-1.
+DO IF #end=-1.
+ BREAK.
+END IF.
+COMPUTE !var(#cnt)=NUMBER(SUBSTR(#str,1,#end),F8.0).
+COMPUTE #beg=#beg+#end+1.
END LOOP IF #end=-1.
EXECUTE.
!ENDDEFINE.

* Now call the macro.
!parse var=problem nbval=5.
!parse var=race nbval=5.
!parse var=dajavi nbval=8.

The attempt to modify this macro for my use but could

I look forward to your support

Thank you very much in advance.

--
Javier Figueroa
Procesamiento y Análisis de bases de datos

===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@...(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
Reply | Threaded
Open this post in threaded view
|

Re: multiple response Several answers variable string and separated into different variables need

David Marso
Administrator
Here is a method which uses this principle but with a bit more flexibility.
DATA LIST  / a b c (3A10).
BEGIN DATA
1234      345621    345632
122       1234      12345678
END DATA.

DEFINE !parse_num (vars !CHAREND('/') /max !cmdend )
!IF (!HEAD(!vars) !NE '') !THEN
!LET !v=!HEAD(!vars) !LET !m = !HEAD(!max)
!LET !vs=!CONCAT(!v,'_','1 TO ',!v,'_',!m)
NUMERIC !vs (F1).
VECTOR !v = !vs .
LOOP #=1 TO !m.
IF CHAR.SUBSTR(!v,#,1) NE '' !v(#)=NUMBER(CHAR.SUBSTR(!v,#,1),F1).
END LOOP.

/*Recursively call macro to run through vars and max lists*/.
!parse_num vars=!TAIL(!vars) / max=!TAIL(!max).
!IFEND
!ENDDEFINE.

!parse_num vars=a b c / max=4 6 8 .
/* run the following after all other desired transformations are done /*.
EXECUTE.

Rick Oliver wrote
data list list /stringvar (a6).
begin data
123456
123498
end data.
do repeat x=var1 to var6
  /y=1 2 3 4 5 6.
compute x=number(char.substr(stringvar,y,1),f1).
end repeat.
list.

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]



From:   Javier Figueroa <[hidden email]>
To:     [hidden email]
Date:   07/02/2015 11:25 AM
Subject:        multiple response Several answers variable string and
separated into different variables need
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



Hello experts SPSS

Good morning, I hope that all their activities ARE realizadon with hits.

I need your help, I have a variable of type character with several
response options one digit every answer and are not separated by commas or
by white, now I need to transfer these responses to different variables,
for example.
the string is called resp variabla and contine 123498, what I need is to
generate six variables with var1 = 1 data, var2 = 2, var3 = 3, var4 = 4,
var5 = 9, var6 = 8.

I found this macro is very good but I only work if the answers are
separated by commas or white, I could help to change this macro and use
it.

DATA LIST LIST /problem(A12) race(A12) dajavi(A12).
BEGIN DATA
'1 2' '4 5 6 9 10' '12348765'
'10 11 3 15' '5 2 3' '12548763'
END DATA.
LIST.


* Define a macro to do the job.
DEFINE !parse (var=!TOKENS(1) /nbval=!TOKENS(1))
COMPUTE !var=CONCAT(RTRIM(!var),' ').
STRING #str(A8).
VECTOR !var (!nbval F8.0).
COMPUTE #beg=1.
LOOP #cnt=1 TO !nbval.
+COMPUTE #str=SUBSTR(!var,#beg).
+COMPUTE #end=INDEX(#str,' ')-1.
+DO IF #end=-1.
+ BREAK.
+END IF.
+COMPUTE !var(#cnt)=NUMBER(SUBSTR(#str,1,#end),F8.0).
+COMPUTE #beg=#beg+#end+1.
END LOOP IF #end=-1.
EXECUTE.
!ENDDEFINE.

* Now call the macro.
!parse var=problem nbval=5.
!parse var=race nbval=5.
!parse var=dajavi nbval=8.

The attempt to modify this macro for my use but could

I look forward to your support

Thank you very much in advance.

--
Javier Figueroa
Procesamiento y Análisis de bases de datos

===================== 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
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?"
Reply | Threaded
Open this post in threaded view
|

Re: multiple response Several answers variable string and separated into different variables need

Jon K Peck
In reply to this post by Javier Figueroa
DO REPEAT works fine for this, but here is a simple Python solution.

begin program.
def f(arg):
  return list(arg)
end program.

spssinc trans result = y1 to y6 type=1
/formula f(stringvar).


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Javier Figueroa <[hidden email]>
To:        [hidden email]
Date:        07/02/2015 10:23 AM
Subject:        [SPSSX-L] multiple response Several answers variable string and separated into different variables need
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hello experts SPSS

Good morning, I hope that all their activities ARE realizadon with hits.

I need your help, I have a variable of type character with several response options one digit every answer and are not separated by commas or by white, now I need to transfer these responses to different variables, for example.
the string is called resp variabla and contine 123498, what I need is to generate six variables with var1 = 1 data, var2 = 2, var3 = 3, var4 = 4, var5 = 9, var6 = 8.

I found this macro is very good but I only work if the answers are separated by commas or white, I could help to change this macro and use it.

DATA LIST LIST /problem(A12) race(A12) dajavi(A12).
BEGIN DATA
'1 2' '4 5 6 9 10' '12348765'
'10 11 3 15' '5 2 3' '12548763'
END DATA.
LIST.


* Define a macro to do the job.
DEFINE !parse (var=!TOKENS(1) /nbval=!TOKENS(1))
COMPUTE !var=CONCAT(RTRIM(!var),' ').
STRING #str(A8).
VECTOR !var (!nbval F8.0).
COMPUTE #beg=1.
LOOP #cnt=1 TO !nbval.
+COMPUTE #str=SUBSTR(!var,#beg).
+COMPUTE #end=INDEX(#str,' ')-1.
+DO IF #end=-1.
+ BREAK.
+END IF.
+COMPUTE !var(#cnt)=NUMBER(SUBSTR(#str,1,#end),F8.0).
+COMPUTE #beg=#beg+#end+1.
END LOOP IF #end=-1.
EXECUTE.
!ENDDEFINE.

* Now call the macro.
!parse var=problem nbval=5.
!parse var=race nbval=5.
!parse var=dajavi nbval=8.

The attempt to modify this macro for my use but could

I look forward to your support

Thank you very much in advance.

--
Javier Figueroa
Procesamiento y Análisis de bases de datos

===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@... (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