<?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 within a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927609#M365022</link>
    <description>&lt;P&gt;Why do you have multiple values in a single variable at all? Generally a poor idea.&lt;/P&gt;
&lt;P&gt;What are we supposed to compare to 'remove duplicate visits'? From which variable?&lt;/P&gt;
&lt;P&gt;What is the maximum number of "visits" that need to be considered? Are ALL the "visits" no more than 2 digits and always numeric? The number is critical as new variables when created really should have a defined length and can't tell what may be needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way to just sort such as string. I am picking on the PPCENSVSx variable. Basically read each value into a variable in an array, sort the array, then stack back together.&lt;/P&gt;
&lt;P&gt;There are other way if you tell us that ALL of the values separated by comma space will ALWAYS be the equivalent of an integer (not stated).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would drop the i, j and V array variables after verifying things are working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data sortstring;
   set dsn;
   array v (15) $ 2 ;
   do i= 1 to countw(ppcensvsx,',');
      v[i]= strip(scan(ppcensvsx,i,','));
   end;
   call sortc(of v[*]);
   length ppsort $ 48;
   do j= (dim(v) -i + 2) to dim(v);
      ppsort = catx(', ',ppsort,v[j]);
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is an approach that uses the "visit" number as an index and will only use the index once in building a string:&lt;/P&gt;
&lt;PRE&gt;data sortstring2;
   set dsn;
   array v (15)  ;
   do i= 1 to countw(ppcensvsx,',');
      v[input(strip(scan(ppcensvsx,i,',')),f4.)]=1;
   end;
   length ppsort $ 48;
   do j= 1 to dim(v);
      if v[j]=1 then ppsort = catx(', ',ppsort,put(j,z2.));
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/311553"&gt;@mariko5797&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want the list of visits (1) to remove any duplicate visits and (2) to list in order.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DSN;
 infile datalines dlm= '*'; 
 label 	ID= "Participant ID"
 	  	AVSLST= "All Visits Completed"
		PPCENSVSx= "Censored Visits (have)"
		PPCENSVS= "Censored Visits (want)"
		;
 length	AVSLST PPCENSVSx PPCENSVS $30.;
 input 	ID AVSLST $ PPCENSVSx $ PPCENSVS $;
 datalines;
	1	* 01, 02, 03, 04, 05, 06, 07, 08	* 06, 12, 10 				* 06, 10, 12
	2	* 01, 02, 03, 04, 05, 06, 07		* 04, 05, 06, 07, 04, 02	* 02, 04, 05, 06, 07
	3	* 01, 02, 03, 05, 06, 07, 08		* 07, 08, 03, 07, 04		* 03, 04, 07, 08
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 May 2024 21:58:54 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-05-08T21:58:54Z</dc:date>
    <item>
      <title>Sorting within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927592#M365018</link>
      <description>&lt;P&gt;I want the list of visits (1) to remove any duplicate visits and (2) to list in order.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DSN;
 infile datalines dlm= '*'; 
 label 	ID= "Participant ID"
 	  	AVSLST= "All Visits Completed"
		PPCENSVSx= "Censored Visits (have)"
		PPCENSVS= "Censored Visits (want)"
		;
 length	AVSLST PPCENSVSx PPCENSVS $30.;
 input 	ID AVSLST $ PPCENSVSx $ PPCENSVS $;
 datalines;
	1	* 01, 02, 03, 04, 05, 06, 07, 08	* 06, 12, 10 				* 06, 10, 12
	2	* 01, 02, 03, 04, 05, 06, 07		* 04, 05, 06, 07, 04, 02	* 02, 04, 05, 06, 07
	3	* 01, 02, 03, 05, 06, 07, 08		* 07, 08, 03, 07, 04		* 03, 04, 07, 08
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 May 2024 20:13:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927592#M365018</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2024-05-08T20:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927606#M365020</link>
      <description>&lt;P&gt;Transpose to a long layout, then sort with NODUPKEY.&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 21:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927606#M365020</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-05-08T21:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927609#M365022</link>
      <description>&lt;P&gt;Why do you have multiple values in a single variable at all? Generally a poor idea.&lt;/P&gt;
