<?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 Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/459481#M116719</link>
    <description>&lt;P&gt;Thank you so much all with a very clever answers! I am learning a lot from you. Hope that one day I will be able to answer by myself...lol&lt;/P&gt;</description>
    <pubDate>Wed, 02 May 2018 20:09:48 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2018-05-02T20:09:48Z</dc:date>
    <item>
      <title>Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458960#M116506</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have the following data set with binary fields C1,C2.....C10.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data tbl;&lt;BR /&gt;input ID C1 C2 C3 C4 C5 C6 C7 C8 C9 C10;&lt;BR /&gt;cards;&lt;BR /&gt;111 1 0 0 0 0 0 0 1 1 0&lt;BR /&gt;222 0 0 0 0 0 0 0 0 1 1&lt;BR /&gt;333 0 1 0 0 0 0 0 0 0 0&lt;BR /&gt;444 0 0 0 0 0 0 0 0 0 0&lt;BR /&gt;555 0 0 0 0 0 0 1 1 0 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a new calculated variable that tell us which variables received value 1.&lt;/P&gt;&lt;P&gt;For example :&lt;/P&gt;&lt;P&gt;For ID &amp;nbsp;&lt;SPAN&gt;111 the new variable&amp;nbsp;will receive value &amp;nbsp;C8,C9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For ID &amp;nbsp;222&amp;nbsp;&amp;nbsp;the new variable&amp;nbsp;will receive value &amp;nbsp;C9,C10&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;F&lt;/SPAN&gt;&lt;SPAN&gt;or ID &amp;nbsp;333&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;the new variable&amp;nbsp;will receive value &amp;nbsp;C&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;F&lt;/SPAN&gt;&lt;SPAN&gt;or ID &amp;nbsp;444&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;the new variable&amp;nbsp;will receive value &amp;nbsp;'No_Fail' because &amp;nbsp;none of fields received&amp;nbsp;value 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;F&lt;/SPAN&gt;&lt;SPAN&gt;or ID &amp;nbsp;555&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;the new variable&amp;nbsp;will receive value &amp;nbsp;C7,C8,C10&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Can anyone help to write the code for that?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Koein&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 May 2018 13:04:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458960#M116506</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-05-01T13:04:50Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458963#M116508</link>
      <description>&lt;P&gt;Define an array for c1-c10.&lt;/P&gt;
&lt;P&gt;Set a new variable of sufficient length to an empty string.&lt;/P&gt;
&lt;P&gt;Loop through the array, and if the current member is true (1), use newvar = catx(',',newvar,vname(array{i})); to concatenate its name.&lt;/P&gt;
&lt;P&gt;If newvar is still empty after the loop, set it to 'No_Fail'.&lt;/P&gt;</description>
      <pubDate>Tue, 01 May 2018 13:18:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458963#M116508</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-01T13:18:54Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458965#M116510</link>
      <description>&lt;P&gt;Thanks. Is there another way without working with arrays. I have never did it before&lt;/P&gt;</description>
      <pubDate>Tue, 01 May 2018 13:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458965#M116510</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-05-01T13:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458973#M116512</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks. Is there another way without working with arrays. I have never did it before&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then it's time you start learning the use of arrays. They provide a simple and elegant solution for your problem.&lt;/P&gt;
&lt;P&gt;The whole solution takes just 7 lines of code:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;array definition&lt;/LI&gt;
&lt;LI&gt;initialization of target variable&lt;/LI&gt;
&lt;LI&gt;start of do loop&lt;/LI&gt;
&lt;LI&gt;if array member, set newvar with catx and vname&lt;/LI&gt;
&lt;LI&gt;end of do loop&lt;/LI&gt;
&lt;LI&gt;if newvar is empty, set to No_Fail&lt;/LI&gt;
&lt;LI&gt;drop the iteration counter so it does not mess up the output dataset&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;For a nice paper about arrays, see&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 May 2018 13:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458973#M116512</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-01T13:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458974#M116513</link>
      <description>&lt;P&gt;You can transpose the data and then find all the values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl;
