<?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: recurse in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513587#M138381</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*create sample data for demonstration;
data have;
    infile cards dlm='09'x;
    input OrgID Product $   States $;
    cards;
1   football    DC
1   football    VA
1   football    MD
2   football    CA
3   football    NV
3   football    CA
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by orgID;
    length combined $100.;
    retain combined;

    if first.orgID then
        combined=states;
    else
        combined=catx(', ', combined, states);

    if last.orgID then
        output;
run;

**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
    by orgID;
    var states;
run;

data want_option2;
    set wide;
    length combined $100.;
    combined=catx(', ', of state_:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the use of CAT_ function instead of pipes, it helps reduce issues, ie trailing and leading spaces or wanting to add a delimiter between items.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a" target="_blank"&gt;https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30022"&gt;@cellurl&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;data out;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set in;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val='bob ';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val=val || middle_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val= val || last_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to make this build up over several lines? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 15 Nov 2018 20:42:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-11-15T20:42:14Z</dc:date>
    <item>
      <title>recurse</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513580#M138377</link>
      <description>&lt;P&gt;data out;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set in;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val='bob ';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val=val || middle_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val= val || last_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to make this build up over several lines? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 20:33:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513580#M138377</guid>
      <dc:creator>cellurl</dc:creator>
      <dc:date>2018-11-15T20:33:57Z</dc:date>
    </item>
    <item>
      <title>Re: recurse</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513584#M138380</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30022"&gt;@cellurl&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;data out;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set in;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val='bob ';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val=val || middle_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val= val || last_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to make this build up over several lines? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It may help to provide an example. If you want to combine several "words" it is usually a good idea to specify an overall length for the variable. In your example the variable VAL would have a length of 3 and concatenating other words won't actually work.&lt;/P&gt;
&lt;P&gt;Consider assuming that you think the longest value resulting with all the words would be 25 characters including spaces to separate them. Increase the value of 25 if you need longer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length val $ 25;&lt;/P&gt;
&lt;P&gt;val = catx(' ','bob', middle_name, last_name);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;would create the val with a single space, the first parameter to the catx function, between bob and the values of middle_name and last_name.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 20:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513584#M138380</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-11-15T20:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: recurse</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513587#M138381</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*create sample data for demonstration;
data have;
    infile cards dlm='09'x;
    input OrgID Product $   States $;
    cards;
1   football    DC
1   football    VA
1   football    MD
2   football    CA
3   football    NV
3   football    CA
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by orgID;
    length combined $100.;
    retain combined;

    if first.orgID then
        combined=states;
    else
        combined=catx(', ', combined, states);

    if last.orgID then
        output;
run;

**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
    by orgID;
    var states;
run;

data want_option2;
    set wide;
    length combined $100.;
    combined=catx(', ', of state_:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the use of CAT_ function instead of pipes, it helps reduce issues, ie trailing and leading spaces or wanting to add a delimiter between items.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a" target="_blank"&gt;https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30022"&gt;@cellurl&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;data out;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set in;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val='bob ';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val=val || middle_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; val= val || last_name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to make this build up over several lines? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 20:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513587#M138381</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-15T20:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: recurse</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513588#M138382</link>
      <description>&lt;P&gt;It is not clear what you mean by "several lines".&lt;/P&gt;
&lt;P&gt;What is your input format. Post a sample of your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW you can write the lines into one:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
        length new_val $30;
    &amp;nbsp; &amp;nbsp; &amp;nbsp;new_val = 'Bob ' || middle_name || last_name;
        /* or:    new_val = catx(' ', 'Bob',middle_name,last_name); */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Nov 2018 20:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513588#M138382</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-11-15T20:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: recurse</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513603#M138389</link>
      <description>&lt;P&gt;replacing pipes with catx is probably the gem of learning. Thanks both of you!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 20:59:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recurse/m-p/513603#M138389</guid>
      <dc:creator>cellurl</dc:creator>
      <dc:date>2018-11-15T20:59:52Z</dc:date>
    </item>
  </channel>
</rss>