&lt;P&gt;What are we supposed to compare to 'remove duplicate visits'? From which variable?&lt;/P&gt;
&lt;P&gt;What is the maximum number of "visits" that need to be considered? Are ALL the "visits" no more than 2 digits and always numeric? The number is critical as new variables when created really should have a defined length and can't tell what may be needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way to just sort such as string. I am picking on the PPCENSVSx variable. Basically read each value into a variable in an array, sort the array, then stack back together.&lt;/P&gt;
&lt;P&gt;There are other way if you tell us that ALL of the values separated by comma space will ALWAYS be the equivalent of an integer (not stated).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would drop the i, j and V array variables after verifying things are working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data sortstring;
   set dsn;
   array v (15) $ 2 ;
   do i= 1 to countw(ppcensvsx,',');
      v[i]= strip(scan(ppcensvsx,i,','));
   end;
   call sortc(of v[*]);
   length ppsort $ 48;
   do j= (dim(v) -i + 2) to dim(v);
      ppsort = catx(', ',ppsort,v[j]);
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is an approach that uses the "visit" number as an index and will only use the index once in building a string:&lt;/P&gt;
&lt;PRE&gt;data sortstring2;
   set dsn;
   array v (15)  ;
   do i= 1 to countw(ppcensvsx,',');
      v[input(strip(scan(ppcensvsx,i,',')),f4.)]=1;
   end;
   length ppsort $ 48;
   do j= 1 to dim(v);
      if v[j]=1 then ppsort = catx(', ',ppsort,put(j,z2.));
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/311553"&gt;@mariko5797&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want the list of visits (1) to remove any duplicate visits and (2) to list in order.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DSN;
 infile datalines dlm= '*'; 
 label 	ID= "Participant ID"
 	  	AVSLST= "All Visits Completed"
		PPCENSVSx= "Censored Visits (have)"
		PPCENSVS= "Censored Visits (want)"
		;
 length	AVSLST PPCENSVSx PPCENSVS $30.;
 input 	ID AVSLST $ PPCENSVSx $ PPCENSVS $;
 datalines;
	1	* 01, 02, 03, 04, 05, 06, 07, 08	* 06, 12, 10 				* 06, 10, 12
	2	* 01, 02, 03, 04, 05, 06, 07		* 04, 05, 06, 07, 04, 02	* 02, 04, 05, 06, 07
	3	* 01, 02, 03, 05, 06, 07, 08		* 07, 08, 03, 07, 04		* 03, 04, 07, 08
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 21:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927609#M365022</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-08T21:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927614#M365026</link>
      <description>&lt;P&gt;Like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;suggested: Transform your data to a long structure which is much easier to work with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm= '*' truncover; 
  label
  ID        = "Participant ID"
  AVSLST    = "All Visits Completed"
  PPCENSVSx = "Censored Visits (have)"
  PPCENSVS  = "Censored Visits (want)"
  ;
  length AVSLST PPCENSVSx PPCENSVS $30.;
  input ID :$6. AVSLST $ PPCENSVSx $ PPCENSVS $;
  datalines;
1 * 01, 02, 03, 04, 05, 06, 07, 08 * 06, 12, 10             * 06, 10, 12
2 * 01, 02, 03, 04, 05, 06, 07     * 04, 05, 06, 07, 04, 02 * 02, 04, 05, 06, 07
3 * 01, 02, 03, 05, 06, 07, 08     * 07, 08, 03, 07, 04     * 03, 04, 07, 08
4
5 *01
6 * *01
;
run;

data long;
  set have;
  length visit_no 8 visit_completed_flg visit_censored_flg 3;
  length _vno $5 _visits $200;
  _visits=catx(',',AVSLST,PPCENSVSx);
  do i=1 to countc(_visits,',')+1;
    _vno=scan(_visits,i);
    visit_no=input(_vno,best32.);
    visit_completed_flg = (findw(AVSLST,strip(_vno))&amp;gt;0);
    visit_censored_flg  = (findw(PPCENSVSx,strip(_vno))&amp;gt;0);
    output;
  end;
  keep id visit_:;
run;

proc sort data=long out=want nodupkey;
  by id visit_no;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 23:03:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927614#M365026</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-08T23:03:44Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927623#M365029</link>
      <description>&lt;P&gt;I think there have been discussions about this, please take a look at:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Double-value-elimination-on-one-cell/m-p/919238" target="_blank" rel="noopener"&gt;Double value elimination on one cell&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 02:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927623#M365029</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2024-05-09T02:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927738#M365061</link>
      <description>&lt;P&gt;I ended up doing a variation of this. Since this is a subject-level dataset, it can't stay in long format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm= '*' truncover; 
  label
  ID        = "Participant ID"
  AVSLST    = "All Visits Completed"
  PPCENSVSx = "Censored Visits (have)"
  PPCENSVS  = "Censored Visits (want)"
  ;
  length AVSLST PPCENSVSx PPCENSVS $30.;
  input ID :$6. AVSLST $ PPCENSVSx $ PPCENSVS $;
  datalines;
	1 * 01, 02, 03, 04, 05, 06, 07, 08 * 06, 12, 10             * 06, 10, 12
	2 * 01, 02, 03, 04, 05, 06, 07     * 04, 05, 06, 07, 04, 02 * 02, 04, 05, 06, 07
	3 * 01, 02, 03, 05, 06, 07, 08     * 07, 08, 03, 07, 04     * 03, 04, 07, 08
	4
	5 *01
	6 * *01
	;
data long;
 set have;
 length VISNO $2.;
	 do i= 1 to countc(PPCENSVSx, ',')+1;
	 	VISNO= scan(PPCENSVSx, i);
		output;
	 end;
proc sort data= long out= long2 nodupkeys; by ID VISNO;
data want;
 length PPCENSVS $54.;
 	do until(last.ID);
		set long2;
		by ID;
			PPCENSVS= catx(', ', PPCENSVS, VISNO);
	end;
 keep ID PPCENSVS;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 May 2024 15:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-within-a-string/m-p/927738#M365061</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2024-05-09T15:48:28Z</dc:date>
    </item>
  </channel>
</rss>

