<?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 Separate a variable containing a list into multiple variables in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737097#M28783</link>
    <description>&lt;P&gt;I have a data set that has a variable that lists a set of states (that are separated by a space). The number of states listed varies by observation. Below is a simplified example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;state_list&lt;/P&gt;
&lt;P&gt;obs1&amp;nbsp; &amp;nbsp; MA NE AK&amp;nbsp;&lt;/P&gt;
&lt;P&gt;obs2&amp;nbsp; &amp;nbsp; NE AL ME NJ&lt;/P&gt;
&lt;P&gt;obs3&amp;nbsp; &amp;nbsp; RI&lt;/P&gt;
&lt;P&gt;obs4&amp;nbsp; &amp;nbsp; NV CA UT OR&lt;/P&gt;
&lt;P&gt;obs5&amp;nbsp; &amp;nbsp; MA NV WA CO MN MI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to do is separate these out so that each state listed for an observation is under a separate variable (e.g., state1, state2, state3, etc), BUT I do not want to manually enter or count the maximum number of states listed within a given instance of the state_list variable. I don't know if the CALL SYMPUT routine can somehow be used? The actual data has many thousands of observations and we receive a new data set every year, so it really is not practical to manually go through and determine the length of the longest list in the state_list variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My resulting data set would look something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;st1&amp;nbsp; st2&amp;nbsp; st3&amp;nbsp; &amp;nbsp;st4&amp;nbsp; &amp;nbsp;st5&amp;nbsp; st6&lt;/P&gt;
&lt;P&gt;obs1&amp;nbsp; &amp;nbsp; MA&amp;nbsp; NE&amp;nbsp; AK&amp;nbsp;&lt;/P&gt;
&lt;P&gt;obs2&amp;nbsp; &amp;nbsp; NE&amp;nbsp; AL&amp;nbsp; ME&amp;nbsp; &amp;nbsp;NJ&lt;/P&gt;
&lt;P&gt;obs3&amp;nbsp; &amp;nbsp; RI&lt;/P&gt;
&lt;P&gt;obs4&amp;nbsp; &amp;nbsp; NV&amp;nbsp; CA&amp;nbsp; UT&amp;nbsp; &amp;nbsp;OR&lt;/P&gt;
&lt;P&gt;obs5&amp;nbsp; &amp;nbsp; MA&amp;nbsp; NV&amp;nbsp; WA&amp;nbsp; CO&amp;nbsp; MN&amp;nbsp; MI&lt;/P&gt;</description>
    <pubDate>Mon, 26 Apr 2021 19:49:34 GMT</pubDate>
    <dc:creator>raivester</dc:creator>
    <dc:date>2021-04-26T19:49:34Z</dc:date>
    <item>
      <title>Separate a variable containing a list into multiple variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737097#M28783</link>
      <description>&lt;P&gt;I have a data set that has a variable that lists a set of states (that are separated by a space). The number of states listed varies by observation. Below is a simplified example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;state_list&lt;/P&gt;
&lt;P&gt;obs1&amp;nbsp; &amp;nbsp; MA NE AK&amp;nbsp;&lt;/P&gt;
&lt;P&gt;obs2&amp;nbsp; &amp;nbsp; NE AL ME NJ&lt;/P&gt;
&lt;P&gt;obs3&amp;nbsp; &amp;nbsp; RI&lt;/P&gt;
&lt;P&gt;obs4&amp;nbsp; &amp;nbsp; NV CA UT OR&lt;/P&gt;
&lt;P&gt;obs5&amp;nbsp; &amp;nbsp; MA NV WA CO MN MI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to do is separate these out so that each state listed for an observation is under a separate variable (e.g., state1, state2, state3, etc), BUT I do not want to manually enter or count the maximum number of states listed within a given instance of the state_list variable. I don't know if the CALL SYMPUT routine can somehow be used? The actual data has many thousands of observations and we receive a new data set every year, so it really is not practical to manually go through and determine the length of the longest list in the state_list variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My resulting data set would look something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;st1&amp;nbsp; st2&amp;nbsp; st3&amp;nbsp; &amp;nbsp;st4&amp;nbsp; &amp;nbsp;st5&amp;nbsp; st6&lt;/P&gt;
&lt;P&gt;obs1&amp;nbsp; &amp;nbsp; MA&amp;nbsp; NE&amp;nbsp; AK&amp;nbsp;&lt;/P&gt;
&lt;P&gt;obs2&amp;nbsp; &amp;nbsp; NE&amp;nbsp; AL&amp;nbsp; ME&amp;nbsp; &amp;nbsp;NJ&lt;/P&gt;
&lt;P&gt;obs3&amp;nbsp; &amp;nbsp; RI&lt;/P&gt;
&lt;P&gt;obs4&amp;nbsp; &amp;nbsp; NV&amp;nbsp; CA&amp;nbsp; UT&amp;nbsp; &amp;nbsp;OR&lt;/P&gt;
&lt;P&gt;obs5&amp;nbsp; &amp;nbsp; MA&amp;nbsp; NV&amp;nbsp; WA&amp;nbsp; CO&amp;nbsp; MN&amp;nbsp; MI&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 19:49:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737097#M28783</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2021-04-26T19:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Separate a variable containing a list into multiple variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737100#M28784</link>
      <description>&lt;P&gt;Split each value out to it's own row and then use PROC TRANSPOSE.&lt;/P&gt;
