<?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 to retain data from one row to the next in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-data-from-one-row-to-the-next/m-p/778619#M247867</link>
    <description>&lt;P&gt;Hi all, I'm struggling with carrying data from one row to the next using a method that I'm pretty sure has worked in the past. I simply want to carry the 'name' from one row to the next, within each table subset, and end up with a last.table record which has all of the names in that table concatenated into one string. I also want to make use of the 'retain' function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my current data:&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;name&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;table&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;rowcat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Riley&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Henderson&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;MacDonald&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Cook&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;DuPage&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Samoa&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;name&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;table&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;rowcat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Henderson&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley, Henderson,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;MacDonald&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley, Henderson, MacDonald,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Cook&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley, Henderson, MacDonald, Cook,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;DuPage&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;DuPage,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Samoa&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;DuPage, Samoa,&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me understand why this code isn't working?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length name $ 12 table $ 2 rowcat $ 200;
	input name $ table $;
	datalines;
		Riley t1
		Henderson t1
		MacDonald t1
		Cook t1
		DuPage t2
		Samoa t2
	;
run;

proc sort data=have; by table; run;

data want; 
	set have; 
	by table;

	if first.table then do;
		rowcat = ' ';
	end;

	rowcat = strip(strip(name)||", "||strip(rowcat));

	retain rowcat;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks!!&lt;/P&gt;</description>
    <pubDate>Thu, 04 Nov 2021 22:23:35 GMT</pubDate>
    <dc:creator>techsassy</dc:creator>
    <dc:date>2021-11-04T22:23:35Z</dc:date>
    <item>
      <title>How to retain data from one row to the next</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-data-from-one-row-to-the-next/m-p/778619#M247867</link>
      <description>&lt;P&gt;Hi all, I'm struggling with carrying data from one row to the next using a method that I'm pretty sure has worked in the past. I simply want to carry the 'name' from one row to the next, within each table subset, and end up with a last.table record which has all of the names in that table concatenated into one string. I also want to make use of the 'retain' function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my current data:&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;name&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;table&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;rowcat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Riley&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Henderson&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;MacDonald&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Cook&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;DuPage&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;Samoa&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;name&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;table&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;rowcat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Henderson&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley, Henderson,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;MacDonald&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley, Henderson, MacDonald,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Cook&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Riley, Henderson, MacDonald, Cook,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;DuPage&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;DuPage,&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Samoa&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;t2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;DuPage, Samoa,&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me understand why this code isn't working?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length name $ 12 table $ 2 rowcat $ 200;
	input name $ table $;
	datalines;
		Riley t1
		Henderson t1
		MacDonald t1
		Cook t1
		DuPage t2
		Samoa t2
	;
run;

proc sort data=have; by table; run;

data want; 
	set have; 
	by table;

	if first.table then do;
		rowcat = ' ';
	end;

	rowcat = strip(strip(name)||", "||strip(rowcat));

	retain rowcat;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks!!&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 22:23:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-data-from-one-row-to-the-next/m-p/778619#M247867</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2021-11-04T22:23:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain data from one row to the next</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-data-from-one-row-to-the-next/m-p/778635#M247872</link>
      <description>&lt;P&gt;This makes the shown desired output:&lt;/P&gt;
&lt;PRE&gt;data have;
	length name $ 12 table $ 2 ;
	input name $ table $;
	datalines;
Riley t1
Henderson t1
MacDonald t1
Cook t1
DuPage t2
Samoa t2
	;
run;

proc sort data=have; 
   by table; 
run;

data want; 
	set have; 
	by table;
   length rowcat $ 200;
   retain rowcat;
	if first.table then do;
		rowcat = ' ';
	end;

	rowcat = catx(', ',rowcat,name);

run;&lt;/PRE&gt;
&lt;P&gt;You had a moderately subtle technical issue.&amp;nbsp; If you Retain a variable that is on the input data set it gets reread from the source data set, in your case missing. So nothing would be kept from the previous record.&lt;/P&gt;
&lt;P&gt;Note that I removed the ROWCAT from the first data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Catx, and the other CAT functions are bit cleaner than using strip(variable)|| constantly. The Catx function places the first parameter between values when both are not missing. So unless you actually want multiple commas adjacent if a Name happened to be missing is a more compact code. CATX automatically removes trailing blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 23:33:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-data-from-one-row-to-the-next/m-p/778635#M247872</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-04T23:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain data from one row to the next</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-data-from-one-row-to-the-next/m-p/778641#M247873</link>
      <description>&lt;P&gt;Thanks so much for the solution, as well as the CAT function encouragement!&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 23:50:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-data-from-one-row-to-the-next/m-p/778641#M247873</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2021-11-04T23:50:25Z</dc:date>
    </item>
  </channel>
</rss>

