<?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 Macro to create concatenated columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/932999#M366987</link>
    <description>&lt;P&gt;Does anyone know how I would create a macro that would create new columns that are combinations of every join of existing columns.&amp;nbsp; For example, I have a dataset with column names B, C, D &amp;amp; E.&amp;nbsp; My desired dataset should have those columns, plus the concatenated values for B&amp;amp;C, B&amp;amp;D. B&amp;amp;E, B&amp;amp;C&amp;amp;D, B&amp;amp;D&amp;amp;E, C&amp;amp;D&amp;amp;E, B&amp;amp;C&amp;amp;D&amp;amp;E.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A couple of caveats to this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) It needs to be agnostic of the dataset, so would need to go to the dictionary tables to get the list of variables.&lt;/P&gt;&lt;P&gt;2) There will be variables I wouldn't like to be included in the output other than the individual variable itself.&amp;nbsp; For example, if variables A &amp;amp; F also existed in the dataset, i'd like a way of saying ignore A &amp;amp; F in the macro to concatenate, but still output the value for A &amp;amp; F by themselves on the final dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jun 2024 13:57:37 GMT</pubDate>
    <dc:creator>Jamie_H</dc:creator>
    <dc:date>2024-06-19T13:57:37Z</dc:date>
    <item>
      <title>Macro to create concatenated columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/932999#M366987</link>
      <description>&lt;P&gt;Does anyone know how I would create a macro that would create new columns that are combinations of every join of existing columns.&amp;nbsp; For example, I have a dataset with column names B, C, D &amp;amp; E.&amp;nbsp; My desired dataset should have those columns, plus the concatenated values for B&amp;amp;C, B&amp;amp;D. B&amp;amp;E, B&amp;amp;C&amp;amp;D, B&amp;amp;D&amp;amp;E, C&amp;amp;D&amp;amp;E, B&amp;amp;C&amp;amp;D&amp;amp;E.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A couple of caveats to this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) It needs to be agnostic of the dataset, so would need to go to the dictionary tables to get the list of variables.&lt;/P&gt;&lt;P&gt;2) There will be variables I wouldn't like to be included in the output other than the individual variable itself.&amp;nbsp; For example, if variables A &amp;amp; F also existed in the dataset, i'd like a way of saying ignore A &amp;amp; F in the macro to concatenate, but still output the value for A &amp;amp; F by themselves on the final dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 13:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/932999#M366987</guid>
      <dc:creator>Jamie_H</dc:creator>
      <dc:date>2024-06-19T13:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create concatenated columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/933001#M366988</link>
      <description>I think a 3rd caveat might also be needed for this challenge. Very quickly, the column names will become too large. So that will also need to be considered. Maybe renaming each variable to a simple letter combination and then having a label hold what the new columns is made up from?</description>
      <pubDate>Wed, 19 Jun 2024 14:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/933001#M366988</guid>
      <dc:creator>Jamie_H</dc:creator>
      <dc:date>2024-06-19T14:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create concatenated columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/933011#M366990</link>
      <description>What's the logic that tells you to exclude other combinations such as:&lt;BR /&gt;&lt;BR /&gt;C&amp;amp;D&lt;BR /&gt;C&amp;amp;E&lt;BR /&gt;D&amp;amp;E</description>
      <pubDate>Wed, 19 Jun 2024 14:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/933011#M366990</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-06-19T14:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create concatenated columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/933013#M366991</link>
      <description>&lt;P&gt;You need to explain more what you want to do.&amp;nbsp; For the main part of the logic you can just use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0yx35py6pk47nn1vyrczffzrw25.htm" target="_self"&gt;CALL ALLCOMB() method&lt;/A&gt;.&amp;nbsp; Just a simple extension of the example in the documentation.&lt;/P&gt;
&lt;P&gt;First generate an observation with every possible combination of NVARs.&amp;nbsp; The second step blanks out the names not used for that particular combination.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combo;
   array name[&amp;amp;nvar] $32 (&amp;amp;varlist);
   n=dim(name);
   do k=1 to n;
     ncomb=comb(n, k);
     do j=1 to ncomb;
       call allcomb(j, k, of name[*]);
       output;
     end;
   end;
run;

data combo;
  set combo;
  array name[&amp;amp;nvar];
  do _n_=k+1 to dim(name); name[_n_]=' '; end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Partial results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1718807849880.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97631i33F0839FF2F8C1F9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1718807849880.png" alt="Tom_0-1718807849880.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You could easily get the list of variable names into the macro variable VARLIST from the CONTENTS of the source dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=contents noprint; run;

proc sql noprint;
  select quote(trim(name)) into :varlist separated by ' '
  from contents
  order by varnum
  ;
%let nvar=&amp;amp;sqlobs;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have list of combinations you could now use it to generate code.&lt;/P&gt;
&lt;P&gt;Not sure what you want but perhaps something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set combo;
  file code;
  array name[&amp;amp;nvar] ;
  varname=catx('_','combo',k,j);
  put varname "= catx('|'" @;
  do i=1 to k;
    put ',' name[i] @;
  end;
  put ');' ;
run;

data want;
  set have;
%include code / source2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1718808323705.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97633i695902FFF85F5630/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1718808323705.png" alt="Tom_1-1718808323705.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 14:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-concatenated-columns/m-p/933013#M366991</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-19T14:46:15Z</dc:date>
    </item>
  </channel>
</rss>

