<?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: Help with strings and retain in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38357#M9831</link>
    <description>I think you need to get rid of leading and trailing blanks....&lt;BR /&gt;
&lt;BR /&gt;
if last.flag1 then text2 = &lt;B&gt;strip&lt;/B&gt;(text2) || &lt;B&gt;strip&lt;/B&gt;(text) ;&lt;BR /&gt;
&lt;BR /&gt;
Not sure if I undertood your problem correctly, but below code should give you the desired output...&lt;BR /&gt;
&lt;BR /&gt;
data one;&lt;BR /&gt;
      length VAR Text $100;&lt;BR /&gt;
      var = 'Test1'; flag1=1; flag2=7; text='This is test string'; output;&lt;BR /&gt;
      var = 'Test1'; flag1=1; flag2=12; text='This is test string'; output;&lt;BR /&gt;
      var = 'Test1'; flag1=2; flag2=.; text='This line needs to be added to previous line'; output;&lt;BR /&gt;
      var = 'Test2'; flag1=1; flag2=55; text='This is test string'; output;&lt;BR /&gt;
      var = 'Test2'; flag1=2; flag2=.; text='This line needs to be added to previous line'; output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort; by var descending flag1 descending flag2; run;&lt;BR /&gt;
&lt;BR /&gt;
data two;&lt;BR /&gt;
      length pretext newtext $100;&lt;BR /&gt;
      set one;&lt;BR /&gt;
      by var descending flag1 descending flag2;&lt;BR /&gt;
      retain pretext preflag;&lt;BR /&gt;
      if first.var then do; pretext=''; preflag=.; end;&lt;BR /&gt;
      if flag1=1 and preflag=2 then newtext=strip(text)||strip(pretext);&lt;BR /&gt;
      else newtext=strip(text);&lt;BR /&gt;
      pretext=text;&lt;BR /&gt;
      preflag=flag1;&lt;BR /&gt;
      if flag1=1;&lt;BR /&gt;
      drop preflag pretext;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort; by var flag1 flag2; run;</description>
    <pubDate>Mon, 22 Nov 2010 21:19:41 GMT</pubDate>
    <dc:creator>NickR</dc:creator>
    <dc:date>2010-11-22T21:19:41Z</dc:date>
    <item>
      <title>Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38353#M9827</link>
      <description>I have the following values and I can't seem to get the end result&lt;BR /&gt;
&lt;BR /&gt;
VAR  FLAG1 FLAG2 Text&lt;BR /&gt;
Test1 1          7.	        This is test string&lt;BR /&gt;
Test1 1         12. 	This is test string&lt;BR /&gt;
Test1 2		        This line needs to be added to previous line&lt;BR /&gt;
Test2 1         55.	This is test string&lt;BR /&gt;
Test2 2		        This line needs to be added to previous line&lt;BR /&gt;
&lt;BR /&gt;
My final result should be &lt;BR /&gt;
&lt;BR /&gt;
Test1 1 7.	        This is test string&lt;BR /&gt;
Test1 1 12. 	This is test string This line needs to be added to previous line&lt;BR /&gt;
Test2 1 55.	This is test string This line needs to be added to previous line&lt;BR /&gt;
&lt;BR /&gt;
This is my program&lt;BR /&gt;
data temp1 ;&lt;BR /&gt;
	set temp0 ;&lt;BR /&gt;
	by var flag1 ;&lt;BR /&gt;
&lt;BR /&gt;
	retain text2 ;&lt;BR /&gt;
	if first.flag1 and find(flag3, '.') &amp;gt; 0 then text2 = text ;&lt;BR /&gt;
	if last.flag1 then text2 = text2 || text ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
Any help would be greatly appreciated.  I think i'm missing something simple but I can't pin point it.   &lt;BR /&gt;
&lt;BR /&gt;
thank you</description>
      <pubDate>Mon, 22 Nov 2010 18:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38353#M9827</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-11-22T18:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38354#M9828</link>
      <description>A couple of things that you need to look at:&lt;BR /&gt;
&lt;BR /&gt;
Flag3 does not appear to be defined.&lt;BR /&gt;
Print out temp0; the data you list for the flag1=2 doesn't look right.&lt;BR /&gt;
&lt;BR /&gt;
Doc Muhlbaier&lt;BR /&gt;
Duke</description>
      <pubDate>Mon, 22 Nov 2010 19:24:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38354#M9828</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2010-11-22T19:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38355#M9829</link>
      <description>Duke,&lt;BR /&gt;
&lt;BR /&gt;
Flag3 is actually flag2.  The rows with flag1=2 need to be rolled up into the text with the previous row.  I've worked with retain before but for some reason this is giving me the hardest time.&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your help</description>
      <pubDate>Mon, 22 Nov 2010 19:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38355#M9829</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-11-22T19:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38356#M9830</link>
      <description>Hi:&lt;BR /&gt;
  Let's verify what WORK.TEMP0 looks like before we go down the road of blaming poor old RETAIN for causing problems. Can you explain what is the purpose of the FLAG2 variable and why it has values on some variables and not on others??? Is FLAG2 character or numeric??? Based on what you posted, and assuming that FLAG2 is numeric (and not character), this is what I think that TEMP0 looks like (showing all the variables "lined up" helps me visualize what every observation looks like):&lt;BR /&gt;
