<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to parse a comma-delimited string of numbers from a variable value into binary variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673068#M202397</link>
    <description>&lt;P&gt;Do you have access to the software that collected the data? Several of the survey data collection programs I have used have options for different layouts of export data sets. A common one is for multiple response questions to have the output as indicators such as&lt;/P&gt;
&lt;P&gt;Q3_1 = 1 if the first response is chosen or Q3_2=0 if the second response choice is NOT chosen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first choice would be to go the collection software and see about such options.&lt;/P&gt;
&lt;P&gt;A second question for you in your "dummy&amp;nbsp; variables" is do you need to capture the ORDER that responses were chosen? That may also be available as an option from the collection software.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By all the responses, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; requested, we would need to have a list for every single variable what the exact choices might be. Your example for Q3 shows values: 1, 2, 3, 7, 8, 9, 12. Do values 4,5,6,10,11 possibly exist? How about 13,14, 15?? and possibly special values like 99 for "refused to answer"? Every single possible code that could appear for Q3 is needed to code properly. And for every single other question that has such a list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have worked with survey data for over 25 years and from the example you show I would &lt;STRONG&gt;never&lt;/STRONG&gt; assume that because you have 3 and 7 in the data that codes of 4,5, and 6 a valid responses. I have seen questions get reused over years with changing codes due to the responses of previous years and seen multiple response lists with as many as 60 such codes. So I would want every single value that is expected explicitly stated before recoding values. But also, as mentioned above, I would start with the software export options to save a lot of potential headaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jul 2020 09:26:57 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-07-29T09:26:57Z</dc:date>
    <item>
      <title>How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673000#M202352</link>
      <description>&lt;P&gt;I have dataset with a many survey responses and many variables. One variable a string variable named Q3 in which the values look like this:&lt;/P&gt;
&lt;P&gt;1,2&lt;/P&gt;
&lt;P&gt;1,3,12&lt;/P&gt;
&lt;P&gt;1,2,3,12,7,8,9&lt;/P&gt;
&lt;P&gt;2,3,9&lt;/P&gt;
&lt;P&gt;... (yes, the values are not entirely in order, though this is probably not a big deal)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to parse this into a set of binary dummy variables, one for each possible value in Q_3, say Q3_1, Q3_2, Q3_3, ... Q3_12. For example, the values of Q3_9 for the data above would be:&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've played with some do loops using the SCAN function, but I have not been able to make it work. Can't find a forum question or help doc that addresses quite this situation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 22:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673000#M202352</guid>
      <dc:creator>EricVanceMartin</dc:creator>
      <dc:date>2020-07-28T22:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673002#M202354</link>
      <description>&lt;P&gt;The programming is easier than it seems.&amp;nbsp; However, it is possible that your data never includes some possible answers and it would be up to you to supply a complete list.&amp;nbsp; Are you prepared to do that?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 23:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673002#M202354</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-07-28T23:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673014#M202366</link>
      <description>&lt;P&gt;Thanks for your reply. I'm not sure I understand what you mean. The term "value" here is a little ambiguous. It could mean the numbers between the commas, or it could mean any combination of these in a comma-delimited list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, the values are from from the back-end of a web-based survey instrument (which I did not design, or I would not be in this situation). I know what the possible values are. I have several survey items set up like this, each with a different number of possible responses.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 01:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673014#M202366</guid>
      <dc:creator>EricVanceMartin</dc:creator>
      <dc:date>2020-07-29T01:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673027#M202374</link>
      <description>&lt;P&gt;Just to clarify, you would need to know the number of possible survey responses for each question.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can we assume that the survey question answers are integers that run from 1 to as many as needed?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There will be an array statement for each question, along the lines of:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array q3_ {12};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This statement would create Q3_1 through Q3_12.&amp;nbsp; So it is necessary to know for each question how many variables must be created.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 02:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673027#M202374</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-07-29T02:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673028#M202375</link>
      <description>&lt;P&gt;Yes, I know the number and range of numbers that represent the possible responses for each item.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 03:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673028#M202375</guid>
      <dc:creator>EricVanceMartin</dc:creator>
      <dc:date>2020-07-29T03:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673029#M202376</link>
      <description>&lt;P&gt;Try the FINDW() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  array q3_ [12] ;
  do index=1 to dim(q3_);
    q3_[index]=0 ne findw(q3,cats(index),',','t');
  end;
  drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs   q3               q3_1   q3_2   q3_3   q3_4   q3_5   q3_6   q3_7   q3_8   q3_9   q3_10   q3_11   q3_12

 1    1,2                1      1      0      0      0      0      0      0      0      0       0       0
 2    1,3,12             1      0      1      0      0      0      0      0      0      0       0       1
 3    1,2,3,12,7,8,9     1      1      1      0      0      0      1      1      1      0       0       1
 4    2,3,9              0      1      1      0      0      0      0      0      1      0       0       0
 5                       0      0      0      0      0      0      0      0      0      0       0       0&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jul 2020 03:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673029#M202376</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-29T03:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673032#M202378</link>
      <description>&lt;P&gt;This looks exactly like it! I will try with my real data tomorrow! Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 03:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673032#M202378</guid>
      <dc:creator>EricVanceMartin</dc:creator>
      <dc:date>2020-07-29T03:29:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673068#M202397</link>
      <description>&lt;P&gt;Do you have access to the software that collected the data? Several of the survey data collection programs I have used have options for different layouts of export data sets. A common one is for multiple response questions to have the output as indicators such as&lt;/P&gt;
