<?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 columns names when value=1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760213#M240367</link>
    <description>&lt;P&gt;First, get data out of structure by transposing, and eliminate the 0 values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
  data=have
  out=long1 (where=(col1 ne 0))
;
by id;
var ind:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next get the value out of _name_, and drop the now redundant col1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long2;
set long;
length ind $3;
ind = substr(_name_,4);
drop _name_ col1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then concatenate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wanr;
set long2;
by id;
length value $100;
if first.id then value = "";
value = catx(',',value,put(ind,$fff.));
if last.id;
keep id value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS your format needs to be a character format.&lt;/P&gt;</description>
    <pubDate>Sun, 08 Aug 2021 07:23:30 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-08-08T07:23:30Z</dc:date>
    <item>
      <title>Concatenate columns names when value=1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760209#M240364</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the way to&amp;nbsp;&amp;nbsp;concatenate all columns names when value=1?&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;I want to do it only for columns that start with name "Ind".&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;I want to have a dynamic&amp;nbsp;program&amp;nbsp; because the number of columns (columns start with Ind) might be changed.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;In this example the wanted new column called "New" will get the following values:&lt;/P&gt;
&lt;P&gt;For ID=1&amp;nbsp; the value will be&amp;nbsp; 315,527&lt;/P&gt;
&lt;P&gt;For ID=2&amp;nbsp; the value will be&amp;nbsp; 118,315&lt;/P&gt;
&lt;P&gt;For ID=3&amp;nbsp; the value will be&amp;nbsp;118,224,315,527&lt;/P&gt;
&lt;P&gt;For ID=4&amp;nbsp; the value will be&amp;nbsp; 527&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also want to create another new columns called "New2" that will have the concatenate descriptions (using proc format).&lt;/P&gt;
&lt;P&gt;proc format ;&lt;/P&gt;
&lt;P&gt;value fff&lt;/P&gt;
&lt;P&gt;118='Reason_A'&lt;/P&gt;
&lt;P&gt;224='Reason_B'&lt;/P&gt;
&lt;P&gt;315='Reason_C'&lt;/P&gt;
&lt;P&gt;527='Reason_D'&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;Run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For ID=1&amp;nbsp; the value will be&amp;nbsp; 'Reason_C,Reason_D'&lt;BR /&gt;For ID=2&amp;nbsp; the value will be&amp;nbsp; 'Reason_A,Reason_C'&lt;BR /&gt;For ID=3&amp;nbsp; the value will be&amp;nbsp; 'Reason_A,Reason_B,Reason_C,Reason_D'&lt;BR /&gt;For ID=4&amp;nbsp; the value will be ' Reason_D'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID Ind118 Ind224 Ind315 Ind527 ;
cards;
1 0 0 1 1 
2 1 0 1 0
3 1 1 1 1 
4 0 1 0 0
5 0 0 0 1
;
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>Sun, 08 Aug 2021 06:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760209#M240364</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-08-08T06:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate columns names when value=1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760213#M240367</link>
      <description>&lt;P&gt;First, get data out of structure by transposing, and eliminate the 0 values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
  data=have
  out=long1 (where=(col1 ne 0))
;
by id;
var ind:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next get the value out of _name_, and drop the now redundant col1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long2;
set long;
length ind $3;
ind = substr(_name_,4);
drop _name_ col1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then concatenate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wanr;
set long2;
by id;
length value $100;
if first.id then value = "";
value = catx(',',value,put(ind,$fff.));
if last.id;
keep id value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS your format needs to be a character format.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Aug 2021 07:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760213#M240367</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-08-08T07:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate columns names when value=1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760244#M240378</link>
      <description>&lt;P&gt;Don't store the REASON into the NAME of the variable.&amp;nbsp; Store it as its own variable.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall;
  input ID @ ;
  do reason=118,224,315,527 ;
    input value @;
    output;
  end;
cards;
1 0 0 1 1 
2 1 0 1 0
3 1 1 1 1 
4 0 1 0 0
5 0 0 0 1
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the problem is simple.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
  value fff
    118='Reason_A'
    224='Reason_B'
    315='Reason_C'
    527='Reason_D'
  ;
run;

data want;
  do until(last.id);
    set tall ;
    by id;
    length new new2 $200;
    if value then do;
       new=catx(',',new,reason);
       new2=catx(',',new2,put(reason,fff.));
    end;
  end;
  drop reason value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    ID    new                new2

 1      1    315,527            Reason_C,Reason_D
 2      2    118,315            Reason_A,Reason_C
 3      3    118,224,315,527    Reason_A,Reason_B,Reason_C,Reason_D
 4      4    224                Reason_B
 5      5    527                Reason_D
&lt;/PRE&gt;
&lt;P&gt;If you keep it in the "wide" format then you will need convert the text from the name into the numeric reason to use the format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array ind ind:;
  length new new2 $200;
  do index=1 to dim(ind);
    if ind[index] then do;
      reason=input(substr(vname(ind[index]),4),32.);
      new=catx(',',new,reason);
      new2=catx(',',new2,put(reason,fff.));
    end;
  end;
  drop index reason;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Aug 2021 15:37:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760244#M240378</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-08T15:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate columns names when value=1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760272#M240389</link>
      <description>&lt;P&gt;I think the key issue here, is the extracting the numeric part of the variable name - i.e. the numeric characters following IND in the variable name.&amp;nbsp; You can use the VNAME function to save those characters in a retained array, which you need to do only once, during the first observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then just retrieve those characters when the corresponding IND variable =1.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Ind118 Ind224 Ind315 Ind527 ;
cards;
1 0 0 1 1 
2 1 0 1 0
3 1 1 1 1 
4 0 1 0 0
5 0 0 0 1
run;

proc format;
  value $fff
    '118'='Reason_A'
    '224'='Reason_B'
    '315'='Reason_C'
    '527'='Reason_D'
    '999'='Other Reason'
    ;
run;

data want (drop=_:);
  set have;
  array _vals {10} $3 ;
  retain _vals: ; /*Numeric characters from the IND varnames */
  array _ind ind: ;
  if _n_=1 then do over _ind;
    _vals{_i_} = left(tranwrd(upcase(vname(_ind)),'IND',''));
  end;
  length codes $15 ;
  length reason $100;
  do over _ind;
    if _ind=1 then do;
      codes=catx(',',codes,_vals{_i_});
      reason=catx(',',reason,put(_vals{_i_},$fff.));
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Be sure to make the _VALS array large enough to accommodate the maximum number of expected IND variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Aug 2021 19:23:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760272#M240389</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-08-08T19:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate columns names when value=1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760279#M240395</link>
      <description>&lt;P&gt;If you are going to use implicit array reference then why not go all in and use it everywhere.&lt;/P&gt;
&lt;P&gt;(Also why not use the numeric format the original poster created instead of having to make a new character format?).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value fff
    118='Reason_A'
    224='Reason_B'
    315='Reason_C'
    527='Reason_D'
    999='Other Reason'
  ;
run;

data want ;
  set have;
  array reason reason1-reason100;
  retain reason: ;
  array ind ind: ;
  if _n_=1 then do over ind;
    reason = input(substr(vname(ind),4),32.);
  end;
  length new new2 $200;
  do over ind;
    if ind then do;
      new=catx(',',new,reason);
      new2=catx(',',new2,put(reason,fff.));
    end;
  end;
  drop reason: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Aug 2021 22:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-columns-names-when-value-1/m-p/760279#M240395</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-08T22:03:43Z</dc:date>
    </item>
  </channel>
</rss>

