<?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: Rolling up data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71985#M20822</link>
    <description>Here is one way, which is not most efficient, but easy.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
/* test data */&lt;BR /&gt;
data one;&lt;BR /&gt;
  input id letter $ @@;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 A 1 B 2 A 2 C 2 G 3 A&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* long to wide */&lt;BR /&gt;
proc transpose data=one out=wide;&lt;BR /&gt;
  var letter;&lt;BR /&gt;
  by id notsorted;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* roll up */&lt;BR /&gt;
data two;&lt;BR /&gt;
  set wide;&lt;BR /&gt;
  length letters $100; /* set it to large enough */&lt;BR /&gt;
  letters = catx(catx(",", of col:), "(", ")");&lt;BR /&gt;
  keep id letters;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* check */&lt;BR /&gt;
proc print data=two noobs;&lt;BR /&gt;
run;&lt;BR /&gt;
/* on lst&lt;BR /&gt;
id    letters&lt;BR /&gt;
 1    (A,B)&lt;BR /&gt;
 2    (A,C,G)&lt;BR /&gt;
 3    (A)&lt;BR /&gt;
*/&lt;BR /&gt;
[/pre]</description>
    <pubDate>Tue, 14 Sep 2010 17:14:49 GMT</pubDate>
    <dc:creator>chang_y_chung_hotmail_com</dc:creator>
    <dc:date>2010-09-14T17:14:49Z</dc:date>
    <item>
      <title>Rolling up data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71983#M20820</link>
      <description>I'm trying to rollup rows of data into a single row with a variable that holds a value for each row that it rolled up.  For example.&lt;BR /&gt;
&lt;BR /&gt;
1 A&lt;BR /&gt;
1 B&lt;BR /&gt;
2 A&lt;BR /&gt;
2 C&lt;BR /&gt;
2 G&lt;BR /&gt;
3 A&lt;BR /&gt;
&lt;BR /&gt;
I need my end result to be&lt;BR /&gt;
1 (A, B)&lt;BR /&gt;
2 (A, C,G)&lt;BR /&gt;
3 (A)&lt;BR /&gt;
&lt;BR /&gt;
This is the code i'm using and I can get the single rows back but not the ID with multiple rows.&lt;BR /&gt;
&lt;BR /&gt;
data z ;&lt;BR /&gt;
 	set letter ;&lt;BR /&gt;
	length letters $20 ;&lt;BR /&gt;
	by id ;&lt;BR /&gt;
	letters='';&lt;BR /&gt;
	if first.id = 1 and last.id = 1 then do ;&lt;BR /&gt;
		letters=letter ;&lt;BR /&gt;
		output;&lt;BR /&gt;
	end ;&lt;BR /&gt;
	else if first.id = 0 and last.id = 0 then do ;&lt;BR /&gt;
			letters=letters || ' ' || letter ;&lt;BR /&gt;
			output ;&lt;BR /&gt;
		end ;&lt;BR /&gt;
	else if first.id = 0 and last.id = 1 then do ;&lt;BR /&gt;
			letters=letters || ' ' || letter ;&lt;BR /&gt;
			output ;&lt;BR /&gt;
		end ;		 &lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
Thank you for any help.</description>
      <pubDate>Tue, 14 Sep 2010 16:48:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71983#M20820</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-09-14T16:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling up data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71984#M20821</link>
      <description>You must code a RETAIN statement when you want to retain an assigned variable across DATA step iterations.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Suggested Google advanced search argument, this topic / post:&lt;BR /&gt;
&lt;BR /&gt;
data step retain statement site:sas.com</description>
      <pubDate>Tue, 14 Sep 2010 16:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71984#M20821</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-09-14T16:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling up data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71985#M20822</link>
      <description>Here is one way, which is not most efficient, but easy.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
/* test data */&lt;BR /&gt;
data one;&lt;BR /&gt;
  input id letter $ @@;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 A 1 B 2 A 2 C 2 G 3 A&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* long to wide */&lt;BR /&gt;
proc transpose data=one out=wide;&lt;BR /&gt;
  var letter;&lt;BR /&gt;
  by id notsorted;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* roll up */&lt;BR /&gt;
data two;&lt;BR /&gt;
  set wide;&lt;BR /&gt;
  length letters $100; /* set it to large enough */&lt;BR /&gt;
  letters = catx(catx(",", of col:), "(", ")");&lt;BR /&gt;
  keep id letters;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* check */&lt;BR /&gt;
proc print data=two noobs;&lt;BR /&gt;
run;&lt;BR /&gt;
/* on lst&lt;BR /&gt;
id    letters&lt;BR /&gt;
 1    (A,B)&lt;BR /&gt;
 2    (A,C,G)&lt;BR /&gt;
 3    (A)&lt;BR /&gt;
*/&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 14 Sep 2010 17:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71985#M20822</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-09-14T17:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling up data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71986#M20823</link>
      <description>Your code needs more than just the retain statement, as you don't capture all conditions and your result wouldn't match that which you said you wanted.  You could try something like:&lt;BR /&gt;
&lt;BR /&gt;
data z (drop=letter);&lt;BR /&gt;
  retain letters;&lt;BR /&gt;
  set letter ;&lt;BR /&gt;
  length letters $20 ;&lt;BR /&gt;
  by id ;&lt;BR /&gt;
  letters=ifc(first.id,letter,catx(',',letters,letter));&lt;BR /&gt;
  if last.id then output;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Tue, 14 Sep 2010 17:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71986#M20823</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-09-14T17:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling up data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71987#M20824</link>
      <description>Thanks to everyone.&lt;BR /&gt;
&lt;BR /&gt;
 art297 you were right I wasn't capturing all the cases.  I corrected that and added the retain statement and now I got it to work.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thanks again to everyone for all your help pointing me in the right direction.</description>
      <pubDate>Tue, 14 Sep 2010 17:33:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-up-data/m-p/71987#M20824</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2010-09-14T17:33:24Z</dc:date>
    </item>
  </channel>
</rss>