input ID C1 C2 C3 C4 C5 C6 C7 C8 C9 C10;
cards;
111 1 0 0 0 0 0 0 1 1 0
222 0 0 0 0 0 0 0 0 1 1
333 0 1 0 0 0 0 0 0 0 0
444 0 0 0 0 0 0 0 0 0 0
555 0 0 0 0 0 0 1 1 0 1
;
run;

proc transpose data=tbl out=Trans name=Var;
by id;
var c1-c10;
run;

data final(Keep=ID New_Var);
Format New_Var $30.;
do until (last.id);
set Trans(where=(col1=1));
by id;
New_Var=Strip(New_Var)||","||Strip(Var);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the data is already sorted order, I'm not sorting it. You may need to sort your data before PROC TRANSPOSE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 May 2018 13:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458974#M116513</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-05-01T13:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458975#M116514</link>
      <description>&lt;P&gt;Using arrays saves coding.&lt;/P&gt;
&lt;P&gt;Here is the code&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;meant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;It can be done in the same step, as below, or in a separate step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl;
  infile cards;
  input ID C1 C2 C3 C4 C5 C6 C7 C8 C9 C10;
  array cx c1 - c10;   /* c1-c10 is equivalent co c1 c2 ... c10 */
  length new_var $30;  /* 3 chars * 10 vars */&lt;BR /&gt;  new_var = ' ';
  do i=1 to 10;   /* check in a loop */
       if cx(i) = 1 then new_var = catx(',' , new_var, vname(cx(i)) );
  end;
cards;
111 1 0 0 0 0 0 0 1 1 0
222 0 0 0 0 0 0 0 0 1 1
333 0 1 0 0 0 0 0 0 0 0
444 0 0 0 0 0 0 0 0 0 0
555 0 0 0 0 0 0 1 1 0 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Compare that code to the alternative, as a separate step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new_tbl;
  set tbl;
        length new_var $30;
        new_var = ' ';
        if c1 = 1 then new_var = 'C1';
        if c2 = 1 then new_var = catx(',' , new_var, 'C2');
        if c3 = 1 then new_var = catx(',' , new_var, 'C3');
        if c4 = 1 then new_var = catx(',' , new_var, 'C4');
        .....  /* i'm lazy to write the in between lines */
        if c10 = 1 then new_var = catx(',' , new_var, 'C10');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 May 2018 13:54:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458975#M116514</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-05-01T13:54:19Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458981#M116518</link>
      <description>&lt;P&gt;Another alternate solution,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl;
input ID C1 C2 C3 C4 C5 C6 C7 C8 C9 C10;
cards;
111 1 0 0 0 0 0 0 1 1 0
222 0 0 0 0 0 0 0 0 1 1
333 0 1 0 0 0 0 0 0 0 0
444 0 0 0 0 0 0 0 0 0 0
555 0 0 0 0 0 0 1 1 0 1
;
run;

proc transpose data=tbl out=Trans name=Var Prefix=Binary_;
by id;
var c1-c10;
run;
proc sort data=trans;
by id Binary_1;
proc transpose data=trans out=Trans2(drop=_label_ _name_) ;
by id Binary_1;
var var;
run;

DATA want(keep=id New_Var);
set trans2;
New_Var=CATX(",",of Col1-Col10);
if Binary_1=1;/* You can change this to 0 also */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 May 2018 14:19:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/458981#M116518</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-05-01T14:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/459481#M116719</link>
      <description>&lt;P&gt;Thank you so much all with a very clever answers! I am learning a lot from you. Hope that one day I will be able to answer by myself...lol&lt;/P&gt;</description>
      <pubDate>Wed, 02 May 2018 20:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate/m-p/459481#M116719</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-05-02T20:09:48Z</dc:date>
    </item>
  </channel>
</rss>

