<?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: Sorting data within the cell in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970844#M377185</link>
    <description>Thank you very much for your code</description>
    <pubDate>Wed, 16 Jul 2025 06:49:21 GMT</pubDate>
    <dc:creator>SukumarBalusamy</dc:creator>
    <dc:date>2025-07-16T06:49:21Z</dc:date>
    <item>
      <title>Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970607#M377104</link>
      <description>&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Col1&lt;/TD&gt;&lt;TD&gt;Col2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1,5,4,3,2,6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;21,3,2,1,5,15,17,3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1,2,3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;4,3,2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;Dear Friends,&lt;/P&gt;&lt;P&gt;I have dataset like above, I would like to convert it like below. Can someone suggest me the easy way. Thanks in advance.&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Col1&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Col2&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1,2,3,4,5,6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1,2,3,3,5,15,17,21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1,2,3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2,3,4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 11 Jul 2025 14:13:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970607#M377104</guid>
      <dc:creator>SukumarBalusamy</dc:creator>
      <dc:date>2025-07-11T14:13:41Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970610#M377105</link>
      <description>&lt;P&gt;Normalize the data first.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall;
  set have;
  length index value 8;
  do index=1 to max(1,countw(col2,','));
     value=input(scan(col2,index,','),32.);
      output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1752245353575.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108308i8DEE4FB627E51904/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1752245353575.png" alt="Tom_0-1752245353575.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then normal sorting works&amp;nbsp; (add NODUPKEY option to PROC SORT statement to eliminate duplicates).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=tall ;
  by col1 value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If for some reason you need to recreate that comma delimited list run another data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.col1);
    set tall;
    by col1;
    length new_col2 $200;
    new_col2=catx(',',new_col2,value);
  end;
  drop index value ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1752245383997.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108309i5C08C9BF2FF578E3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1752245383997.png" alt="Tom_1-1752245383997.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jul 2025 14:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970610#M377105</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-11T14:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970611#M377106</link>
      <description>&lt;P&gt;You could try combing "infile trick" and missing=" " option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Col1 Col2 :$ 200.;
cards;
1	1,5,4,3,2,6
2	21,3,2,1,5,15,17,3
3	1,2,3
4	4,3,2
;
run;
proc print data = have;
run;


/* create a fake temp file */
filename f temp;
data _null_;
  file f;
  put;
run;
options missing=" "; /* for CATX() to replace missing dot (.) with a space (" ") */

%let WN=20; /* max number of possible values on a list */

data want;
  set have; 
  infile f truncover dlm=",";
  input @1 @;
  _infile_ = Col2;
  input @1 (m1-m&amp;amp;WN.) (:) @@;

  array mm m1-m&amp;amp;WN.;
  put mm[*]=;
  call sortn(of m:);

  Col2 = catx(",",of m:);

  keep Col1 Col2;
run;
filename f clear;
options missing=.;

proc print data = want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jul 2025 14:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970611#M377106</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-11T14:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970613#M377107</link>
      <description>&lt;P&gt;If you know the range of number you could use an array to collect and catx() regenerate the string. Make sure to set the MISSING option properly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input col1 col2 :$50. ;
cards;
1 1,5,4,3,2,6
2 21,3,2,1,5,15,17,3
3 1,2,3
4 4,3,2
5 1,2,junk,3
;

options missing=' ';
data want;
  set have;
  array x[100] ;
  do index=1 to countw(col2,',');
    value=input(scan(col2,index,','),??32.);
    if value in (1:100) then x[value]=value;
    else put 'WARNING: Invalid number at ' index= col1= col2=;
  end;
  col2=catx(',',of x:);
  drop index value x:;
run;
options missing='.';
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log&lt;/P&gt;
&lt;PRE&gt;11   options missing=' ';
12   data want;
13     set have;
14     array x[100] ;
15     do index=1 to countw(col2,',');
16       value=input(scan(col2,index,','),??32.);
17       if value in (1:100) then x[value]=value;
18       else put 'WARNING: Invalid number at ' index= col1= col2=;
19     end;
20     col2=catx(',',of x:);
21     drop index value x:;
22   run;

WARNING: Invalid number at index=3 col1=5 col2=1,2,junk,3
NOTE: There were 5 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 5 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


23   options missing='.';
&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="Tom_0-1752245003304.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108307iF3542B6218735DB5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1752245003304.png" alt="Tom_0-1752245003304.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jul 2025 14:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970613#M377107</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-11T14:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970621#M377108</link>
      <description>&lt;P&gt;Or use the "RESOLVE trick" (just for fun):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_ i);
set have;
_=col2;
col2=' ';
do i=1 to countw(_,',');
  col2=catx(',',col2,input(resolve(cats('%sysfunc(ordinal(',i,',',_,'))')),32.));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Jul 2025 15:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970621#M377108</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-07-11T15:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970637#M377117</link>
      <description>&lt;P&gt;You can transfer the components of the cell into a hash object with the "ordered" attribute.&amp;nbsp; Then iterate through them and concatenate each of them:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input col1 col2 :$50. ;
cards;
1 1,5,4,3,2,6,3.5
2 21,3,2,1,5,15,17,3
3 1,2,3
4 4,3,2
5 1,2,junk,3
;

data want (drop=i _:);
  set have;
  if _n_=1 then do;
    declare hash h (ordered:'A',multidata:'Y');
      h.definekey('_x');
      h.definedata('_x');
      h.definedone();
    declare hiter hi ('h');
  end;

  do i=1 to countw(col2,',');
    _x=input(scan(col2,i,','),best12.);
    if _x^=. then h.add();
  end;
  length new_col $50;
  do while (hi.next()=0);
    new_col=catx(',',new_col,_x);
  end;
  h.clear();
run;
  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 Jul 2025 02:42:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970637#M377117</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-07-12T02:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970843#M377184</link>
      <description>Thank you so much for code this worked, I like this solution very much.</description>
      <pubDate>Wed, 16 Jul 2025 06:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970843#M377184</guid>
      <dc:creator>SukumarBalusamy</dc:creator>
      <dc:date>2025-07-16T06:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970844#M377185</link>
      <description>Thank you very much for your code</description>
      <pubDate>Wed, 16 Jul 2025 06:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970844#M377185</guid>
      <dc:creator>SukumarBalusamy</dc:creator>
      <dc:date>2025-07-16T06:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970845#M377186</link>
      <description>Thank you very much for the code</description>
      <pubDate>Wed, 16 Jul 2025 06:49:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970845#M377186</guid>
      <dc:creator>SukumarBalusamy</dc:creator>
      <dc:date>2025-07-16T06:49:49Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting data within the cell</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970846#M377187</link>
      <description>Thank you very much for generalized code</description>
      <pubDate>Wed, 16 Jul 2025 06:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-data-within-the-cell/m-p/970846#M377187</guid>
      <dc:creator>SukumarBalusamy</dc:creator>
      <dc:date>2025-07-16T06:50:48Z</dc:date>
    </item>
  </channel>
</rss>

