parsing out one variable to many

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

parsing out one variable to many

Nancy Rusinak-2
I am collecting survey data.  Respondents are asked to categorize their
comments into different buckets.  They can choose as many as they like or
none at all.  If they choose the first category, the data puts a "1" in the
field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a
"1" put in that variable if the respondent chose that bucket; a variable
called "communication" for category 2 and a "1" put in that variable if the
respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

=====================
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: parsing out one variable to many

John F Hall

Nancy

 

IN SPSS, easiest would be to have 15 variables, one for each response category.  If you label each value (1st variable only) you can then use MULT RESPONSE to tabulate the responses. 

 

val lab v1 1 ‘Strategy’ 2 ‘ ~~~~’  etc etc

mult resp category (v1 to v15 (1,15))

/freq category.

 

Another way is to label each variable and use MULT RESPONSE in dichotomous mode, but to do this you first have to recode the values for all variables to 1.  I’ve played safe and kept your originals, but recoded to a new variable

 

recode v1 to v15 (1 thru 15 = 1)(else =0) into r1 to r15.

var lab r1 ‘~~~’

/r2 ‘~~~’

  ~~~

/r15 ‘~~~’.

mult resp rcategory (r1 to r15 (1))

/freq rcategory.

 

There are examples of this (illustrated by multiple response questions from the British Social Attitudes survey 1987 and the European Social Survey 2002) in sections 3.2 and 3.3 (pp 27-35) and the 3rd slide show of my 2006 Old Dog, Old Tricks presentation SPSS usage in major surveys.

