<?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 create a string of values with a condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572595#M161597</link>
    <description>&lt;P&gt;Edit your post, pasting the code using the Insert SAS Code icon.&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jul 2019 03:29:20 GMT</pubDate>
    <dc:creator>ScottBass</dc:creator>
    <dc:date>2019-07-11T03:29:20Z</dc:date>
    <item>
      <title>How to create a string of values with a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572591#M161595</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Have a dataset that has an ID column, an item column and column called gap that is created to show the gap in days between the item columns per ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have code where I want to create a string (like a DNA string) of the item by each ID. So for each ID I would create a column called string that would concatenate all the item column values into a long string.&lt;/P&gt;&lt;P&gt;For example for ID 1 (in the data have below) the final string would yield O-O-O-O-O-T and for ID 2 the final string would be O-O-O-O-O-T. I know how to create the above string (see my code below).&lt;/P&gt;&lt;P&gt;What I want to do is bring in the condition that if the gap is greater than 10 then to start the string building anew.&lt;/P&gt;&lt;P&gt;so my output would actually look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas on what to add to my code below?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would want is:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;ID&amp;nbsp;string&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;1&amp;nbsp;&amp;nbsp; &lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;O-O-O&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp; O-O-T&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp; O-O-O&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp; O-O-T&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a sample of creating the have dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input ID 1. item $1. gap 5.; 
datalines; 
1 O 3 
1 O 4 
1 O 5 
1 O 15 
1 O 3 
1 T 4 
2 O 3 
2 O 5 
2 O 7 
2 O 11 
2 O 3 
2 T O 
; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this code I can create the string by ID, but how do I add the condition that if the gap is greater than 10 to start building a new string?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
set have; 
by ID; 
retain O T string;
length O T $1. string $50. ; 
if first.ID then do; 
O='0'; T='0';string=' ';end; 
string=catx('-',string,item); 
if item='O' then O ='1';
else if item='T' then T='1'; 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would want is:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;ID&amp;nbsp;string&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;1&amp;nbsp;&amp;nbsp; &lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;O-O-O&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp; O-O-T&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp; O-O-O&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp; O-O-T&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 03:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572591#M161595</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-07-11T03:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a string of values with a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572595#M161597</link>
      <description>&lt;P&gt;Edit your post, pasting the code using the Insert SAS Code icon.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 03:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572595#M161597</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-07-11T03:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a string of values with a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572596#M161598</link>
      <description>&lt;P&gt;edited&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 03:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572596#M161598</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-07-11T03:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a string of values with a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572604#M161606</link>
      <description>&lt;P&gt;Your first data step creates some empty variables, but that is easy fixed. So, for the next step there is a bit of if cases in the end. I assume that you only want to output the last row for each ID or if gap is larger than 10.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length ID 8 item $ 1 gap 8;
	input ID item gap ;
	datalines;
1 O 3 
1 O 4 
1 O 5 
1 O 15 
1 O 3 
1 T 4 
2 O 3 
2 O 5 
2 O 7 
2 O 11 
2 O 3 
2 T O 
;;;
run;

data want;
	set have;
	by ID;
	retain O T string;
	length O T $1. string $50.;

	if first.ID then do;
		O='0';
		T='0';
		string=' ';
	end;


	if item='O' then
		O ='1';
	else if item='T' then
		T='1';
	if last.ID then do;&lt;BR /&gt;		*If this is the last row with this ID, then add item to the string then output it.;
		string=catx('-',string,item);
		output;
	end;
	else if gap &amp;gt; 10 then do;
		*If the gap is larger than 10, then output it first, then "reinit"&amp;nbsp;string&amp;nbsp;with&amp;nbsp;item.&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;;
		output;
		string=item;
	end;
	else 	string=catx('-',string,item);&lt;BR /&gt;	*If neither of those cases, add item to string and then, without output, read the next line. ;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2019 04:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572604#M161606</guid>
      <dc:creator>heffo</dc:creator>
      <dc:date>2019-07-11T04:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a string of values with a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572605#M161607</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/103523"&gt;@sas_student1&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Looks like a veritable DoW-loop job:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                            
 input ID item :$1. gap ;              
 cards ;                               
1 O 3                                  
1 O 4                                  
1 O 5                                  
1 O 15                                 
1 O 3                                  
1 T 4                                  
2 O 3                                  
2 O 5                                  
2 O 7                                  
2 O 11                                 
2 O 3                                  
2 T 0                                  
;                                      
run ;                                  
                                       
data want (keep = id string) ;         
  do until (last.id) ;                 
    set have ;                         
    by id ;                            
    length string $ 16 ;               
    if gap &amp;gt; 10 then do ;              
      output ;                         
      string = "" ;                    
    end ;                              
    string = catx ("-", string, item) ;
  end ;                                
  output ;                             
run ;                                  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 04:28:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572605#M161607</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-11T04:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a string of values with a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572667#M161627</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                            
 input ID item :$1. gap ;              
 cards ;                               
1 O 3                                  
1 O 4                                  
1 O 5                                  
1 O 15                                 
1 O 3                                  
1 T 4                                  
2 O 3                                  
2 O 5                                  
2 O 7                                  
2 O 11                                 
2 O 3                                  
2 T 0                                  
;                                      
run ; 
data temp;
 set have;
 by id;
 if first.id or gap&amp;gt;10 then group+1;
run;
data want;
length want $ 200;
 do until(last.group);
   set temp;
   by group;
   want=catx('-',want,item);
 end;
 drop gap item group;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2019 11:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/572667#M161627</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-11T11:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a string of values with a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/576780#M163310</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/261618"&gt;@heffo&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp; Thank you to all!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 02:09:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-string-of-values-with-a-condition/m-p/576780#M163310</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-07-26T02:09:56Z</dc:date>
    </item>
  </channel>
</rss>