[pre]&lt;BR /&gt;
what is in WORK.TEMP0&lt;BR /&gt;
          &lt;BR /&gt;
 var     flag1    flag2    text&lt;BR /&gt;
            &lt;BR /&gt;
Test1      1         7     This is test string&lt;BR /&gt;
Test1      1        12     This is test string&lt;BR /&gt;
Test1      2         .     This line needs to be added to previous line&lt;BR /&gt;
Test2      1        55     This is test string&lt;BR /&gt;
Test2      2         .     This line needs to be added to previous line&lt;BR /&gt;
[/pre]&lt;BR /&gt;
             &lt;BR /&gt;
Can you also explain why your desired output has this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
Test1 1 7. This is test string&lt;BR /&gt;
Test1 1 12. This is test string This line needs to be added to previous line&lt;BR /&gt;
Test2 1 55. This is test string This line needs to be added to previous line&lt;BR /&gt;
[/pre]&lt;BR /&gt;
   &lt;BR /&gt;
I don't understand why there are 2 Test1 observations, but only 1 Test2 observation. Also in your original data you show values for FLAG2 of 7. 12. and 55. -- is the '.' significant????? &lt;BR /&gt;
 &lt;BR /&gt;
Can you elaborate on WORK.TEMP0 and the desired results??&lt;BR /&gt;
&lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 22 Nov 2010 20:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38356#M9830</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-11-22T20:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38357#M9831</link>
      <description>I think you need to get rid of leading and trailing blanks....&lt;BR /&gt;
&lt;BR /&gt;
if last.flag1 then text2 = &lt;B&gt;strip&lt;/B&gt;(text2) || &lt;B&gt;strip&lt;/B&gt;(text) ;&lt;BR /&gt;
&lt;BR /&gt;
Not sure if I undertood your problem correctly, but below code should give you the desired output...&lt;BR /&gt;
&lt;BR /&gt;
data one;&lt;BR /&gt;
      length VAR Text $100;&lt;BR /&gt;
      var = 'Test1'; flag1=1; flag2=7; text='This is test string'; output;&lt;BR /&gt;
      var = 'Test1'; flag1=1; flag2=12; text='This is test string'; output;&lt;BR /&gt;
      var = 'Test1'; flag1=2; flag2=.; text='This line needs to be added to previous line'; output;&lt;BR /&gt;
      var = 'Test2'; flag1=1; flag2=55; text='This is test string'; output;&lt;BR /&gt;
      var = 'Test2'; flag1=2; flag2=.; text='This line needs to be added to previous line'; output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort; by var descending flag1 descending flag2; run;&lt;BR /&gt;
&lt;BR /&gt;
data two;&lt;BR /&gt;
      length pretext newtext $100;&lt;BR /&gt;
      set one;&lt;BR /&gt;
      by var descending flag1 descending flag2;&lt;BR /&gt;
      retain pretext preflag;&lt;BR /&gt;
      if first.var then do; pretext=''; preflag=.; end;&lt;BR /&gt;
      if flag1=1 and preflag=2 then newtext=strip(text)||strip(pretext);&lt;BR /&gt;
      else newtext=strip(text);&lt;BR /&gt;
      pretext=text;&lt;BR /&gt;
      preflag=flag1;&lt;BR /&gt;
      if flag1=1;&lt;BR /&gt;
      drop preflag pretext;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort; by var flag1 flag2; run;</description>
      <pubDate>Mon, 22 Nov 2010 21:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38357#M9831</guid>
      <dc:creator>NickR</dc:creator>
      <dc:date>2010-11-22T21:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38358#M9832</link>
      <description>Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
Thank so much for your reply.  &lt;BR /&gt;
What you have for TEMP0 is exactly what I have except that Flag2 is character and the "." is needed.  This is based on a big file i'm working with.  I'm trying to crate a dataset based on certain flags.  What is happeneing in my file is that the text is wraping to another line where flag2 = missing.  So that row has to be rolled up with the previous lines text.  What is happening is that I was given a really big text file and I have to find a way to pull out specific portions of that file.  That i'm close too, it's the wrapping the text from the previous line that was giving me a really hard time.  &lt;BR /&gt;
&lt;BR /&gt;
This is the code I finally think I have working&lt;BR /&gt;
&lt;BR /&gt;
data temp1 ;&lt;BR /&gt;
	length lngstr $ 200 ;&lt;BR /&gt;
	set temp0 ;&lt;BR /&gt;
	retain lngstr " " ;	&lt;BR /&gt;