(See: http://surveyresearch.weebly.com/old-dog-old-tricks-using-spss-syntax-to-beat-the-mouse-trap.html)

 

Get back to me if you don’t understand that or if you need further help.

 

 

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

Start page:  www.surveyresearch.weebly.com/spss-without-tears.html

  

 

 

 

 

 

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Nancy Rusinak
Sent: 22 April 2013 15:54
To: [hidden email]
Subject: parsing out one variable to many

 

I am collecting survey data.  Respondents are asked to categorize their comments into different buckets.  They can choose as many as they like or none at all.  If they choose the first category, the data puts a "1" in the field.  If they choose the first and fifth category, the data reads "1,5."

There are 15 possible choices.

 

I'd like to create a variable called "strategy" for category 1 and have a "1" put in that variable if the respondent chose that bucket; a variable called "communication" for category 2 and a "1" put in that variable if the respondent chose that bucket and so on.

 

sample data, variable named qID_9109:

1,2

3,6,7

2

5,10,12,14

 

Can anyone help here?

 

=====================

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: parsing out one variable to many

David Marso
Administrator
In reply to this post by Nancy Rusinak-2
FWIW: That's a silly way to enter data!  Especially when you don't have a well thought out plan to turn it into something usable.
However, here is a mop up method.
http://spssx-discussion.1045642.n5.nabble.com/Parsing-String-Name-Variable-tc5714037.html#a5714040

Nancy Rusinak-2 wrote
I am collecting survey data.  Respondents are asked to categorize their
comments into different buckets.  They can choose as many as they like or
none at all.  If they choose the first category, the data puts a "1" in the
field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a
"1" put in that variable if the respondent chose that bucket; a variable
called "communication" for category 2 and a "1" put in that variable if the
respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

=====================
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: parsing out one variable to many

Art Kendall
In reply to this post by Nancy Rusinak-2
If you have not yet entered the data.

You need to have as many variables as the maximum number that any respondent choose.

For the data example you posted that would be 4 variables.
maybe named qID9109c1 to qID9109c4
then you would deal with that via MULT RESP .  This would be a multiple response set as opposed to a multiple dichotomy set.

Are your data already entered so that you have string variables that look like the examples you posted?


Art Kendall
Social Research Consultants
On 4/22/2013 9:54 AM, Nancy Rusinak-2 [via SPSSX Discussion] wrote:
I am collecting survey data.  Respondents are asked to categorize their
comments into different buckets.  They can choose as many as they like or
none at all.  If they choose the first category, the data puts a "1" in the
field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a
"1" put in that variable if the respondent chose that bucket; a variable
called "communication" for category 2 and a "1" put in that variable if the
respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

=====================
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



If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/parsing-out-one-variable-to-many-tp5719621.html
To start a new topic under SPSSX Discussion, email [hidden email]
To unsubscribe from SPSSX Discussion, click here.
NAML

Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: parsing out one variable to many

Maguin, Eugene
In reply to this post by Nancy Rusinak-2
Here's a not so compact method. A key drawback is that when the loop exits after processing a case a warning is issued. I don't  know whether there is a way to rewrite it to preclude the warning since you don't know how many responses are concatenated unless you scoot through each record and count the commas. Gene Maguin

String #s(a36).
Vector y(15,f2.0).
Compute #s=qID_9109.
If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
Loop if (char.len(#s) ge 2).
Compute #i=index(#s,',').
If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
if (char.len(#s) ge 4) #s=substr(#s,#i+1).
End loop.


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Nancy Rusinak
Sent: Monday, April 22, 2013 9:54 AM
To: [hidden email]
Subject: parsing out one variable to many

I am collecting survey data.  Respondents are asked to categorize their comments into different buckets.  They can choose as many as they like or none at all.  If they choose the first category, the data puts a "1" in the field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a "1" put in that variable if the respondent chose that bucket; a variable called "communication" for category 2 and a "1" put in that variable if the respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

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

Re: parsing out one variable to many

Bruce Weaver
Administrator
Gene, I think the problem was that your condition for exiting the loop was never becoming true, so it was only exiting the loop when it the loop number exceeded MXLOOPS.  Given that we know 15 is the maximum number of loops (in this case, at least), if you LOOP just 15 times, it works with no warning messages.  E.g.,

data list list / qID_9109(a36).
begin data
"1,2"
"3,6,7"
"2"
"5,10,12,14"
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
end data.
list.

* Gene Maguin's code with some small modificiations.

String #s(a36).
Vector y(15,f2.0).
Compute #s=qID_9109.
If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
compute #k = 1.
Loop #k = 1 to 15.
Compute #i=index(#s,',').
If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
if (char.len(#s) ge 4) #s=substr(#s,#i+1).
End loop.
recode Y1 to Y15 (sysmis=0).
LIST Y1 to Y15 qID_9109.

OUTPUT:

y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15 qID_9109
 
 1  1  0  0  0  0  0  0  0   0   0   0   0   0   0 1,2
 0  0  1  0  0  1  1  0  0   0   0   0   0   0   0 3,6,7
 0  1  0  0  0  0  0  0  0   0   0   0   0   0   0 2
 0  0  0  0  1  0  0  0  0   1   0   1   0   1   0 5,10,12,14
 1  1  1  1  1  1  1  1  1   1   1   1   1   1   1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
 
Number of cases read:  5    Number of cases listed:  5



Maguin, Eugene wrote
Here's a not so compact method. A key drawback is that when the loop exits after processing a case a warning is issued. I don't  know whether there is a way to rewrite it to preclude the warning since you don't know how many responses are concatenated unless you scoot through each record and count the commas. Gene Maguin

String #s(a36).
Vector y(15,f2.0).
Compute #s=qID_9109.
If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
Loop if (char.len(#s) ge 2).
Compute #i=index(#s,',').
If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
if (char.len(#s) ge 4) #s=substr(#s,#i+1).
End loop.


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Nancy Rusinak
Sent: Monday, April 22, 2013 9:54 AM
To: [hidden email]
Subject: parsing out one variable to many

I am collecting survey data.  Respondents are asked to categorize their comments into different buckets.  They can choose as many as they like or none at all.  If they choose the first category, the data puts a "1" in the field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a "1" put in that variable if the respondent chose that bucket; a variable called "communication" for category 2 and a "1" put in that variable if the respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

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

Re: parsing out one variable to many

John F Hall
In reply to this post by John F Hall

Nancy

 

I can see your problem, but other listers have suggested solutions to spread out the strings.  I’m copying this back to the list so others can see it.

 

How many cases do you have?  Are there only up to three responses for each respondent?  If so you need to spread the responses out on to three separate columns, which is what you’ve tried to do with your syntax.  String manipulation is not my forte, I’m afraid.  You can apply the same logic if there are more than three responses per case.

 

I’m always aware that time = money and I don’t have time to learn about strings.  Provided the data set is not too large, I would probably attempt something by hand such as copying the strings into two more columns and then editing them.  I would also try copying the column for Q.66 into Word to edit into something that data list can read as three variables (even if the 2nd and 3rd are blank).  

 

Here’s my rough and ready solution:

 

Copy your data into a txt file

 

0,1,14

2,6,7

10,11,15

3

0,4

8,10,12

 

 

Replace all single digits with leading 0 and commas by blanks (I did this by hand)

 

00 01 14

02 06 07

10 11 15

03

00 04

08 10 12

 

. . and save to nancy.txt on f:

 

SPSS syntax:

 

data list file 'f:\nancy.txt' rec 1

/ x1 1-2 x2 4-5 x3 7-8.

 

list /cases 6.

 

freq var x1 x2 x3.

 

mult resp groups q66 (x1 to x3 (0,15))

/freq q66.

 

 

 

DATASET ACTIVATE DataSet9.

data list file 'f:\nancy.txt' rec 1

/ x1 1-2 x2 4-5 x3 7-8.

 

Data List will read 1 records from f:\nancy.txt

 

Variable          Rec   Start     End  Format

x1                  1       1       2  F2.0

x2                  1       4       5  F2.0

x3                  1       7       8  F2.0

list /cases 6.

 

 

 

 

x1 x2 x3

 

0  1 14

2  6  7

10 11 15

3  .  .

0  4  .

8 10 12

 

 

Number of cases read:  6    Number of cases listed:  6

 

 

Statistics

 

x1

x2

x3

N

Valid

6

5

4

Missing

0

1

2

 

 

x1

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

0

2

33.3

33.3

33.3

2

1

16.7

16.7

50.0

3

1

16.7

16.7

66.7

8

1

16.7

16.7

83.3

10

1

16.7

16.7

100.0

Total

6

100.0

100.0

 

 

 

x2

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

1

1

16.7

20.0

20.0

4

1

16.7

20.0

40.0

6

1

16.7

20.0

60.0

10

1

16.7

20.0

80.0

11

1

16.7

20.0

100.0

Total

5

83.3

100.0

 

Missing

System

1

16.7

 

 

Total

6

100.0

 

 

 

 

x3

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

7

1

16.7

25.0

25.0

12

1

16.7

25.0

50.0

14

1

16.7

25.0

75.0

15

1

16.7

25.0

100.0

Total

4

66.7

100.0

 

Missing

System

2

33.3

 

 

Total

6

100.0

 

 

 

 

Case Summary

 

Cases

Valid

Missing

Total

N

Percent

N

Percent

N

Percent

q66a

6

100.0%

0

.0%

6

100.0%

a. Group

 

 

q66 Frequencies

 

Responses

Percent of Cases

N

Percent

q66a

0

2

13.3%

33.3%

1

1

6.7%

16.7%

2

1

6.7%

16.7%

3

1

6.7%

16.7%

4

1

6.7%

16.7%

6

1

6.7%

16.7%

7

1

6.7%

16.7%

8

1

6.7%

16.7%

10

2

13.3%

33.3%

11

1

6.7%

16.7%

12

1

6.7%

16.7%

14

1

6.7%

16.7%

15

1

6.7%

16.7%

Total

15

100.0%

250.0%

a. Group

 

Is this what you wanted? 

 

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

Start page:  www.surveyresearch.weebly.com/spss-without-tears.html

  

 

 

 

 

 

 

 

 

 

 

From: Nancy Rusinak [mailto:[hidden email]]
Sent: 23 April 2013 05:15
To: John F Hall
Subject: Re: parsing out one variable to many

 

Hello Mr. Hall,

 

Thank you for writing & attempting to assist me.

 

I'm afraid I have never worked with MULT RESPONSE syntax in the past so it's all Greek to me at this point.

 

I have worked a good bit with SUBSTR and am trying to create substrings using the "," as a delimiter. I'm almost there but, alas, not quite. Can you possibly help?

 

Here's a sample of my data -variable is called Q66. It's string, 10 wide:

 

0,1,14

2,6,7

10,11,15

3

0,4

8,10,12

 

I only want 3 variables when I finish. For example, for the first row of data, variable 1 would be "0," variable 2 would be "1," and variable 3 would be "14."

 

Here's my syntax, which I found online and have been trying to adapt for my purposes:

 

STRING #TEMP(A50).

COMPUTE #TEMP=Q66.

VECTOR VAR(3, A2).

LOOP #I=1 TO 3.

COMPUTE #INDEX=INDEX(#TEMP, ",").

COMPUTE VAR(#I)=SUBSTR(#TEMP,1, #INDEX-1).

COMPUTE #TEMP=SUBSTR(#TEMP, #INDEX+1).

END LOOP IF INDEX(#TEMP, ",")=0.

EXECUTE.

  

When I run this, the first two variables are correctly populated for the data strings that contain a "," but not for data strings that do not. Those are null. Also, the third variable is not populated for any of the data - just null down the column. SPSS output logs the following: "the third argument to SUBSTR (the length) is missing or otherwise invalid. The argument must be a non-negative integer. The result has been set to the null string." This indicates to me that the #INDEX-1 and #INDEX+1 are an issue but I do not know a workaround...

 

Any help you could offer would be greatly appreciated!

 

Best,

 

Nancy Rusinak, Ed.S.


Sent from my iPad


On Apr 22, 2013, at 10:38 AM, John F Hall <[hidden email]> wrote:

Nancy

 

IN SPSS, easiest would be to have 15 variables, one for each response category.  If you label each value (1st variable only) you can then use MULT RESPONSE to tabulate the responses. 

 

val lab v1 1 ‘Strategy’ 2 ‘ ~~~~’  etc etc

mult resp category (v1 to v15 (1,15))

/freq category.

 

Another way is to label each variable and use MULT RESPONSE in dichotomous mode, but to do this you first have to recode the values for all variables to 1.  I’ve played safe and kept your originals, but recoded to a new variable

 

recode v1 to v15 (1 thru 15 = 1)(else =0) into r1 to r15.

var lab r1 ‘~~~’

/r2 ‘~~~’

  ~~~

/r15 ‘~~~’.

mult resp rcategory (r1 to r15 (1))

/freq rcategory.

 

There are examples of this (illustrated by multiple response questions from the British Social Attitudes survey 1987 and the European Social Survey 2002) in sections 3.2 and 3.3 (pp 27-35) and the 3rd slide show of my 2006 Old Dog, Old Tricks presentation SPSS usage in major surveys.

(See: http://surveyresearch.weebly.com/old-dog-old-tricks-using-spss-syntax-to-beat-the-mouse-trap.html)

 

Get back to me if you don’t understand that or if you need further help.

 

 

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

Start page:  www.surveyresearch.weebly.com/spss-without-tears.html

  

 

 

 

 

 

-----Original Message-----
From: SPSSX(r) Discussion [[hidden email]] On Behalf Of Nancy Rusinak
Sent: 22 April 2013 15:54
To: [hidden email]
Subject: parsing out one variable to many

 

I am collecting survey data.  Respondents are asked to categorize their comments into different buckets.  They can choose as many as they like or none at all.  If they choose the first category, the data puts a "1" in the field.  If they choose the first and fifth category, the data reads "1,5."

There are 15 possible choices.

 

I'd like to create a variable called "strategy" for category 1 and have a "1" put in that variable if the respondent chose that bucket; a variable called "communication" for category 2 and a "1" put in that variable if the respondent chose that bucket and so on.

 

sample data, variable named qID_9109:

1,2

3,6,7

2

5,10,12,14

 

Can anyone help here?

 

=====================

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: parsing out one variable to many

John F Hall
In reply to this post by John F Hall

Nancy

 

Forgot to say there is a set of syntax-based MULT RESPONSE tutorials on:

http://surveyresearch.weebly.com/33-multiple-response-mult-response.html

 

I noticed there was a set of 4 responses in one of your earlier mails to SPSS-X: I’ll work out something in SPSS syntax to spread responses out on to 15 columns.

 

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

Start page:  www.surveyresearch.weebly.com/spss-without-tears.html

  

Reply | Threaded
Open this post in threaded view
|

Re: parsing out one variable to many

David Marso
Administrator
In reply to this post by Bruce Weaver
Or
END LOOP IF (condition).
condition: (#S=" ")?
(#i=0) followed by slamming rest of #s into last vector position.

Bruce Weaver wrote
Gene, I think the problem was that your condition for exiting the loop was never becoming true, so it was only exiting the loop when it the loop number exceeded MXLOOPS.  Given that we know 15 is the maximum number of loops (in this case, at least), if you LOOP just 15 times, it works with no warning messages.  E.g.,

data list list / qID_9109(a36).
begin data
"1,2"
"3,6,7"
"2"
"5,10,12,14"
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
end data.
list.

* Gene Maguin's code with some small modificiations.

String #s(a36).
Vector y(15,f2.0).
Compute #s=qID_9109.
If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
compute #k = 1.
Loop #k = 1 to 15.
Compute #i=index(#s,',').
If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
if (char.len(#s) ge 4) #s=substr(#s,#i+1).
End loop.
recode Y1 to Y15 (sysmis=0).
LIST Y1 to Y15 qID_9109.

OUTPUT:

y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15 qID_9109
 
 1  1  0  0  0  0  0  0  0   0   0   0   0   0   0 1,2
 0  0  1  0  0  1  1  0  0   0   0   0   0   0   0 3,6,7
 0  1  0  0  0  0  0  0  0   0   0   0   0   0   0 2
 0  0  0  0  1  0  0  0  0   1   0   1   0   1   0 5,10,12,14
 1  1  1  1  1  1  1  1  1   1   1   1   1   1   1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
 
Number of cases read:  5    Number of cases listed:  5



Maguin, Eugene wrote
Here's a not so compact method. A key drawback is that when the loop exits after processing a case a warning is issued. I don't  know whether there is a way to rewrite it to preclude the warning since you don't know how many responses are concatenated unless you scoot through each record and count the commas. Gene Maguin

String #s(a36).
Vector y(15,f2.0).
Compute #s=qID_9109.
If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
Loop if (char.len(#s) ge 2).
Compute #i=index(#s,',').
If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
if (char.len(#s) ge 4) #s=substr(#s,#i+1).
End loop.


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Nancy Rusinak
Sent: Monday, April 22, 2013 9:54 AM
To: [hidden email]
Subject: parsing out one variable to many

I am collecting survey data.  Respondents are asked to categorize their comments into different buckets.  They can choose as many as they like or none at all.  If they choose the first category, the data puts a "1" in the field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a "1" put in that variable if the respondent chose that bucket; a variable called "communication" for category 2 and a "1" put in that variable if the respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

=====================
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: parsing out one variable to many

David Marso
Administrator
In reply to this post by John F Hall
@John,
"time = money and I don’t have time to learn about strings".
Over the long haul you will save a lot of time=money by spending 20 minutes to learn about parsing strings.  I am surprised/shocked/appalled to read this from someone who claims to have been using SPSS since 1972?
--
John F Hall wrote
Nancy

I can see your problem, but other listers have suggested solutions to spread
out the strings.  I'm copying this back to the list so others can see it.

How many cases do you have?  Are there only up to three responses for each
respondent?  If so you need to spread the responses out on to three separate
columns, which is what you've tried to do with your syntax.  String
manipulation is not my forte, I'm afraid.  You can apply the same logic if
there are more than three responses per case.

I'm always aware that time = money and I don't have time to learn about
strings.  Provided the data set is not too large, I would probably attempt
something by hand such as copying the strings into two more columns and then
editing them.  I would also try copying the column for Q.66 into Word to
edit into something that data list can read as three variables (even if the
2nd and 3rd are blank).

Here's my rough and ready solution:

Copy your data into a txt file

0,1,14
2,6,7
10,11,15
3
0,4
8,10,12


Replace all single digits with leading 0 and commas by blanks (I did this by
hand)

00 01 14
02 06 07
10 11 15
03
00 04
08 10 12

. . and save to nancy.txt on f:

SPSS syntax:

data list file 'f:\nancy.txt' rec 1
/ x1 1-2 x2 4-5 x3 7-8.

list /cases 6.

freq var x1 x2 x3.

mult resp groups q66 (x1 to x3 (0,15))
/freq q66.



DATASET ACTIVATE DataSet9.
data list file 'f:\nancy.txt' rec 1
/ x1 1-2 x2 4-5 x3 7-8.

Data List will read 1 records from f:\nancy.txt

Variable          Rec   Start     End  Format
x1                  1       1       2  F2.0
x2                  1       4       5  F2.0
x3                  1       7       8  F2.0
list /cases 6.




x1 x2 x3

0  1 14
2  6  7
10 11 15
3  .  .
0  4  .
8 10 12


Number of cases read:  6    Number of cases listed:  6



Statistics


x1
x2
x3

N
Valid
6
5
4

Missing
0
1
2



x1


Frequency
Percent
Valid Percent
Cumulative Percent

Valid
0
2
33.3
33.3
33.3

2
1
16.7
16.7
50.0

3
1
16.7
16.7
66.7

8
1
16.7
16.7
83.3

10
1
16.7
16.7
100.0

Total
6
100.0
100.0




x2


Frequency
Percent
Valid Percent
Cumulative Percent

Valid
1
1
16.7
20.0
20.0

4
1
16.7
20.0
40.0

6
1
16.7
20.0
60.0

10
1
16.7
20.0
80.0

11
1
16.7
20.0
100.0

Total
5
83.3
100.0


Missing
System
1
16.7



Total
6
100.0





x3


Frequency
Percent
Valid Percent
Cumulative Percent

Valid
7
1
16.7
25.0
25.0

12
1
16.7
25.0
50.0

14
1
16.7
25.0
75.0

15
1
16.7
25.0
100.0

Total
4
66.7
100.0


Missing
System
2
33.3



Total
6
100.0





Case Summary


Cases

Valid
Missing
Total

N
Percent
N
Percent
N
Percent

q66a
6
100.0%
0
.0%
6
100.0%

a. Group



q66 Frequencies


Responses
Percent of Cases

N
Percent

q66a
0
2
13.3%
33.3%

1
1
6.7%
16.7%

2
1
6.7%
16.7%

3
1
6.7%
16.7%

4
1
6.7%
16.7%

6
1
6.7%
16.7%

7
1
6.7%
16.7%

8
1
6.7%
16.7%

10
2
13.3%
33.3%

11
1
6.7%
16.7%

12
1
6.7%
16.7%

14
1
6.7%
16.7%

15
1
6.7%
16.7%

Total
15
100.0%
250.0%

a. Group

Is this what you wanted?

John F Hall (Mr)
[Retired academic survey researcher]

Email:   [hidden email]
Website: www.surveyresearch.weebly.com
Start page:  www.surveyresearch.weebly.com/spss-without-tears.html











From: Nancy Rusinak [mailto:[hidden email]]
Sent: 23 April 2013 05:15
To: John F Hall
Subject: Re: parsing out one variable to many

Hello Mr. Hall,

Thank you for writing & attempting to assist me.

I'm afraid I have never worked with MULT RESPONSE syntax in the past so it's
all Greek to me at this point.

I have worked a good bit with SUBSTR and am trying to create substrings
using the "," as a delimiter. I'm almost there but, alas, not quite. Can you
possibly help?

Here's a sample of my data -variable is called Q66. It's string, 10 wide:

0,1,14
2,6,7
10,11,15
3
0,4
8,10,12

I only want 3 variables when I finish. For example, for the first row of
data, variable 1 would be "0," variable 2 would be "1," and variable 3 would
be "14."

Here's my syntax, which I found online and have been trying to adapt for my
purposes:

STRING #TEMP(A50).
COMPUTE #TEMP=Q66.
VECTOR VAR(3, A2).
LOOP #I=1 TO 3.
COMPUTE #INDEX=INDEX(#TEMP, ",").
COMPUTE VAR(#I)=SUBSTR(#TEMP,1, #INDEX-1).
COMPUTE #TEMP=SUBSTR(#TEMP, #INDEX+1).
END LOOP IF INDEX(#TEMP, ",")=0.
EXECUTE.

When I run this, the first two variables are correctly populated for the
data strings that contain a "," but not for data strings that do not. Those
are null. Also, the third variable is not populated for any of the data -
just null down the column. SPSS output logs the following: "the third
argument to SUBSTR (the length) is missing or otherwise invalid. The
argument must be a non-negative integer. The result has been set to the null
string." This indicates to me that the #INDEX-1 and #INDEX+1 are an issue
but I do not know a workaround...

Any help you could offer would be greatly appreciated!

Best,

Nancy Rusinak, Ed.S.

Sent from my iPad

On Apr 22, 2013, at 10:38 AM, John F Hall <[hidden email]> wrote:
Nancy

IN SPSS, easiest would be to have 15 variables, one for each response
category.  If you label each value (1st variable only) you can then use MULT
RESPONSE to tabulate the responses.

val lab v1 1 'Strategy' 2 ' ~~~~'  etc etc
mult resp category (v1 to v15 (1,15))
/freq category.

Another way is to label each variable and use MULT RESPONSE in dichotomous
mode, but to do this you first have to recode the values for all variables
to 1.  I've played safe and kept your originals, but recoded to a new
variable

recode v1 to v15 (1 thru 15 = 1)(else =0) into r1 to r15.
var lab r1 '~~~'
/r2 '~~~'
  ~~~
/r15 '~~~'.
mult resp rcategory (r1 to r15 (1))
/freq rcategory.

There are examples of this (illustrated by multiple response questions from
the British Social Attitudes survey 1987 and the European Social Survey
2002) in sections 3.2 and 3.3 (pp 27-35) and the 3rd slide show of my 2006
Old Dog, Old Tricks presentation SPSS usage in major surveys.
(See:
http://surveyresearch.weebly.com/old-dog-old-tricks-using-spss-syntax-to-bea
t-the-mouse-trap.html)

Get back to me if you don't understand that or if you need further help.


John F Hall (Mr)
[Retired academic survey researcher]

Email:   [hidden email]
Website: www.surveyresearch.weebly.com
Start page:  www.surveyresearch.weebly.com/spss-without-tears.html






-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Nancy Rusinak
Sent: 22 April 2013 15:54
To: [hidden email]
Subject: parsing out one variable to many

I am collecting survey data.  Respondents are asked to categorize their
comments into different buckets.  They can choose as many as they like or
none at all.  If they choose the first category, the data puts a "1" in the
field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a
"1" put in that variable if the respondent chose that bucket; a variable
called "communication" for category 2 and a "1" put in that variable if the
respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

=====================
To manage your subscription to SPSSX-L, send a message to
<mailto:[hidden email]> [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: parsing out one variable to many

David Marso
Administrator
In reply to this post by David Marso
Coup de Grace (Thread Killer: Aside from a vague feeling someone will toss some unnecessary python dependency into the mix).  Shorter and a bit more general and eye friendly .
--
DATA LIST /strX (A80).
BEGIN DATA
1,2,4,7
2,7,9,10,12
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
END DATA.

DEFINE strSPLIT
   (Orig !TOKENS(1) /New !TOKENS(1) /NumX !TOKENS(1)/ Delim !TOKENS(1) !DEFAULT (",")).            
STRING #cpyx(A80).
COMPUTE #cpyx=!Orig .
VECTOR !New (!NumX).
RECODE !CONCAT(!New,1," TO ", !New,!NumX, '(SYSMIS=0)').
FORMATS !CONCAT(!New,1," TO ", !New,!NumX, '(F1.0)').
LOOP.
+  COMPUTE #ind=INDEX(#cpyx,!QUOTE(!Delim)).
+  DO IF (#ind GT 0).
+    COMPUTE !New(NUMBER(RTRIM(SUBSTR(#cpyx,1,#ind-1)),F2))=1.
+    COMPUTE #cpyx=SUBSTR(#cpyx,#ind+1).
+  ELSE .
+    COMPUTE !New(NUMBER(RTRIM(#cpyx),F2))=1.
+  END IF.
END LOOP IF #ind=0.
!ENDDEFINE .

strSPlit Orig=strX New=X  NumX=15 .


LIST.
The variables are listed in the following order:

LINE   1: STRX

LINE   2: X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15

    STRX: 1,2,4,7
      X1: 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0

    STRX: 2,7,9,10,12
      X1: 0 1 0 0 0 0 1 0 1 1 0 1 0 0 0

    STRX: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
      X1: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


Number of cases read:  3    Number of cases listed:  3

** Actual syntax generated by Macro **.
Data List will read 1 records from the command file

Variable   Rec   Start     End         Format

STRX         1       1      80         A80

strSPlit Orig=strX New=X  NumX=15 .
1107 M>  
1108 M>  .
1109 M>  STRING #CPYX(A80).
1110 M>  COMPUTE #CPYX= strX.
1111 M>  VECTOR X ( 15 ).
1112 M>  RECODE X1 TO X15(SYSMIS=0).
1113 M>  FORMATS X1 TO X15(F1.0).
1114 M>  LOOP.
1115 M>  COMPUTE #IND=INDEX(#CPYX, ',' ).
1116 M>  DO IF (#IND GT 0).
1117 M>  COMPUTE X (NUMBER(RTRIM(SUBSTR(#CPYX,1,#IND-1)),F2))=1.
1118 M>  COMPUTE #CPYX=SUBSTR(#CPYX,#IND+1).
1119 M>  ELSE.
1120 M>  COMPUTE X (NUMBER(RTRIM(#CPYX),F2))=1.
1121 M>  END IF.
1122 M>  END LOOP IF #IND=0.
1123 M>   .

** Another version which merely spreads the values from left to right rather than dummy coding **.

DATA LIST /strX (A80).
BEGIN DATA
1,2,4,7
2,7,9,10,12
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
END DATA.

DEFINE StrParse ( Origstr !TOKENS(1)
                /     New !TOKENS(1)
                /     FMT !TOKENS(1) !DEFAULT (A2)
                / NumElem !TOKENS(1)
                /   Delim !TOKENS(1) !DEFAULT (",") ).
STRING #cpyx(A80).
COMPUTE #cpyx=!OrigStr .
VECTOR !New (!Numelem,!FMT).
COMPUTE #insert=1.
!LET !expr = 'SUBSTR(#cpyx,1,#ind-1)'.
!IF (!SUBSTR(FMT,1) !NE A) !THEN
!LET !Expr=!CONCAT('NUMBER(RTRIM(SUBSTR(#cpyx,1,#ind-1)),',!fmt,')').
!IFEND
LOOP.
+  COMPUTE #ind=INDEX(#cpyx,!QUOTE(!Delim)).
+  DO IF (#ind GT 0).
+    COMPUTE !New(#insert)=!Expr .
+    COMPUTE #cpyx=SUBSTR(#cpyx,#ind+1).
+    COMPUTE #insert=#insert+1.
+  ELSE .
!LET !Expr= '#cpyx'.
!IF (!SUBSTR(FMT,1) !NE A) !THEN
!LET !Expr=!CONCAT('NUMBER(RTRIM(#cpyx),',!fmt,')').
!IFEND
+    COMPUTE !New(#insert)=!Expr .
+  END IF.
END LOOP IF #ind=0.
!ENDDEFINE .
SET MPRINT ON PRINTBACK ON.
strParse Origstr=strX New=X FMT=N2 NumElem=15 .
LIST.

The variables are listed in the following order:

LINE   1: STRX

LINE   2: X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15

    STRX: 1,2,4,7
      X1: 01 02 04 07  .  .  .  .  .  .  .  .  .  .  .

    STRX: 2,7,9,10,12
      X1: 02 07 09 10 12  .  .  .  .  .  .  .  .  .  .

    STRX: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
      X1: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15


Number of cases read:  3    Number of cases listed:  3

David Marso wrote
Or
END LOOP IF (condition).
condition: (#S=" ")?
(#i=0) followed by slamming rest of #s into last vector position.

Bruce Weaver wrote
Gene, I think the problem was that your condition for exiting the loop was never becoming true, so it was only exiting the loop when it the loop number exceeded MXLOOPS.  Given that we know 15 is the maximum number of loops (in this case, at least), if you LOOP just 15 times, it works with no warning messages.  E.g.,

data list list / qID_9109(a36).
begin data
"1,2"
"3,6,7"
"2"
"5,10,12,14"
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
end data.
list.

* Gene Maguin's code with some small modificiations.

String #s(a36).
Vector y(15,f2.0).
Compute #s=qID_9109.
If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
compute #k = 1.
Loop #k = 1 to 15.
Compute #i=index(#s,',').
If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
if (char.len(#s) ge 4) #s=substr(#s,#i+1).
End loop.
recode Y1 to Y15 (sysmis=0).
LIST Y1 to Y15 qID_9109.

OUTPUT:

y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15 qID_9109
 
 1  1  0  0  0  0  0  0  0   0   0   0   0   0   0 1,2
 0  0  1  0  0  1  1  0  0   0   0   0   0   0   0 3,6,7
 0  1  0  0  0  0  0  0  0   0   0   0   0   0   0 2
 0  0  0  0  1  0  0  0  0   1   0   1   0   1   0 5,10,12,14
 1  1  1  1  1  1  1  1  1   1   1   1   1   1   1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
 
Number of cases read:  5    Number of cases listed:  5



Maguin, Eugene wrote
Here's a not so compact method. A key drawback is that when the loop exits after processing a case a warning is issued. I don't  know whether there is a way to rewrite it to preclude the warning since you don't know how many responses are concatenated unless you scoot through each record and count the commas. Gene Maguin

String #s(a36).
Vector y(15,f2.0).
Compute #s=qID_9109.
If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
Loop if (char.len(#s) ge 2).
Compute #i=index(#s,',').
If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
if (char.len(#s) ge 4) #s=substr(#s,#i+1).
End loop.


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Nancy Rusinak
Sent: Monday, April 22, 2013 9:54 AM
To: [hidden email]
Subject: parsing out one variable to many

I am collecting survey data.  Respondents are asked to categorize their comments into different buckets.  They can choose as many as they like or none at all.  If they choose the first category, the data puts a "1" in the field.  If they choose the first and fifth category, the data reads "1,5."
There are 15 possible choices.

I'd like to create a variable called "strategy" for category 1 and have a "1" put in that variable if the respondent chose that bucket; a variable called "communication" for category 2 and a "1" put in that variable if the respondent chose that bucket and so on.

sample data, variable named qID_9109:
1,2
3,6,7
2
5,10,12,14

Can anyone help here?

=====================
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: parsing out one variable to many

Jon K Peck
The Python solution I posted yesterday is much simpler, easier to understand, and easier to confirm correct.


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:        04/23/2013 07:43 AM
Subject:        Re: [SPSSX-L] parsing out one variable to many
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Coup de Grace (Thread Killer: Aside from a vague feeling someone will toss
some unnecessary python dependency into the mix).  Shorter and a bit more
general and eye friendly .
--
DATA LIST /strX (A80).
BEGIN DATA
1,2,4,7
2,7,9,10,12
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
END DATA.

DEFINE strSPLIT
  (Orig !TOKENS(1) /New !TOKENS(1) /NumX !TOKENS(1)/ Delim !TOKENS(1)
!DEFAULT (",")).
STRING #cpyx(A80).
COMPUTE #cpyx=!Orig .
VECTOR !New (!NumX).
RECODE !CONCAT(!New,1," TO ", !New,!NumX, '(SYSMIS=0)').
FORMATS !CONCAT(!New,1," TO ", !New,!NumX, '(F1.0)').
LOOP.
+  COMPUTE #ind=INDEX(#cpyx,!QUOTE(!Delim)).
+  DO IF (#ind GT 0).
+    COMPUTE !New(NUMBER(RTRIM(SUBSTR(#cpyx,1,#ind-1)),F2))=1.
+    COMPUTE #cpyx=SUBSTR(#cpyx,#ind+1).
+  ELSE .
+    COMPUTE !New(NUMBER(RTRIM(#cpyx),F2))=1.
+  END IF.
*END LOOP IF #ind=0.*
!ENDDEFINE .

strSPlit Orig=strX New=X  NumX=15 .


LIST.
The variables are listed in the following order:

LINE   1: STRX

LINE   2: X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15

   STRX: 1,2,4,7
     X1: 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0

   STRX: 2,7,9,10,12
     X1: 0 1 0 0 0 0 1 0 1 1 0 1 0 0 0

   STRX: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
     X1: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


Number of cases read:  3    Number of cases listed:  3

** Actual syntax generated by Macro **.
Data List will read 1 records from the command file

Variable   Rec   Start     End         Format

STRX         1       1      80         A80

strSPlit Orig=strX New=X  NumX=15 .
1107 M>
1108 M>  .
1109 M>  STRING #CPYX(A80).
1110 M>  COMPUTE #CPYX= strX.
1111 M>  VECTOR X ( 15 ).
1112 M>  RECODE X1 TO X15(SYSMIS=0).
1113 M>  FORMATS X1 TO X15(F1.0).
1114 M>  LOOP.
1115 M>  COMPUTE #IND=INDEX(#CPYX, ',' ).
1116 M>  DO IF (#IND GT 0).
1117 M>  COMPUTE X (NUMBER(RTRIM(SUBSTR(#CPYX,1,#IND-1)),F2))=1.
1118 M>  COMPUTE #CPYX=SUBSTR(#CPYX,#IND+1).
1119 M>  ELSE.
1120 M>  COMPUTE X (NUMBER(RTRIM(#CPYX),F2))=1.
1121 M>  END IF.
1122 M>  END LOOP IF #IND=0.
1123 M>   .

** Another version which merely spreads the values from left to right rather
than dummy coding **.

DATA LIST /strX (A80).
BEGIN DATA
1,2,4,7
2,7,9,10,12
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
END DATA.

DEFINE StrParse ( Origstr !TOKENS(1)
               /     New !TOKENS(1)
               /     FMT !TOKENS(1) !DEFAULT (A2)
               / NumElem !TOKENS(1)
               /   Delim !TOKENS(1) !DEFAULT (",") ).
STRING #cpyx(A80).
COMPUTE #cpyx=!OrigStr .
VECTOR !New (!Numelem,!FMT).
COMPUTE #insert=1.
!LET !expr = 'SUBSTR(#cpyx,1,#ind-1)'.
!IF (!SUBSTR(FMT,1) !NE A) !THEN
!LET !Expr=!CONCAT('NUMBER(RTRIM(SUBSTR(#cpyx,1,#ind-1)),',!fmt,')').
!IFEND
LOOP.
+  COMPUTE #ind=INDEX(#cpyx,!QUOTE(!Delim)).
+  DO IF (#ind GT 0).
+    COMPUTE !New(#insert)=!Expr .
+    COMPUTE #cpyx=SUBSTR(#cpyx,#ind+1).
+    COMPUTE #insert=#insert+1.
+  ELSE .
!LET !Expr= '#cpyx'.
!IF (!SUBSTR(FMT,1) !NE A) !THEN
!LET !Expr=!CONCAT('NUMBER(RTRIM(#cpyx),',!fmt,')').
!IFEND
+    COMPUTE !New(#insert)=!Expr .
+  END IF.
*END LOOP IF #ind=0.*
!ENDDEFINE .
SET MPRINT ON PRINTBACK ON.
strParse Origstr=strX New=X FMT=N2 NumElem=15 .
LIST.

The variables are listed in the following order:

LINE   1: STRX

LINE   2: X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15

   STRX: 1,2,4,7
     X1: 01 02 04 07  .  .  .  .  .  .  .  .  .  .  .

   STRX: 2,7,9,10,12
     X1: 02 07 09 10 12  .  .  .  .  .  .  .  .  .  .

   STRX: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
     X1: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15


Number of cases read:  3    Number of cases listed:  3


David Marso wrote
> Or
> END LOOP IF (condition).
> condition: (#S=" ")?
> (#i=0) followed by slamming rest of #s into last vector position.
> Bruce Weaver wrote
>> Gene, I
/
>> think
/
>>  the problem was that your condition for exiting the loop was never
>> becoming true, so it was only exiting the loop when it the loop number
>> exceeded MXLOOPS.  Given that we know 15 is the maximum number of loops
>> (in this case, at least), if you LOOP just 15 times, it works with no
>> warning messages.  E.g.,
>>
>> data list list / qID_9109(a36).
>> begin data
>> "1,2"
>> "3,6,7"
>> "2"
>> "5,10,12,14"
>> "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
>> end data.
>> list.
>>
>> * Gene Maguin's code with some small modificiations.
>>
>> String #s(a36).
>> Vector y(15,f2.0).
>> Compute #s=qID_9109.
>> If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
>> compute #k = 1.
*
>> Loop #k = 1 to 15.
*
>> Compute #i=index(#s,',').
>> If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
>> If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
>> if (char.len(#s) ge 4) #s=substr(#s,#i+1).
>> End loop.
*
>> recode Y1 to Y15 (sysmis=0).
*
>> LIST Y1 to Y15 qID_9109.
>>
>> OUTPUT:
>>
>> y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15 qID_9109
>>
>>  1  1  0  0  0  0  0  0  0   0   0   0   0   0   0 1,2
>>  0  0  1  0  0  1  1  0  0   0   0   0   0   0   0 3,6,7
>>  0  1  0  0  0  0  0  0  0   0   0   0   0   0   0 2
>>  0  0  0  0  1  0  0  0  0   1   0   1   0   1   0 5,10,12,14
>>  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1
>> 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
>>
>> Number of cases read:  5    Number of cases listed:  5
>>
>>
>> Maguin, Eugene wrote
>>> Here's a not so compact method. A key drawback is that when the loop
>>> exits after processing a case a warning is issued. I don't  know whether
>>> there is a way to rewrite it to preclude the warning since you don't
>>> know how many responses are concatenated unless you scoot through each
>>> record and count the commas. Gene Maguin
>>>
>>> String #s(a36).
>>> Vector y(15,f2.0).
>>> Compute #s=qID_9109.
>>> If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
>>> Loop if (char.len(#s) ge 2).
>>> Compute #i=index(#s,',').
>>> If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
>>> If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
>>> if (char.len(#s) ge 4) #s=substr(#s,#i+1).
>>> End loop.
>>>
>>>
>>> -----Original Message-----
>>> From: SPSSX(r) Discussion [mailto:

>>> SPSSX-L@.UGA

>>> ] On Behalf Of Nancy Rusinak
>>> Sent: Monday, April 22, 2013 9:54 AM
>>> To:

>>> SPSSX-L@.UGA

>>> Subject: parsing out one variable to many
>>>
>>> I am collecting survey data.  Respondents are asked to categorize their
>>> comments into different buckets.  They can choose as many as they like
>>> or none at all.  If they choose the first category, the data puts a "1"
>>> in the field.  If they choose the first and fifth category, the data
>>> reads "1,5."
>>> There are 15 possible choices.
>>>
>>> I'd like to create a variable called "strategy" for category 1 and have
>>> a "1" put in that variable if the respondent chose that bucket; a
>>> variable called "communication" for category 2 and a "1" put in that
>>> variable if the respondent chose that bucket and so on.
>>>
>>> sample data, variable named qID_9109:
>>> 1,2
>>> 3,6,7
>>> 2
>>> 5,10,12,14
>>>
>>> Can anyone help here?
>>>
>>> =====================
>>> 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
>>>
>>> =====================
>>> 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





-----
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?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/parsing-out-one-variable-to-many-tp5719621p5719658.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


Reply | Threaded
Open this post in threaded view
|

Re: parsing out one variable to many

David Marso
Administrator
I don't see your post.
I have confirmed my code it correct.  Feel free to try to break it!
--
Jon K Peck wrote
The Python solution I posted yesterday is much simpler, easier to
understand, and easier to confirm correct.


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:   04/23/2013 07:43 AM
Subject:        Re: [SPSSX-L] parsing out one variable to many
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



Coup de Grace (Thread Killer: Aside from a vague feeling someone will toss
some unnecessary python dependency into the mix).  Shorter and a bit more
general and eye friendly .
--
DATA LIST /strX (A80).
BEGIN DATA
1,2,4,7
2,7,9,10,12
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
END DATA.

DEFINE strSPLIT
   (Orig !TOKENS(1) /New !TOKENS(1) /NumX !TOKENS(1)/ Delim !TOKENS(1)
!DEFAULT (",")).
STRING #cpyx(A80).
COMPUTE #cpyx=!Orig .
VECTOR !New (!NumX).
RECODE !CONCAT(!New,1," TO ", !New,!NumX, '(SYSMIS=0)').
FORMATS !CONCAT(!New,1," TO ", !New,!NumX, '(F1.0)').
LOOP.
+  COMPUTE #ind=INDEX(#cpyx,!QUOTE(!Delim)).
+  DO IF (#ind GT 0).
+    COMPUTE !New(NUMBER(RTRIM(SUBSTR(#cpyx,1,#ind-1)),F2))=1.
+    COMPUTE #cpyx=SUBSTR(#cpyx,#ind+1).
+  ELSE .
+    COMPUTE !New(NUMBER(RTRIM(#cpyx),F2))=1.
+  END IF.
*END LOOP IF #ind=0.*
!ENDDEFINE .

strSPlit Orig=strX New=X  NumX=15 .


LIST.
The variables are listed in the following order:

LINE   1: STRX

LINE   2: X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15

    STRX: 1,2,4,7
      X1: 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0

    STRX: 2,7,9,10,12
      X1: 0 1 0 0 0 0 1 0 1 1 0 1 0 0 0

    STRX: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
      X1: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


Number of cases read:  3    Number of cases listed:  3

** Actual syntax generated by Macro **.
Data List will read 1 records from the command file

Variable   Rec   Start     End         Format

STRX         1       1      80         A80

strSPlit Orig=strX New=X  NumX=15 .
1107 M>
1108 M>  .
1109 M>  STRING #CPYX(A80).
1110 M>  COMPUTE #CPYX= strX.
1111 M>  VECTOR X ( 15 ).
1112 M>  RECODE X1 TO X15(SYSMIS=0).
1113 M>  FORMATS X1 TO X15(F1.0).
1114 M>  LOOP.
1115 M>  COMPUTE #IND=INDEX(#CPYX, ',' ).
1116 M>  DO IF (#IND GT 0).
1117 M>  COMPUTE X (NUMBER(RTRIM(SUBSTR(#CPYX,1,#IND-1)),F2))=1.
1118 M>  COMPUTE #CPYX=SUBSTR(#CPYX,#IND+1).
1119 M>  ELSE.
1120 M>  COMPUTE X (NUMBER(RTRIM(#CPYX),F2))=1.
1121 M>  END IF.
1122 M>  END LOOP IF #IND=0.
1123 M>   .

** Another version which merely spreads the values from left to right
rather
than dummy coding **.

DATA LIST /strX (A80).
BEGIN DATA
1,2,4,7
2,7,9,10,12
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
END DATA.

DEFINE StrParse ( Origstr !TOKENS(1)
                /     New !TOKENS(1)
                /     FMT !TOKENS(1) !DEFAULT (A2)
                / NumElem !TOKENS(1)
                /   Delim !TOKENS(1) !DEFAULT (",") ).
STRING #cpyx(A80).
COMPUTE #cpyx=!OrigStr .
VECTOR !New (!Numelem,!FMT).
COMPUTE #insert=1.
!LET !expr = 'SUBSTR(#cpyx,1,#ind-1)'.
!IF (!SUBSTR(FMT,1) !NE A) !THEN
!LET !Expr=!CONCAT('NUMBER(RTRIM(SUBSTR(#cpyx,1,#ind-1)),',!fmt,')').
!IFEND
LOOP.
+  COMPUTE #ind=INDEX(#cpyx,!QUOTE(!Delim)).
+  DO IF (#ind GT 0).
+    COMPUTE !New(#insert)=!Expr .
+    COMPUTE #cpyx=SUBSTR(#cpyx,#ind+1).
+    COMPUTE #insert=#insert+1.
+  ELSE .
!LET !Expr= '#cpyx'.
!IF (!SUBSTR(FMT,1) !NE A) !THEN
!LET !Expr=!CONCAT('NUMBER(RTRIM(#cpyx),',!fmt,')').
!IFEND
+    COMPUTE !New(#insert)=!Expr .
+  END IF.
*END LOOP IF #ind=0.*
!ENDDEFINE .
SET MPRINT ON PRINTBACK ON.
strParse Origstr=strX New=X FMT=N2 NumElem=15 .
LIST.

The variables are listed in the following order:

LINE   1: STRX

LINE   2: X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15

    STRX: 1,2,4,7
      X1: 01 02 04 07  .  .  .  .  .  .  .  .  .  .  .

    STRX: 2,7,9,10,12
      X1: 02 07 09 10 12  .  .  .  .  .  .  .  .  .  .

    STRX: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
      X1: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15


Number of cases read:  3    Number of cases listed:  3


David Marso wrote
> Or
> END LOOP IF (condition).
> condition: (#S=" ")?
> (#i=0) followed by slamming rest of #s into last vector position.
> Bruce Weaver wrote
>> Gene, I
/
>> think
/
>>  the problem was that your condition for exiting the loop was never
>> becoming true, so it was only exiting the loop when it the loop number
>> exceeded MXLOOPS.  Given that we know 15 is the maximum number of loops
>> (in this case, at least), if you LOOP just 15 times, it works with no
>> warning messages.  E.g.,
>>
>> data list list / qID_9109(a36).
>> begin data
>> "1,2"
>> "3,6,7"
>> "2"
>> "5,10,12,14"
>> "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
>> end data.
>> list.
>>
>> * Gene Maguin's code with some small modificiations.
>>
>> String #s(a36).
>> Vector y(15,f2.0).
>> Compute #s=qID_9109.
>> If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
>> compute #k = 1.
*
>> Loop #k = 1 to 15.
*
>> Compute #i=index(#s,',').
>> If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
>> If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
>> if (char.len(#s) ge 4) #s=substr(#s,#i+1).
>> End loop.
*
>> recode Y1 to Y15 (sysmis=0).
*
>> LIST Y1 to Y15 qID_9109.
>>
>> OUTPUT:
>>
>> y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15 qID_9109
>>
>>  1  1  0  0  0  0  0  0  0   0   0   0   0   0   0 1,2
>>  0  0  1  0  0  1  1  0  0   0   0   0   0   0   0 3,6,7
>>  0  1  0  0  0  0  0  0  0   0   0   0   0   0   0 2
>>  0  0  0  0  1  0  0  0  0   1   0   1   0   1   0 5,10,12,14
>>  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1
>> 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
>>
>> Number of cases read:  5    Number of cases listed:  5
>>
>>
>> Maguin, Eugene wrote
>>> Here's a not so compact method. A key drawback is that when the loop
>>> exits after processing a case a warning is issued. I don't  know
whether
>>> there is a way to rewrite it to preclude the warning since you don't
>>> know how many responses are concatenated unless you scoot through each
>>> record and count the commas. Gene Maguin
>>>
>>> String #s(a36).
>>> Vector y(15,f2.0).
>>> Compute #s=qID_9109.
>>> If (substr(#s,char.len(#s),1) ne ',') #s=concat(rtrim(qID_9109),',').
>>> Loop if (char.len(#s) ge 2).
>>> Compute #i=index(#s,',').
>>> If (#i eq 2) y(number(substr(#s,1,1),f1.0))=1.
>>> If (#i eq 3) y(number(substr(#s,1,2),f2.0))=1.
>>> if (char.len(#s) ge 4) #s=substr(#s,#i+1).
>>> End loop.
>>>
>>>
>>> -----Original Message-----
>>> From: SPSSX(r) Discussion [mailto:

>>> SPSSX-L@.UGA

>>> ] On Behalf Of Nancy Rusinak
>>> Sent: Monday, April 22, 2013 9:54 AM
>>> To:

>>> SPSSX-L@.UGA

>>> Subject: parsing out one variable to many
>>>
>>> I am collecting survey data.  Respondents are asked to categorize
their
>>> comments into different buckets.  They can choose as many as they like
>>> or none at all.  If they choose the first category, the data puts a
"1"
>>> in the field.  If they choose the first and fifth category, the data
>>> reads "1,5."
>>> There are 15 possible choices.
>>>
>>> I'd like to create a variable called "strategy" for category 1 and
have
>>> a "1" put in that variable if the respondent chose that bucket; a
>>> variable called "communication" for category 2 and a "1" put in that
>>> variable if the respondent chose that bucket and so on.
>>>
>>> sample data, variable named qID_9109:
>>> 1,2
>>> 3,6,7
>>> 2
>>> 5,10,12,14
>>>
>>> Can anyone help here?
>>>
>>> =====================
>>> 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
>>>
>>> =====================
>>> 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





-----
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?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/parsing-out-one-variable-to-many-tp5719621p5719658.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
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: parsing out one variable to many

John F Hall
In reply to this post by David Marso

David

 

In all the years I've been designing and analysing surveys I've never needed to parse a script.  I offered a solution to do the analysis in response to an off-list note from Nancy about MULT RESPONSE and copied my reply and her note to the list.  In any case there were plenty of suggestions offered on parsing the script.  The solution I offered took me about 20 minutes in which I modified Nancy's data, checked the FM, wrote and tested the syntax, then produced a specimen analysis.  Nancy has never used MULT RESPONSE, but I'm sure she'll need it in her analysis. 

 

Your solution creates a string of 1s and 0s over 15 variables which can be analysed using MULT RESPONSE in dichotomous mode.  My SPSS syntax deals with codes 1 - 15 in up to three variables, leaving the original values intact.  A further solution would be for your code to be modified to yield codes 1 - 15 each with its own variable. It depends whether one wants to use MULT RESPONSE in integer or dichotomous mode.  I've frequently used both (15 variable) conventions in the same survey on the same variables by recoding 1-15 to 1 or the series of 1s to 1 -15.

 

I've dealt with coding conventions like these over many years, and run many an SPSS job to convert between one convention and another, but in the days of 80-column cards the fewer columns used the cheaper the data-prep.

 

I'm busy writing tutorials, but will have a look at scripts etc when I have time.  However, if they're not covered in Gradpack, I probably won't bother as I'm concerned mainly with entry level materials to get people started on, and getting an appetite for, the world of survey research and survey analysis.

 

John

 

 

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

Start page:  www.surveyresearch.weebly.com/spss-without-tears.html

  

 

 

 

 

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of David Marso
Sent: 23 April 2013 13:30
To: [hidden email]
Subject: Re: parsing out one variable to many

 

@John,

"time = money and I don’t have time to learn about strings".

Over the long haul you will save a lot of time=money by spending 20 minutes to learn about parsing strings.  I am surprised/shocked/appalled to read this from someone who claims to have been using SPSS since 1972?

--

 

John F Hall wrote

> Nancy

> 

> I can see your problem, but other listers have suggested solutions to

> spread out the strings.  I'm copying this back to the list so others

> can see it.

> 

> How many cases do you have?  Are there only up to three responses for

> each respondent?  If so you need to spread the responses out on to

> three separate columns, which is what you've tried to do with your

> syntax.  String manipulation is not my forte, I'm afraid.  You can

> apply the same logic if there are more than three responses per case.

> 

> I'm always aware that time = money and I don't have time to learn

> about strings.  Provided the data set is not too large, I would

> probably attempt something by hand such as copying the strings into

> two more columns and then editing them.  I would also try copying the

> column for Q.66 into Word to edit into something that data list can

> read as three variables (even if the 2nd and 3rd are blank).

> 

> Here's my rough and ready solution:

> 

> Copy your data into a txt file

> 

> 0,1,14

> 2,6,7

> 10,11,15

> 3

> 0,4

> 8,10,12

> 

> 

> Replace all single digits with leading 0 and commas by blanks (I did

> this by

> hand)

> 

> 00 01 14

> 02 06 07

> 10 11 15

> 03

> 00 04

> 08 10 12

> 

> . . and save to nancy.txt on f:

> 

> SPSS syntax:

> 

> data list file 'f:\nancy.txt' rec 1

> / x1 1-2 x2 4-5 x3 7-8.

> 

> list /cases 6.

> 

> freq var x1 x2 x3.

> 

> mult resp groups q66 (x1 to x3 (0,15)) /freq q66.

> 

> From: Nancy Rusinak [mailto:

 

> nancy@

 

> ]

> Sent: 23 April 2013 05:15

> To: John F Hall

> Subject: Re: parsing out one variable to many

> 

> Hello Mr. Hall,

> 

> Thank you for writing & attempting to assist me.

> 

> I'm afraid I have never worked with MULT RESPONSE syntax in the past so

> it's

> all Greek to me at this point.

> 

> I have worked a good bit with SUBSTR and am trying to create substrings

> using the "," as a delimiter. I'm almost there but, alas, not quite. Can

> you

> possibly help?

> 

> Here's a sample of my data -variable is called Q66. It's string, 10 wide:

> 

> 0,1,14

> 2,6,7

> 10,11,15

> 3

> 0,4

> 8,10,12

> 

> I only want 3 variables when I finish. For example, for the first row of

> data, variable 1 would be "0," variable 2 would be "1," and variable 3

> would

> be "14."

> 

> Here's my syntax, which I found online and have been trying to adapt for

> my

> purposes:

> 

> STRING #TEMP(A50).

> COMPUTE #TEMP=Q66.

> VECTOR VAR(3, A2).

> LOOP #I=1 TO 3.

> COMPUTE #INDEX=INDEX(#TEMP, ",").

> COMPUTE VAR(#I)=SUBSTR(#TEMP,1, #INDEX-1).

> COMPUTE #TEMP=SUBSTR(#TEMP, #INDEX+1).

> END LOOP IF INDEX(#TEMP, ",")=0.

> EXECUTE.

> 

> When I run this, the first two variables are correctly populated for the

> data strings that contain a "," but not for data strings that do not.

> Those

> are null. Also, the third variable is not populated for any of the data -

> just null down the column. SPSS output logs the following: "the third

> argument to SUBSTR (the length) is missing or otherwise invalid. The

> argument must be a non-negative integer. The result has been set to the

> null

> string." This indicates to me that the #INDEX-1 and #INDEX+1 are an issue

> but I do not know a workaround...

> 

> Any help you could offer would be greatly appreciated!

> 

> Best,

> 

> Nancy Rusinak, Ed.S.

> 

> Sent from my iPad

> 

> On Apr 22, 2013, at 10:38 AM, John F Hall &lt;

 

> johnfhall@

 

> &gt; wrote:

> Nancy

> 

> IN SPSS, easiest would be to have 15 variables, one for each response

> category.  If you label each value (1st variable only) you can then use

> MULT

> RESPONSE to tabulate the responses.

> 

> val lab v1 1 'Strategy' 2 ' ~~~~'  etc etc

> mult resp category (v1 to v15 (1,15))

> /freq category.

> 

> Another way is to label each variable and use MULT RESPONSE in dichotomous

> mode, but to do this you first have to recode the values for all variables

> to 1.  I've played safe and kept your originals, but recoded to a new

> variable

> 

> recode v1 to v15 (1 thru 15 = 1)(else =0) into r1 to r15.

> var lab r1 '~~~'

> /r2 '~~~'

>   ~~~

> /r15 '~~~'.

> mult resp rcategory (r1 to r15 (1))

> /freq rcategory.

> 

> There are examples of this (illustrated by multiple response questions

> from

> the British Social Attitudes survey 1987 and the European Social Survey

> 2002) in sections 3.2 and 3.3 (pp 27-35) and the 3rd slide show of my 2006

> Old Dog, Old Tricks presentation SPSS usage in major surveys.

> (See:

> http://surveyresearch.weebly.com/old-dog-old-tricks-using-spss-syntax-to-bea

> t-the-mouse-trap.html)

> 

> Get back to me if you don't understand that or if you need further help.

> 

> 

> John F Hall (Mr)

> [Retired academic survey researcher]

> 

> Email:

 

> johnfhall@

 

> Website: www.surveyresearch.weebly.com

> Start page:  www.surveyresearch.weebly.com/spss-without-tears.html

> 

> 

> 

> 

> 

> 

> -----Original Message-----

> From: SPSSX(r) Discussion [mailto:

 

> [hidden email]

 

> ] On Behalf Of

> Nancy Rusinak

> Sent: 22 April 2013 15:54

> To:

 

> [hidden email]

 

> Subject: parsing out one variable to many

> 

> I am collecting survey data.  Respondents are asked to categorize their

> comments into different buckets.  They can choose as many as they like or

> none at all.  If they choose the first category, the data puts a "1" in

> the

> field.  If they choose the first and fifth category, the data reads "1,5."

> There are 15 possible choices.

> 

> I'd like to create a variable called "strategy" for category 1 and have a

> "1" put in that variable if the respondent chose that bucket; a variable

> called "communication" for category 2 and a "1" put in that variable if

> the

> respondent chose that bucket and so on.

> 

> sample data, variable named qID_9109:

> 1,2

> 3,6,7

> 2

> 5,10,12,14

> 

> Can anyone help here?

> 

> =====================

> To manage your subscription to SPSSX-L, send a message to

> &lt;mailto:

 

> [hidden email]

 

> &gt;

 

> [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?"

--

View this message in context: http://spssx-discussion.1045642.n5.nabble.com/parsing-out-one-variable-to-many-tp5719621p5719656.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