<?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: Concatenating Character Data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18937#M3782</link>
    <description>Hi:  &lt;BR /&gt;
You could either do it with FIRST.byvar and LAST.byvar in a datastep. Every time you read a new obs, you could concatenate that obs value for comment onto a new comment variable whose value would be retained until you got to the last obs of the group.&lt;BR /&gt;
 &lt;BR /&gt;
Or, you could use PROC TRANSPOSE by L_NAME to get the comments all on one obs and then just concatenate all the 5 possible comments together.&lt;BR /&gt;
 &lt;BR /&gt;
One possible transpose solution is shown below.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
** Read data and make an "origord" var;&lt;BR /&gt;
** to keep comments in the order they appeared in;&lt;BR /&gt;
** the original input data.;&lt;BR /&gt;
data allcom;&lt;BR /&gt;
  length l_name $25 comment $30;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input l_name &amp;amp; $ comment &amp;amp; $;&lt;BR /&gt;
  origord = _n_;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
Smith           One Fish&lt;BR /&gt;
Smith           Two Fish&lt;BR /&gt;
Smith           Red Fish&lt;BR /&gt;
Smith           Blue Fish&lt;BR /&gt;
Jones           Twas brillig and&lt;BR /&gt;
Jones           the slithy toves did&lt;BR /&gt;
Jones           gyre and gimble in the wabe&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                                 &lt;BR /&gt;
** Sort the data;&lt;BR /&gt;
proc sort data=allcom;&lt;BR /&gt;
by l_name origord;&lt;BR /&gt;
run;&lt;BR /&gt;
                             &lt;BR /&gt;
** Transpose the data;&lt;BR /&gt;
proc transpose data=allcom out=tr_out;&lt;BR /&gt;
by l_name;&lt;BR /&gt;
var comment;&lt;BR /&gt;
run;&lt;BR /&gt;
                               &lt;BR /&gt;
** Make the NewComment variable;&lt;BR /&gt;
data newcom (keep=l_name newcomment);&lt;BR /&gt;
  length col1 col2 col3 col4 col5 $30 newcomment $150;&lt;BR /&gt;
  set tr_out;&lt;BR /&gt;
              &lt;BR /&gt;
  newcomment = catx(' ',col1, col2, col3, col4, col5);&lt;BR /&gt;
run;&lt;BR /&gt;
                        &lt;BR /&gt;
** Print the new file;&lt;BR /&gt;
proc print data=newcom;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
    <pubDate>Wed, 15 Apr 2009 13:14:26 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2009-04-15T13:14:26Z</dc:date>
    <item>
      <title>Concatenating Character Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18936#M3781</link>
      <description>I&lt;BR /&gt;
have data that looks like this:&lt;BR /&gt;&lt;BR /&gt;L_NAME&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
COMMENT&lt;BR /&gt;Smith&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment1&lt;BR /&gt;Smith&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment2&lt;BR /&gt;Smith&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment3&lt;BR /&gt;Smith&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment4&lt;BR /&gt;Jones&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment1&lt;BR /&gt;Jones&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment2&lt;BR /&gt;Jones&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment3&lt;BR /&gt;&lt;BR /&gt;There can be up to 5 comments per last&lt;BR /&gt;
name.&amp;nbsp; I want to get all 4 comments for Smith (for example)&lt;BR /&gt;
concatenated on one line such as this:&lt;BR /&gt;&lt;BR /&gt;L_NAME&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
COMMENT&lt;BR /&gt;Smith&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;
Comment1Comment2Comment3Comment4&lt;BR /&gt;&lt;BR /&gt;I usually zip&lt;BR /&gt;
through these data step problems but this one is giving me fits.&lt;BR /&gt;&lt;BR /&gt;Any&lt;BR /&gt;
hints are greatly appreciated!</description>
      <pubDate>Wed, 15 Apr 2009 12:49:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18936#M3781</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-04-15T12:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating Character Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18937#M3782</link>
      <description>Hi:  &lt;BR /&gt;
You could either do it with FIRST.byvar and LAST.byvar in a datastep. Every time you read a new obs, you could concatenate that obs value for comment onto a new comment variable whose value would be retained until you got to the last obs of the group.&lt;BR /&gt;
 &lt;BR /&gt;
Or, you could use PROC TRANSPOSE by L_NAME to get the comments all on one obs and then just concatenate all the 5 possible comments together.&lt;BR /&gt;
 &lt;BR /&gt;
One possible transpose solution is shown below.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
** Read data and make an "origord" var;&lt;BR /&gt;
** to keep comments in the order they appeared in;&lt;BR /&gt;
** the original input data.;&lt;BR /&gt;
data allcom;&lt;BR /&gt;
  length l_name $25 comment $30;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input l_name &amp;amp; $ comment &amp;amp; $;&lt;BR /&gt;
  origord = _n_;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
