<?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 within a datastep? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838890#M331694</link>
    <description>&lt;P&gt;Looks like you just want to SET those two dataset together.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.key);
    set havea haveb ;
    by key;
    length sum_k 8 concat_text $100 ;
    sum_k=sum(sum_k,k);
    concat_text=catx('/',concat_text,text);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    key    k     text     sum_k    concat_text

 1      1     1    US0003      3      US0001/US0002/US0003
 2      2     1    US0006      3      US0004/US0005/US0006
 3      3     1    US0005      2      US0001/US0005
 4      4     1    US0006      2      US0002/US0006
 5      5     1    US0003      1      US0003
&lt;/PRE&gt;</description>
    <pubDate>Mon, 17 Oct 2022 03:11:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-10-17T03:11:41Z</dc:date>
    <item>
      <title>Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838745#M331630</link>
      <description>&lt;P&gt;Esteemed Advisers:&lt;/P&gt;
&lt;P&gt;I'm stuck on a problem of how to concatentate text variable within a datastep.&lt;/P&gt;
&lt;P&gt;Below is a simplified version of the problem and my failed attempt to solve it.&lt;/P&gt;
&lt;P&gt;This is what I hope to achieve:&lt;/P&gt;
&lt;P&gt;data want&lt;BR /&gt;key k concat_text&lt;BR /&gt;1 3 US0001/US0002/US0003&lt;BR /&gt;2 3 US0004/US0005/US0006&lt;BR /&gt;3 2 US0001/US0005&lt;BR /&gt;4 2 US0002/US0006&lt;BR /&gt;5 1 US0003&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HaveA;
infile datalines delimiter=',';
length key 8. k 8.;
input key k;
datalines;
1,0
2,0
3,0
4,0
5,0
;
run;

data HaveB;
infile datalines delimiter=',';
length key 8. k 8. text $6;
input key k text;
datalines;
1,1,US0001
1,1,US0002
1,1,US0003
2,1,US0004
2,1,US0005
2,1,US0006
3,1,US0001
3,1,US0005
4,1,US0002
4,1,US0006
5,1,US0003
;
run;

data want (keep=key k concat_text);
update havea (in=a) haveb(in=b rename=k=kb);
length concat_text $100;
by key;
retain concat_text;
if a and b then do
concat_text=trim(left(concat_text))||trim(left(text));
k=k+kb;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Clearly I'm using the RETAIN statment incorrectly.&amp;nbsp; And I don't know how to introduce the delimiter in the concatenated variable.&amp;nbsp; Thanks in advance for your help with this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gene&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2022 22:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838745#M331630</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2022-10-14T22:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838755#M331632</link>
      <description>&lt;P&gt;You are using an UPDATE statement.&amp;nbsp; Do you know how that works?&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2022 22:53:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838755#M331632</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-10-14T22:53:18Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838765#M331640</link>
      <description>&lt;P&gt;Yes. I've used Update before.&amp;nbsp; I think it suits my needs.&amp;nbsp; And I think I know how it works.&amp;nbsp; But if there's a better or different way, I'm open to other approaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gene&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2022 01:39:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838765#M331640</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2022-10-15T01:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838783#M331651</link>
      <description>&lt;P&gt;Use a MERGE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=key k concat_text);
merge
  havea (in=a rename=(k=ka))
  haveb (in=b rename=(k=kb))
;
length k 8 concat_text $100;
by key;
if a and b;
retain concat_text k;
if first.key
then do;
  concat_text = text;
  k = ka;
end;
else concat_text = catx(",",concat_text,text);
k + kb;
if last.key;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Oct 2022 05:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838783#M331651</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-15T05:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838785#M331653</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299716"&gt;@genemroz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yes. I've used Update before.&amp;nbsp; I think it suits my needs.&amp;nbsp; And I think I know how it works.&amp;nbsp; But if there's a better or different way, I'm open to other approaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gene&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Update works for this case. BUT&lt;/P&gt;
&lt;P&gt;1) you need to reset the accumulated text for a new key&lt;/P&gt;
&lt;P&gt;2) and Catx will place the / and strip any leading/trailing spaces in the concatenation.&lt;/P&gt;
&lt;PRE&gt;data want (keep=key k concat_text);
   update havea (in=a) haveb(in=b rename=k=kb);
   length concat_text $100;
   by key;
   retain concat_text;
   &lt;STRONG&gt;if first.key then call missing(concat_text);&lt;/STRONG&gt;
   if a and b then do
         concat_text=&lt;STRONG&gt;catx('/',concat_text,text)&lt;/STRONG&gt;;
   k=k+kb;
end;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Oct 2022 05:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838785#M331653</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-10-15T05:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838819#M331662</link>
      <description>Both solutions worked but I'm marking this as "accepted" because it works within the context of using the UPDATE statement.  Many thanks to both Advisers for their time and effort to help me with this.</description>
      <pubDate>Sat, 15 Oct 2022 14:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838819#M331662</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2022-10-15T14:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838889#M331693</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Why would you use update when you have NO variables to update?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Your base dataset only has two variables, one of which is&amp;nbsp; the ID variable.&amp;nbsp; So only variable that could be updated is K.&lt;/P&gt;
&lt;P&gt;Your transaction dataset has three variables, but after you rename K to KB then NONE of them will UDPATE the one non ID variable in the base dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you are essentially just doing a MERGE with an implied IF LAST.KEY statement at the end.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2022 03:07:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838889#M331693</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-17T03:07:07Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838890#M331694</link>
      <description>&lt;P&gt;Looks like you just want to SET those two dataset together.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.key);
    set havea haveb ;
    by key;
    length sum_k 8 concat_text $100 ;
    sum_k=sum(sum_k,k);
    concat_text=catx('/',concat_text,text);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    key    k     text     sum_k    concat_text

 1      1     1    US0003      3      US0001/US0002/US0003
 2      2     1    US0006      3      US0004/US0005/US0006
 3      3     1    US0005      2      US0001/US0005
 4      4     1    US0006      2      US0002/US0006
 5      5     1    US0003      1      US0003
&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Oct 2022 03:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838890#M331694</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-17T03:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating within a datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838996#M331729</link>
      <description>Thanks for this solution.  It shows that, together with the other two proposed solutions, there are often multiple possible solutions with SAS for any given problem.  &lt;BR /&gt;Regards,&lt;BR /&gt;Gene</description>
      <pubDate>Mon, 17 Oct 2022 14:58:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-within-a-datastep/m-p/838996#M331729</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2022-10-17T14:58:18Z</dc:date>
    </item>
  </channel>
</rss>

