<?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: Concatenate in SAS Health and Life Sciences</title>
    <link>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Concatenate/m-p/6435#M629</link>
    <description>My approach would have been different from a macro approach. I stuck with DATA step for the solution, assuming the data was stored in a file called PTINFO, with the variables VAR1 (patient id) and VAR2:&lt;BR /&gt;
[pre]&lt;BR /&gt;
    &lt;BR /&gt;
proc sort data=ptinfo out=ptinfo;&lt;BR /&gt;
by var1;&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
data newpt(keep=var1 newvar2);&lt;BR /&gt;
  set ptinfo; by var1;&lt;BR /&gt;
  length newvar2 $200 sep $2;&lt;BR /&gt;
  retain sep ', ' newvar2;&lt;BR /&gt;
  if first.var1 then newvar2 = ' ';&lt;BR /&gt;
  newvar2 = catx(sep,newvar2,var2);&lt;BR /&gt;
  if last.var1 then output;&lt;BR /&gt;
run;&lt;BR /&gt;
     &lt;BR /&gt;
proc print data=newpt;&lt;BR /&gt;
title 'New PT file';&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
[/pre]&lt;BR /&gt;
Yields this result from the proc print:&lt;BR /&gt;
[pre]&lt;BR /&gt;
New PT file&lt;BR /&gt;
  &lt;BR /&gt;
Obs    var1     newvar2&lt;BR /&gt;
  &lt;BR /&gt;
 1     a101    aaaa, bbbb&lt;BR /&gt;
 2     a102    cccc&lt;BR /&gt;
 3     a103    dddd, eeee, fffff&lt;BR /&gt;
  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
The ability to use BY group processing and FIRST.VAR1 and LAST.VAR1 made it possible to read through every PT observation and build a new variable called NEWVAR2 by concatenating the single value for VAR2 onto the value of NEWVAR2 for every row. The only other things needed to make this work are the reinitialization of NEWVAR1 for the first record for a patient and then to control the output using an explicit OUTPUT statement on the last record for a patient.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
    <pubDate>Mon, 21 Jan 2008 16:39:41 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2008-01-21T16:39:41Z</dc:date>
    <item>
      <title>Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Concatenate/m-p/6433#M627</link>
      <description>Hi all&lt;BR /&gt;
  i have problem with concatenating rows &lt;BR /&gt;
here is the sample dataset&lt;BR /&gt;
pt      var1 &lt;BR /&gt;
a101   aaaa&lt;BR /&gt;
a101  bbbb&lt;BR /&gt;
a102   cccc&lt;BR /&gt;
a103   dddd&lt;BR /&gt;
a103   eeee&lt;BR /&gt;
a103    fffff&lt;BR /&gt;
i have to get result like below&lt;BR /&gt;
 &lt;BR /&gt;
pt    var1 &lt;BR /&gt;
a101  aaaa, bbbb&lt;BR /&gt;
a101   cccc&lt;BR /&gt;
a103   dddd, eeee, ffff&lt;BR /&gt;
                  can any one tell me how to solve this &lt;BR /&gt;
                            thanks</description>
      <pubDate>Mon, 21 Jan 2008 10:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Concatenate/m-p/6433#M627</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-01-21T10:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Concatenate/m-p/6434#M628</link>
      <description>The solution I came up with uses SQL and macro variables:&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
input var1 $4. var2 $6.;&lt;BR /&gt;
cards;&lt;BR /&gt;
a101 aaaa&lt;BR /&gt;
a101 bbbb&lt;BR /&gt;
a102 cccc&lt;BR /&gt;
a103 dddd&lt;BR /&gt;
a103 eeee&lt;BR /&gt;
a103 fffff&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;;&lt;BR /&gt;
	select count (distinct var1) into :loopcnt&lt;BR /&gt;
	from test;&lt;BR /&gt;
%let loopcnt = &amp;amp;loopcnt;&lt;BR /&gt;
	select distinct var1 into :V1 - :V&amp;amp;loopcnt&lt;BR /&gt;
	from test;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
%macro loop;&lt;BR /&gt;
%do i = 1 %to &amp;amp;loopcnt;&lt;BR /&gt;
	proc sql noprint;&lt;BR /&gt;
		select var1, var2 into :Val1, :Val2 separated by ","&lt;BR /&gt;
		from test&lt;BR /&gt;
		where var1 = "&amp;amp;&amp;amp;V&amp;amp;i";&lt;BR /&gt;
	quit;	&lt;BR /&gt;
&lt;BR /&gt;
	data tmp;&lt;BR /&gt;
		var1 = "&amp;amp;Val1";&lt;BR /&gt;
		length String $256;&lt;BR /&gt;
		String = "&amp;amp;Val2";&lt;BR /&gt;
	run;&lt;BR /&gt;
&lt;BR /&gt;
	proc datasets library=work nolist;&lt;BR /&gt;
		%if &amp;amp;i = 1 %then %do; delete out; %end;&lt;BR /&gt;
		append base=out data=tmp;&lt;BR /&gt;
	run;&lt;BR /&gt;
	quit;&lt;BR /&gt;
&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend loop;&lt;BR /&gt;
%loop;</description>
      <pubDate>Mon, 21 Jan 2008 16:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Concatenate/m-p/6434#M628</guid>
      <dc:creator>1162</dc:creator>
      <dc:date>2008-01-21T16:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Concatenate/m-p/6435#M629</link>
      <description>My approach would have been different from a macro approach. I stuck with DATA step for the solution, assuming the data was stored in a file called PTINFO, with the variables VAR1 (patient id) and VAR2:&lt;BR /&gt;
[pre]&lt;BR /&gt;
    &lt;BR /&gt;
proc sort data=ptinfo out=ptinfo;&lt;BR /&gt;
by var1;&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
data newpt(keep=var1 newvar2);&lt;BR /&gt;
  set ptinfo; by var1;&lt;BR /&gt;
  length newvar2 $200 sep $2;&lt;BR /&gt;
  retain sep ', ' newvar2;&lt;BR /&gt;
  if first.var1 then newvar2 = ' ';&lt;BR /&gt;
  newvar2 = catx(sep,newvar2,var2);&lt;BR /&gt;
  if last.var1 then output;&lt;BR /&gt;
run;&lt;BR /&gt;
     &lt;BR /&gt;
proc print data=newpt;&lt;BR /&gt;
title 'New PT file';&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
[/pre]&lt;BR /&gt;
Yields this result from the proc print:&lt;BR /&gt;
[pre]&lt;BR /&gt;
New PT file&lt;BR /&gt;
  &lt;BR /&gt;
Obs    var1     newvar2&lt;BR /&gt;
  &lt;BR /&gt;
 1     a101    aaaa, bbbb&lt;BR /&gt;
 2     a102    cccc&lt;BR /&gt;
 3     a103    dddd, eeee, fffff&lt;BR /&gt;
  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
The ability to use BY group processing and FIRST.VAR1 and LAST.VAR1 made it possible to read through every PT observation and build a new variable called NEWVAR2 by concatenating the single value for VAR2 onto the value of NEWVAR2 for every row. The only other things needed to make this work are the reinitialization of NEWVAR1 for the first record for a patient and then to control the output using an explicit OUTPUT statement on the last record for a patient.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 21 Jan 2008 16:39:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Concatenate/m-p/6435#M629</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-01-21T16:39:41Z</dc:date>
    </item>
  </channel>
</rss>

