SUPPORT / SAMPLES & SAS NOTES
 

Support

Sample 39036: Eliminating duplicate characters in a string

DetailsAboutRate It
/* for the purpose of this sample, we're not creating a data set */
   data _null_;
/* read data from DATALINES */
     input string $ 1-20;
/* examine the beginning value */
     put 'Before ' string=;
/* capture the number of characters in the variable */
     l1=length(string);
/* the loop below captures each character in turn and compares
   the remaining characters by position.  If a match is found,
   the matching value is replaced with a null character, '00'x.
   If the first value is a null character, the comparison loop
   is not executed. */
     do i = 1 to l1-1;
       t1=substr(string,i,1);
       if t1 ^= '00'x then do j = i+1 to l1;
       if t1=substr(string,j,1) then substr(string,j,1)='00'x;
       end;
     end;
/* the COMPRESS function is used to remove the nulls*/
     string=compress(string,'00'x);
/* this is to view the variable after alteration. */
     put 'After  ' string= /;
drop l1 t1 i j ;
datalines;
QWERTQWERT
AASSDDFFGG569
;
run;



These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.