&lt;P&gt;Q3_1 = 1 if the first response is chosen or Q3_2=0 if the second response choice is NOT chosen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first choice would be to go the collection software and see about such options.&lt;/P&gt;
&lt;P&gt;A second question for you in your "dummy&amp;nbsp; variables" is do you need to capture the ORDER that responses were chosen? That may also be available as an option from the collection software.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By all the responses, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; requested, we would need to have a list for every single variable what the exact choices might be. Your example for Q3 shows values: 1, 2, 3, 7, 8, 9, 12. Do values 4,5,6,10,11 possibly exist? How about 13,14, 15?? and possibly special values like 99 for "refused to answer"? Every single possible code that could appear for Q3 is needed to code properly. And for every single other question that has such a list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have worked with survey data for over 25 years and from the example you show I would &lt;STRONG&gt;never&lt;/STRONG&gt; assume that because you have 3 and 7 in the data that codes of 4,5, and 6 a valid responses. I have seen questions get reused over years with changing codes due to the responses of previous years and seen multiple response lists with as many as 60 such codes. So I would want every single value that is expected explicitly stated before recoding values. But also, as mentioned above, I would start with the software export options to save a lot of potential headaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 09:26:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673068#M202397</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-29T09:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673209#M202470</link>
      <description>&lt;P&gt;This appears to have worked perfectly!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's a whole lotta trimmin' goin' on in there, but if I read the code right, the main action is findw. Can you tell me if I understand this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically it creates a variable name and assigns a value based on a logical evaluation of the statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;0 ne [position of index in the string]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...so if the index does not exist in the string, findw returns 0, 0 ne 0 evaluates false, and the value 0--for false--is assigned to the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...but if the index does exist, findw returns a number greater than 0, 0 ne [not 0] evaluates true, and the value 1--for true--is assigned to the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...? Thanks again!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 16:45:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673209#M202470</guid>
      <dc:creator>EricVanceMartin</dc:creator>
      <dc:date>2020-07-29T16:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673224#M202478</link>
      <description>&lt;P&gt;The trimming is so that trailing spaces stored in the original variable do not cause false negatives. You could also just include space as one of the delimiters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  array q3_ [12] ;
  do index=1 to dim(q3_);
    q3_[index]=0 ne indexw(q3,cats(index),' ,');
  end;
  drop index;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The ARRAY statement will create 12 variables named Q3_1 to Q3_12.&lt;/P&gt;
&lt;P&gt;The CATS() function will convert the numeric index into a string.&lt;/P&gt;
&lt;P&gt;The INDEXW() or FINDW() function will locate that string in the list.&lt;/P&gt;
&lt;P&gt;The comparison will check whether or not it was found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 17:11:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673224#M202478</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-29T17:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673246#M202487</link>
      <description>&lt;P&gt;Thanks for your thoughtful reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I said above, I know all the possible response values for every item. If an unnecessary variable were to be generated, I can easily delete or ignore it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're right that I could have played with the survey software (it's Qualtrics). I didn't think of that. I generally take any opportunity to learn new SAS skills. In any case, this instrument (which is reused periodically) is on the block for revisions soon, and a cleanup of these ridiculous multiple-response items is definitely part of that!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom's solution using findw works perfectly.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 18:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673246#M202487</guid>
      <dc:creator>EricVanceMartin</dc:creator>
      <dc:date>2020-07-29T18:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a comma-delimited string of numbers from a variable value into binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673262#M202498</link>
      <description>&lt;P&gt;Also, I see that you are right that it would obviously be better to feed a discrete and correct list of the values to a script. In this case, because we are going to revise the instrument, I hope I never have to use this code again! Also, there is a check on spurious variables, in that the codebook--which I have to make myself--will not contain those values, and I will see this when I apply labels and such. Thanks again for your thoughts!!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 18:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-a-comma-delimited-string-of-numbers-from-a-variable/m-p/673262#M202498</guid>
      <dc:creator>EricVanceMartin</dc:creator>
      <dc:date>2020-07-29T18:51:23Z</dc:date>
    </item>
  </channel>
</rss>

