<?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 How do I remove duplicate entries in a string leaving only unique entries in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-remove-duplicate-entries-in-a-string-leaving-only/m-p/666995#M22998</link>
    <description>&lt;P&gt;I have a data set (.cvs), that I use on a daily basis (includes about 60-75 variables).&amp;nbsp; The source of this data set has gone through some upgrades, and now automatically pulls data into the system.&amp;nbsp; It will not override data, so the system just adds a new iteration (to those questions that allow it) even if it is the exact same data that is already in there. So for each person (row) some of my variables have a string of data separated by commas. I have attached an example excel spreadsheet, showing the data I have and what I want (I work with health data, so this is an example of what my data set looks like). Now, if the unique variables cannot be separated into their own columns, I am okay with having them in the same column.&lt;/P&gt;&lt;P&gt;My SAS skills (SAS 9.4) are fairly minimal (proc freqs, some simple SQL, merging, concatenation, sorting, pulling first obs of a string, etc.)&lt;/P&gt;&lt;P&gt;I have searched on line and tried the following:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;Want_date=scan(Have_Date, 1, ' ');&lt;BR /&gt;do i=2 to countw(Have_Date,' ');&lt;BR /&gt;word=scan(Have_Date, i, ' ');&lt;BR /&gt;found=find(Want_date, word, 'it');&lt;BR /&gt;if found=0 then Want_date=catx(' ', Want_date, word);&lt;BR /&gt;end;&lt;BR /&gt;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This did not work.&amp;nbsp; But to be honest I wasn't sure what this was exactly doing, so I may not have done it right.&lt;/P&gt;&lt;P&gt;When that didn't work I also found this code online and tried it:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;data want;&lt;BR /&gt;if not prxId then prxId + prxParse("s/\b(\w{2,})\b(.*)\b(\1\s*)\b/\1\2/io");&lt;BR /&gt;set have;&lt;BR /&gt;Want_date= Have_Date;&lt;BR /&gt;do i = 1 to 100 until (times=0);&lt;BR /&gt;call prxChange(prxId,1,Want_date,Want_date,len,trunc,times);&lt;BR /&gt;end;&lt;BR /&gt;drop i prxId len trunc times;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This was close, I ended up with a single date but it was followed by all the commas and the slash marks from the other dates in the field. (example:&amp;nbsp;05/30/2019,//,//,//,//,//,//,//,//).&amp;nbsp; Again, it could be because I am not sure what the code is all doing, so I wasn't able to correct it to remove the commas and slash marks.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just sent this one example but I have multiple columns that all have this same problem.&amp;nbsp; So any help would be appreciated.&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 05 Jul 2020 04:51:03 GMT</pubDate>
    <dc:creator>Tracy_Bis</dc:creator>
    <dc:date>2020-07-05T04:51:03Z</dc:date>
    <item>
      <title>How do I remove duplicate entries in a string leaving only unique entries</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-remove-duplicate-entries-in-a-string-leaving-only/m-p/666995#M22998</link>
      <description>&lt;P&gt;I have a data set (.cvs), that I use on a daily basis (includes about 60-75 variables).&amp;nbsp; The source of this data set has gone through some upgrades, and now automatically pulls data into the system.&amp;nbsp; It will not override data, so the system just adds a new iteration (to those questions that allow it) even if it is the exact same data that is already in there. So for each person (row) some of my variables have a string of data separated by commas. I have attached an example excel spreadsheet, showing the data I have and what I want (I work with health data, so this is an example of what my data set looks like). Now, if the unique variables cannot be separated into their own columns, I am okay with having them in the same column.&lt;/P&gt;&lt;P&gt;My SAS skills (SAS 9.4) are fairly minimal (proc freqs, some simple SQL, merging, concatenation, sorting, pulling first obs of a string, etc.)&lt;/P&gt;&lt;P&gt;I have searched on line and tried the following:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;Want_date=scan(Have_Date, 1, ' ');&lt;BR /&gt;do i=2 to countw(Have_Date,' ');&lt;BR /&gt;word=scan(Have_Date, i, ' ');&lt;BR /&gt;found=find(Want_date, word, 'it');&lt;BR /&gt;if found=0 then Want_date=catx(' ', Want_date, word);&lt;BR /&gt;end;&lt;BR /&gt;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This did not work.&amp;nbsp; But to be honest I wasn't sure what this was exactly doing, so I may not have done it right.&lt;/P&gt;&lt;P&gt;When that didn't work I also found this code online and tried it:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;data want;&lt;BR /&gt;if not prxId then prxId + prxParse("s/\b(\w{2,})\b(.*)\b(\1\s*)\b/\1\2/io");&lt;BR /&gt;set have;&lt;BR /&gt;Want_date= Have_Date;&lt;BR /&gt;do i = 1 to 100 until (times=0);&lt;BR /&gt;call prxChange(prxId,1,Want_date,Want_date,len,trunc,times);&lt;BR /&gt;end;&lt;BR /&gt;drop i prxId len trunc times;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This was close, I ended up with a single date but it was followed by all the commas and the slash marks from the other dates in the field. (example:&amp;nbsp;05/30/2019,//,//,//,//,//,//,//,//).&amp;nbsp; Again, it could be because I am not sure what the code is all doing, so I wasn't able to correct it to remove the commas and slash marks.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just sent this one example but I have multiple columns that all have this same problem.&amp;nbsp; So any help would be appreciated.&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 04:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-remove-duplicate-entries-in-a-string-leaving-only/m-p/666995#M22998</guid>
      <dc:creator>Tracy_Bis</dc:creator>
      <dc:date>2020-07-05T04:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I remove duplicate entries in a string leaving only unique entries</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-remove-duplicate-entries-in-a-string-leaving-only/m-p/666997#M22999</link>
      <description>&lt;P&gt;So in your input are mainly 3 variables (I omit prefix HAVE_ ) :&lt;/P&gt;
&lt;P&gt;&amp;nbsp; person_id, date and store.&lt;/P&gt;
&lt;P&gt;Try next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp(keep=person_ID datex store);
 set have(rename=(have_person_id = person_ID));
       i=1;
      /* assuming number of date = number of stores in strings */
      do until (missing(date));
           datex = scan(have_date,i,',');
           store = scan(have_store,i,',');
           output;
           i+1;
      end;
run;
proc sort data=temp out=temp1 nodupkey;
  by person_id datex;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use the result dataset as is for any analysis you need, without replacing the&amp;nbsp; long format to wide format, as shown in wanted output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you insist to have the wide format use PROC TRANSPOSE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 05:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-remove-duplicate-entries-in-a-string-leaving-only/m-p/666997#M22999</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-05T05:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I remove duplicate entries in a string leaving only unique entries</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-remove-duplicate-entries-in-a-string-leaving-only/m-p/667003#M23003</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/336460"&gt;@Tracy_Bis&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an attempt to achieve this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines dlm="09"x;
	input Have_PersonID	$ Have_Date:$200. Have_Store:$200.;
	datalines;
123	04/12/2019,04/12/2019,04/12/2019,04/12/2019,04/12/2019,04/12/2019,04/12/2019,04/12/2019	Tracy's Store,Tracy's Store,Tracy's Store,Tracy's Store,Tracy's Store,Tracy's Store,Tracy's Store,Tracy's Store
457	07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019,07/05/2019	,,,,,,,,,,,,,,
789	03/19/2019,03/19/2019,03/19/2019,03/19/2019,03/19/2019,03/19/2019,03/19/2019,03/19/2019,03/19/2019	,,,,,,,,,
101	05/18/2019,05/18/2019	Mickeys,Mickeys
234	08/02/2019,08/02/2019,08/02/2019,08/02/2019,08/02/2019,08/02/2019,08/02/2019,08/02/2019,08/02/2019,08/02/2019	Donalds, ND,Donalds, ND,Donalds, ND,Donalds, ND,Donalds, ND,Donalds, ND,Donalds, ND,Donalds, ND,Donalds, ND,Donalds, ND
557	05/19/2019,05/19/2019,05/19/2019,05/19/2019,05/30/2019,05/30/2019,05/30/2019	Daisy's, TX,Daisy's, TX,Daisy's, TX,Daisy's, TX,Pluto, MT,Pluto, MT,Pluto, MT
891	06/21/2019,06/21/2019,06/21/2019,07/10/2019	Hewy, Dewy, And Lewy,Hewy, Dewy, And Lewy,Hewy, Dewy, And Lewy,Goofy, Daffy, And Minnie
;
run;

%macro _mymac(param);
	data _have_&amp;amp;param. (keep= Have_PersonID have_&amp;amp;param._i i);
		set have;
		do i=1 to countw(Have_&amp;amp;param.);
			have_&amp;amp;param._i = scan(Have_&amp;amp;param.,i,",");
			if not missing(have_&amp;amp;param._i) then output _have_&amp;amp;param.;
		end;
	run;
	
	proc sort data=_have_&amp;amp;param. nodupkey;
		by Have_PersonID have_&amp;amp;param._i;
	run;
	
	proc sort data=_have_&amp;amp;param.;
		by Have_PersonID i;
	run;
	proc transpose data=_have_&amp;amp;param. out=_have_&amp;amp;param._tr (drop=_:);
		var have_&amp;amp;param._i;
		by Have_PersonID;
	run;
	
	data _have_&amp;amp;param._final;
		set _have_&amp;amp;param._tr;
		length have_&amp;amp;param._i $ 200;
		want_&amp;amp;param. = catx(", ", of col:);
		keep Have_PersonID want_&amp;amp;param.;
	run;
%mend;

%_mymac(date)
%_mymac(store)

data want;
	merge _have_date_final _have_store_final;
	by Have_PersonID;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Capture d’écran 2020-07-05 à 10.08.39.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46905iF08692FC94C4EB3E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-07-05 à 10.08.39.png" alt="Capture d’écran 2020-07-05 à 10.08.39.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best,&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 08:09:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-remove-duplicate-entries-in-a-string-leaving-only/m-p/667003#M23003</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-07-05T08:09:38Z</dc:date>
    </item>
  </channel>
</rss>