Smith           One Fish&lt;BR /&gt;
Smith           Two Fish&lt;BR /&gt;
Smith           Red Fish&lt;BR /&gt;
Smith           Blue Fish&lt;BR /&gt;
Jones           Twas brillig and&lt;BR /&gt;
Jones           the slithy toves did&lt;BR /&gt;
Jones           gyre and gimble in the wabe&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                                 &lt;BR /&gt;
** Sort the data;&lt;BR /&gt;
proc sort data=allcom;&lt;BR /&gt;
by l_name origord;&lt;BR /&gt;
run;&lt;BR /&gt;
                             &lt;BR /&gt;
** Transpose the data;&lt;BR /&gt;
proc transpose data=allcom out=tr_out;&lt;BR /&gt;
by l_name;&lt;BR /&gt;
var comment;&lt;BR /&gt;
run;&lt;BR /&gt;
                               &lt;BR /&gt;
** Make the NewComment variable;&lt;BR /&gt;
data newcom (keep=l_name newcomment);&lt;BR /&gt;
  length col1 col2 col3 col4 col5 $30 newcomment $150;&lt;BR /&gt;
  set tr_out;&lt;BR /&gt;
              &lt;BR /&gt;
  newcomment = catx(' ',col1, col2, col3, col4, col5);&lt;BR /&gt;
run;&lt;BR /&gt;
                        &lt;BR /&gt;
** Print the new file;&lt;BR /&gt;
proc print data=newcom;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 15 Apr 2009 13:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18937#M3782</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-04-15T13:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating Character Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18938#M3783</link>
      <description>I was using the first.byvar and last.byvar approach to no avail.&lt;BR /&gt;
&lt;BR /&gt;
I'll give the transpose solution a whirl.&lt;BR /&gt;
&lt;BR /&gt;
Thanks!</description>
      <pubDate>Wed, 15 Apr 2009 13:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18938#M3783</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-04-15T13:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating Character Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18939#M3784</link>
      <description>Check your SAS log output for a diagnostic message, such as: &lt;BR /&gt;
&lt;BR /&gt;
NOTE: VARIABLE FIRST.&lt;SOME_BY_VARIABLE&gt; IS UNINITIALIZED&lt;BR /&gt;
&lt;BR /&gt;
indicating that you have forgotten to code a BY statement in your DATA step.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/SOME_BY_VARIABLE&gt;</description>
      <pubDate>Wed, 15 Apr 2009 17:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18939#M3784</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-04-15T17:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating Character Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18940#M3785</link>
      <description>Dorfman and Whitlock are names of experts who in discussion together came up with an approach that moves the SET statement away from the top of a data step into a loop. The approach is acquiring the title DoW loop. &lt;BR /&gt;
Here it is most helpful[pre]   data reduced( keep= L_name comment  compress=yes ) ;&lt;BR /&gt;
      length comment $2000 ; * surely enough, but can be wider  ;&lt;BR /&gt;
      do until( last.L_name ) ;&lt;BR /&gt;
         set full_data_set( rename=( comment= oneComment )) ;&lt;BR /&gt;
         by L_name ;&lt;BR /&gt;
         comment= trimn( comment ) !! oneComment ;&lt;BR /&gt;
      end ;&lt;BR /&gt;
   run ; [/pre]The merit of DoW looping is brief code using the nature of the data step to help solve the problem.&lt;BR /&gt;
See Paul Dorfman's 2009 paper on the topic at &lt;A href="http://support.sas.com/resources/papers/proceedings09/038-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/038-2009.pdf&lt;/A&gt; &lt;BR /&gt;
 &lt;BR /&gt;
I'm sure understanding and mastering the technique is an important stage in learning to program in a SAS System environment.&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Thu, 16 Apr 2009 09:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18940#M3785</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-04-16T09:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating Character Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18941#M3786</link>
      <description>Hello Peter.&lt;BR /&gt;
&lt;BR /&gt;
Great reply.&lt;BR /&gt;
&lt;BR /&gt;
The wonderful magic of the DoW loop technique and its many variations.&lt;BR /&gt;
&lt;BR /&gt;
I attended Paul's presentation about the DoW loop at SGF2009, for me one of the best papers.&lt;BR /&gt;
I also loved the presentation about the "hardcore" performance POKE/PEEK functions (see: &lt;A href="http://support.sas.com/resources/papers/proceedings09/010-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/010-2009.pdf&lt;/A&gt; ).&lt;BR /&gt;
&lt;BR /&gt;
Greetings from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos at &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Thu, 16 Apr 2009 10:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Concatenating-Character-Data/m-p/18941#M3786</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-04-16T10:30:43Z</dc:date>
    </item>
  </channel>
</rss>

