<?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: how to count and output specific number of strings in a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/433101#M107348</link>
    <description>&lt;P&gt;I love this question absolutely.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input ID 1. DNA $30.;
cards;
1 O-O-O-O-O-O-O-O-O-O-O-O-O-O-O	
2 M-M-M-M-M-M-O-I-I-I-I-O-O-M-M	
3 M-M-M-M-O	                
4 O-I-O-I-M-O-O-O-I-I	        
5 O	 
; run;
data temp;
 set have;
 do i=1 to countw(dna,'-');
  value=scan(dna,i,'-');output;
 end;
 drop i dna;
run;
proc summary data=temp ;
by id value notsorted;
output out=temp1;
run;
data want;
length want $ 200;
 do until(last.id);
  set temp1;
  by id;
  if _freq_=1 then want=catx('-',want,value);
   else want=catx('-',want,cats(_freq_,value));
 end;
 drop _type_ _freq_ value;
run;
proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 01 Feb 2018 13:44:21 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-02-01T13:44:21Z</dc:date>
    <item>
      <title>how to count and output specific number of strings in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432793#M107230</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable in a SAS dataset that is a string of characters such as O-O-O-M-M-O-I-O,&amp;nbsp;what I would like to do is&amp;nbsp;try to shorten the value of the variable by adding a number that indicates how many times a character is repeated in a row. So for the above example I would like to see it be converted to 3O-2M-O-I-O. &amp;nbsp;Suggestions on how to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is a code that you can use:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input ID 1. DNA $30.;
datalines;
1	O-O-O-O-O-O-O-O-O-O-O-O-O-O-O	
2	M-M-M-M-M-M-O-I-I-I-I-O-O-M-M	
3	M-M-M-M-O	                
4	O-I-O-I-M-O-O-O-I-I	        
5	O	 
; run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would like for the above is to get the "want DNA" column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;HAVE DNA&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;WANT DNA&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;O-O-O-O-O-O-O-O-O-O-O-O-O-O-O&lt;/TD&gt;&lt;TD&gt;15O&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;M-M-M-M-M-M-O-I-I-I-I-O-O-M-M&lt;/TD&gt;&lt;TD&gt;6M-O-4I-2O-2M&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;M-M-M-M-O&lt;/TD&gt;&lt;TD&gt;4M-O&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;O-I-O-I-M-O-O-O-I-I&lt;/TD&gt;&lt;TD&gt;O-I-O-I-M-3O-2I&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;O&lt;/TD&gt;&lt;TD&gt;O&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:36:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432793#M107230</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2018-01-31T15:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to count and output specific number of strings in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432808#M107236</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length out_strand $30;
  length cur_strand cur_base $1;

  do _i = 1 to countw(dna);  
    cur_strand = scan(dna,_i,'-');  *identify the current base we are looking at;

	  do strand_count = 0 by 1 until (cur_strand ne cur_base);  *iterate over the scans to find the next nonmatch;
	    cur_base = scan(dna,strand_count+_i,'-');
      end;

      *compose the output string, checking to see if we need to append the number if &amp;gt;1 or not if =1;
	  out_strand = catx('-',out_strand,cats(ifc(strand_count&amp;gt;1,strand_count,''),cur_strand));

      _i = strand_count+_i-1;  *have to decrement one, since we go one past the match;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should work for what you need; basically you scan over the string and keep scanning until you reach a non-match.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 16:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432808#M107236</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2018-01-31T16:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to count and output specific number of strings in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432823#M107241</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data finale(drop= i j k count);
set have;
length want_dna $200;											*This needs to be set to a maximum lenghth;
do i=1 to n;
	count=0;
	key=substr(DNA,i,1);
	do j=i to n;
		if i=j and i ne 1 then do;
			do k=1 to k=i-1;
				if key=substr(DNA,k,1) then goto skip;
			end;
		end;
	if key=substr(DNA,j,1) then count=+1;
	end;

	if i=1 then do;
		if count gt 1 then 	WANT_DNA=key||'-'||put(count,best12.);
		else WANT_DNA=key;
	end;
	else do;
		if count gt 1 then 	WANT_DNA=WANT_DNA||'-'||key||'-'||put(count,best12.);
		else WANT_DNA=WANT_DNA||'-'||key;
	end;
	skip:
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have not tested this code, to be updated.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 16:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432823#M107241</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2018-01-31T16:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to count and output specific number of strings in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432841#M107245</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138619"&gt;@Satish_Parida&lt;/a&gt;! I did get an error:&lt;/P&gt;&lt;P&gt;Variable n is uninitialized.&lt;/P&gt;&lt;P&gt;ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or&lt;/P&gt;&lt;P&gt;the BY expression is missing, zero, or invalid.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the code by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46466"&gt;@snoopy369&lt;/a&gt;&amp;nbsp;worked!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you to you both!!&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 17:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/432841#M107245</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2018-01-31T17:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to count and output specific number of strings in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/433101#M107348</link>
      <description>&lt;P&gt;I love this question absolutely.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input ID 1. DNA $30.;
cards;
1 O-O-O-O-O-O-O-O-O-O-O-O-O-O-O	
2 M-M-M-M-M-M-O-I-I-I-I-O-O-M-M	
3 M-M-M-M-O	                
4 O-I-O-I-M-O-O-O-I-I	        
5 O	 
; run;
data temp;
 set have;
 do i=1 to countw(dna,'-');
  value=scan(dna,i,'-');output;
 end;
 drop i dna;
run;
proc summary data=temp ;
by id value notsorted;
output out=temp1;
run;
data want;
length want $ 200;
 do until(last.id);
  set temp1;
  by id;
  if _freq_=1 then want=catx('-',want,value);
   else want=catx('-',want,cats(_freq_,value));
 end;
 drop _type_ _freq_ value;
run;
proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Feb 2018 13:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-and-output-specific-number-of-strings-in-a-variable/m-p/433101#M107348</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-02-01T13:44:21Z</dc:date>
    </item>
  </channel>
</rss>