&lt;BR /&gt;
	if flag2 ne . then lngstr = text ;&lt;BR /&gt;
	else if flag2 = 1 then lngstr = catx(' ',lngstr,text);	&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
Thank you again for all your help on this and other questions i've asked.</description>
      <pubDate>Tue, 23 Nov 2010 13:52:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38358#M9832</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-11-23T13:52:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38359#M9833</link>
      <description>Nick,&lt;BR /&gt;
&lt;BR /&gt;
Thank you for your solution as well. That worked just like I wanted it too.  I'm going to spend some time looking at it and clearing up how I have to handle strings with the retain statement.&lt;BR /&gt;
&lt;BR /&gt;
Thank you again for your help&lt;BR /&gt;
&lt;BR /&gt;
Jerry</description>
      <pubDate>Tue, 23 Nov 2010 13:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38359#M9833</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-11-23T13:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38360#M9834</link>
      <description>Hi:&lt;BR /&gt;
  If FLAG2 is CHARACTER, then this statement &lt;BR /&gt;
[pre]&lt;BR /&gt;
if flag2 ne . then lngstr = text ;&lt;BR /&gt;
else if flag2 = 1 then lngstr = catx(' ',lngstr,text); &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                    &lt;BR /&gt;
should be causing a note in the log:&lt;BR /&gt;
[pre]&lt;BR /&gt;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).&lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
This IF statement is forcing FLAG2 to be treated as a NUMBER, not a CHARACTER -- it might not make any difference for a simple test like what you're doing, but it is better to get these concepts straight from the beginning. &lt;BR /&gt;
&lt;BR /&gt;
However, I see a different problem. In the data that I posted, the values for FLAG2 are 7, 12, 55 and . or as character strings they would be '7', '12', '55' and '.'. None of the FLAG2 variables has a value of 1. On the other hand, FLAG1 has values of 1 and 2, so I'm wondering whether you've posted working code.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 23 Nov 2010 14:13:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38360#M9834</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-11-23T14:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38361#M9835</link>
      <description>Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
you are correct.  That code should have been&lt;BR /&gt;
&lt;BR /&gt;
if flag2 ne . then lngstr = text ;&lt;BR /&gt;
else lngstr = catx(' ',lngstr,text); &lt;BR /&gt;
&lt;BR /&gt;
If flag2 is "." that means it should be added to the end of the line above it.&lt;BR /&gt;
&lt;BR /&gt;
Thanks again for you time and help.  I work with SAS but not on a consistent basis as I would like so I'm not able to grasp it like I would like.  That help here is truly a gift. &lt;BR /&gt;
&lt;BR /&gt;
Thank You</description>
      <pubDate>Tue, 23 Nov 2010 19:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38361#M9835</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-11-23T19:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38362#M9836</link>
      <description>Hi.&lt;BR /&gt;
Just as Cynthia said,If flag2 is character ,why using ' flag2 ne .  '  ? Your data structure is ambiguous.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data temp0;&lt;BR /&gt;
 input var $ flag1 flag2 $3. text &amp;amp; $50.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
Test1 1 7. This is test string&lt;BR /&gt;
Test1 1 12. This is test string&lt;BR /&gt;
Test1 2     This line needs to be added to previous line&lt;BR /&gt;
Test2 1 55. This is test string&lt;BR /&gt;
Test2 2     This line needs to be added to previous line&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=temp0;&lt;BR /&gt;
 by var;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp1 index(keep=var text rename=(text=_text));&lt;BR /&gt;
 set temp0;&lt;BR /&gt;
 by var;&lt;BR /&gt;
 if last.var then do; &lt;BR /&gt;
                  output index; &lt;BR /&gt;
                  delete; &lt;BR /&gt;
                  end;&lt;BR /&gt;
 output temp1;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp2;&lt;BR /&gt;
 merge temp1 index;&lt;BR /&gt;
 by var;&lt;BR /&gt;
run;&lt;BR /&gt;
data op(drop=text _text);&lt;BR /&gt;
 set temp2;&lt;BR /&gt;
 by var;&lt;BR /&gt;
 length __text $ 100;&lt;BR /&gt;
 __text=text;&lt;BR /&gt;
 if last.var then __text=catx(' ',text,_text);&lt;BR /&gt;
run;&lt;BR /&gt;
proc print noobs;run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 24 Nov 2010 01:45:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38362#M9836</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-11-24T01:45:06Z</dc:date>
    </item>
    <item>
      <title>Re: Help with strings and retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38363#M9837</link>
      <description>KSharp,&lt;BR /&gt;
&lt;BR /&gt;
Thank you for your post.  I corrected the issue you mention in your post and finally have it working.  &lt;BR /&gt;
&lt;BR /&gt;
Thank you for your help&lt;BR /&gt;
&lt;BR /&gt;
Jerry</description>
      <pubDate>Wed, 24 Nov 2010 13:33:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-strings-and-retain/m-p/38363#M9837</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-11-24T13:33:00Z</dc:date>
    </item>
  </channel>
</rss>