&lt;P&gt;Untested demo:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long;
set have;

nWords = countw(state_list);

do i=1 to nWords;
state = scan(state_list, i);
output;
end;
run;

proc transpose data=long out=wide prefix = State;
by OBS;
var State;
ID i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Transposing data tutorials:&lt;BR /&gt;Long to Wide:&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/326526"&gt;@raivester&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a data set that has a variable that lists a set of states (that are separated by a space). The number of states listed varies by observation. Below is a simplified example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;state_list&lt;/P&gt;
&lt;P&gt;obs1&amp;nbsp; &amp;nbsp; MA NE AK&amp;nbsp;&lt;/P&gt;
&lt;P&gt;obs2&amp;nbsp; &amp;nbsp; NE AL ME NJ&lt;/P&gt;
&lt;P&gt;obs3&amp;nbsp; &amp;nbsp; RI&lt;/P&gt;
&lt;P&gt;obs4&amp;nbsp; &amp;nbsp; NV CA UT OR&lt;/P&gt;
&lt;P&gt;obs5&amp;nbsp; &amp;nbsp; MA NV WA CO MN MI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to do is separate these out so that each state listed for an observation is under a separate variable (e.g., state1, state2, state3, etc), BUT I do not want to manually enter or count the maximum number of states listed within a given instance of the state_list variable. I don't know if the CALL SYMPUT routine can somehow be used? The actual data has many thousands of observations and we receive a new data set every year, so it really is not practical to manually go through and determine the length of the longest list in the state_list variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My resulting data set would look something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;st1&amp;nbsp; st2&amp;nbsp; st3&amp;nbsp; &amp;nbsp;st4&amp;nbsp; &amp;nbsp;st5&amp;nbsp; st6&lt;/P&gt;
&lt;P&gt;obs1&amp;nbsp; &amp;nbsp; MA&amp;nbsp; NE&amp;nbsp; AK&amp;nbsp;&lt;/P&gt;
&lt;P&gt;obs2&amp;nbsp; &amp;nbsp; NE&amp;nbsp; AL&amp;nbsp; ME&amp;nbsp; &amp;nbsp;NJ&lt;/P&gt;
&lt;P&gt;obs3&amp;nbsp; &amp;nbsp; RI&lt;/P&gt;
&lt;P&gt;obs4&amp;nbsp; &amp;nbsp; NV&amp;nbsp; CA&amp;nbsp; UT&amp;nbsp; &amp;nbsp;OR&lt;/P&gt;
&lt;P&gt;obs5&amp;nbsp; &amp;nbsp; MA&amp;nbsp; NV&amp;nbsp; WA&amp;nbsp; CO&amp;nbsp; MN&amp;nbsp; MI&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 20:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737100#M28784</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-26T20:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Separate a variable containing a list into multiple variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737171#M28787</link>
      <description>&lt;P&gt;Use a DO loop with SCAN() and OUTPUT to convert to a tall format dataset.&lt;/P&gt;
&lt;P&gt;Here is a method that works even when some observations have an empty list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $ state_list $50. ;
cards;
obs1 MA NE AK
obs2 NE AL ME NJ
obs3 RI
obs4 NV CA UT OR
obs5 MA NV WA CO MN MI
obs6
;

data want ;
  set have;
  do state_no=1 by 1 until (state_no &amp;gt;= countw(state_list,' '));
    state=scan(state_list,state_no,' ');
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use that dataset for most things. You can even use PROC REPORT to print it in the wide style.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=want missing;
  column id state,state_no;
  define id / group;
  define state_no / across 'STATE';
  define state / group ' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 286px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58721iB2603EC0BCD920E8/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Or if you really want that wide report as a dataset use PROC TRANSPOSE&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=want out=wide(drop=_name_)  prefix=st ;
  by id;
  var state;
  id state_no;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 333px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58720i1EC043FE91F91968/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 06:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737171#M28787</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-27T06:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Separate a variable containing a list into multiple variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737246#M28796</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $ state_list $50. ;
cards;
obs1 MA NE AK
obs2 NE AL ME NJ
obs3 RI
obs4 NV CA UT OR
obs5 MA NV WA CO MN MI
obs6
;
proc sql;
select max(countw(state_list)) into : n
 from have;
 quit;
data want;
 set have;
 array st{&amp;amp;n} $ 40;
 do i=1 to countw(state_list);
   st{i}=scan(state_list,i);
 end;
drop i;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you have a BIG table.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 12:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Separate-a-variable-containing-a-list-into-multiple-variables/m-p/737246#M28796</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-27T12:46:32Z</dc:date>
    </item>
  </channel>
</rss>

