<?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 concatenate multiple columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250146#M268720</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set that I'd like to have a few column concatenated into one colum. The data set and variables are like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(10 variables)&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;department item cat1 cat2 cat3 cat4 cat5 nt1 nt2 nt3&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;aaa &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;v &amp;nbsp; &amp;nbsp;1a &amp;nbsp; 2a &amp;nbsp;&amp;nbsp;3a &amp;nbsp; 4a &amp;nbsp; 5a &amp;nbsp; 1a &amp;nbsp;2a &amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;bbb &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;w&lt;SPAN&gt; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;1b&lt;SPAN&gt; &amp;nbsp;&lt;/SPAN&gt; 2b &amp;nbsp;&amp;nbsp;3b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1b &amp;nbsp;2b &amp;nbsp;3b&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ccc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x&lt;SPAN&gt; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;1c &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;2c&lt;SPAN&gt; &amp;nbsp;&lt;/SPAN&gt; 3c &amp;nbsp; 4c &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1c &amp;nbsp;2c &amp;nbsp;3c&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ddd &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y&lt;SPAN&gt; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;1d &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;2d &amp;nbsp; 3d &amp;nbsp; 4d &amp;nbsp; 5d &amp;nbsp; 1d &amp;nbsp;2d &amp;nbsp;3d&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN style="line-height: normal;"&gt;The end table should look like this (3 variables):&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;department item newvar&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;aaa &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;v &amp;nbsp; &amp;nbsp;1a, 2a, 3a, 4a, 5a, 1a, 2a&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;bbb &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;w &amp;nbsp; &amp;nbsp;1b, 2b, 3b, 1b, 2b, 3b&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ccc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x &amp;nbsp; &amp;nbsp;1c, 2c, 3c, 4c, 1c, 2c, 3c&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ddd &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y &amp;nbsp; &amp;nbsp;1d, 2d, 3d, 4d, 5d, 1d, 2d, 3d&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN style="line-height: normal;"&gt;These cat and nt variables are obtained by joining two tables (catTable and ntTable) so we have the option of working with one table at a time and concatenating twice, but the number of cat and nt variables can vary at any time so concatenating needs to be dynamic.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN style="line-height: normal;"&gt;I thought about using this SAS option to create a macro but didn't know how to make the macro variable work. Thanks in advance for your help.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dsid1 = open("catTable");
dsid2 = open("ntTable");
&amp;amp;num_cat = attrn(dsid1,"NVARS")-2;
&amp;amp;num_nt = attrn(dsid2,"NVARS")-2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Feb 2016 19:24:41 GMT</pubDate>
    <dc:creator>mhouse</dc:creator>
    <dc:date>2016-02-15T19:24:41Z</dc:date>
    <item>
      <title>concatenate multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250146#M268720</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set that I'd like to have a few column concatenated into one colum. The data set and variables are like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(10 variables)&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;department item cat1 cat2 cat3 cat4 cat5 nt1 nt2 nt3&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;aaa &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;v &amp;nbsp; &amp;nbsp;1a &amp;nbsp; 2a &amp;nbsp;&amp;nbsp;3a &amp;nbsp; 4a &amp;nbsp; 5a &amp;nbsp; 1a &amp;nbsp;2a &amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;bbb &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;w&lt;SPAN&gt; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;1b&lt;SPAN&gt; &amp;nbsp;&lt;/SPAN&gt; 2b &amp;nbsp;&amp;nbsp;3b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1b &amp;nbsp;2b &amp;nbsp;3b&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ccc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x&lt;SPAN&gt; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;1c &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;2c&lt;SPAN&gt; &amp;nbsp;&lt;/SPAN&gt; 3c &amp;nbsp; 4c &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1c &amp;nbsp;2c &amp;nbsp;3c&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ddd &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y&lt;SPAN&gt; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;1d &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;2d &amp;nbsp; 3d &amp;nbsp; 4d &amp;nbsp; 5d &amp;nbsp; 1d &amp;nbsp;2d &amp;nbsp;3d&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN style="line-height: normal;"&gt;The end table should look like this (3 variables):&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;department item newvar&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;aaa &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;v &amp;nbsp; &amp;nbsp;1a, 2a, 3a, 4a, 5a, 1a, 2a&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;bbb &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;w &amp;nbsp; &amp;nbsp;1b, 2b, 3b, 1b, 2b, 3b&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ccc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x &amp;nbsp; &amp;nbsp;1c, 2c, 3c, 4c, 1c, 2c, 3c&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ddd &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y &amp;nbsp; &amp;nbsp;1d, 2d, 3d, 4d, 5d, 1d, 2d, 3d&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN style="line-height: normal;"&gt;These cat and nt variables are obtained by joining two tables (catTable and ntTable) so we have the option of working with one table at a time and concatenating twice, but the number of cat and nt variables can vary at any time so concatenating needs to be dynamic.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN style="line-height: normal;"&gt;I thought about using this SAS option to create a macro but didn't know how to make the macro variable work. Thanks in advance for your help.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dsid1 = open("catTable");
dsid2 = open("ntTable");
&amp;amp;num_cat = attrn(dsid1,"NVARS")-2;
&amp;amp;num_nt = attrn(dsid2,"NVARS")-2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 19:24:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250146#M268720</guid>
      <dc:creator>mhouse</dc:creator>
      <dc:date>2016-02-15T19:24:41Z</dc:date>
    </item>
    <item>
      <title>Re: concatenate multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250148#M268721</link>
      <description>&lt;P&gt;Declare an array that contains the max amount of variables then use CATX. You appear to be filling in missing values? You'll have to account for that logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array vars(*) cat1-cat&amp;amp;num_cat nt1-nt&amp;amp;num_nt;

new_var = catx(",", of vars(*));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 19:27:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250148#M268721</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-15T19:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: concatenate multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250159#M268722</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18748"&gt;@mhouse﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the "&lt;SPAN&gt;cat and nt variables" have these numbered names, you could try the second of these two steps:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (department item cat1 cat2 cat3 cat4 cat5 nt1 nt2 nt3) (:$3.);
cards;
aaa        v    1a   2a   3a   4a   5a   1a  2a  .
bbb        w    1b   2b   3b    .    .   1b  2b  3b
ccc        x    1c   2c   3c   4c    .   1c  2c  3c
ddd        y    1d   2d   3d   4d   5d   1d  2d  3d
;

data want;
set have;
length newvar $100; /* Please adapt length as appropriate. */
newvar=catx(', ', of cat:, of nt:);
drop cat: nt:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Feb 2016 19:48:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250159#M268722</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-02-15T19:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: concatenate multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250188#M268723</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh﻿&lt;/a&gt;! I didn't know how to make the macro variables work without writing a full macro but the second solution worked for my purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again,&lt;/P&gt;
&lt;P&gt;mhouse&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 20:51:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/concatenate-multiple-columns/m-p/250188#M268723</guid>
      <dc:creator>mhouse</dc:creator>
      <dc:date>2016-02-15T20:51:58Z</dc:date>
    </item>
  </channel>
</rss>

