<?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: Concatinate multiple column labels in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652577#M195936</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21176"&gt;@sasg&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you for the response, but in my actual scenario dataset already exist and labels for var1, var2 are loc1 and loc2..instead of specifying labels is there anyway i can read from dataset itself.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SET statement to use an existing data set instead of datalines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic of the Array assignment, creating the labelvar and Do loop are exactly the same&lt;/P&gt;</description>
    <pubDate>Tue, 02 Jun 2020 15:08:01 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-02T15:08:01Z</dc:date>
    <item>
      <title>Concatinate multiple column labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652381#M195842</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I have a dataset with 140 variables, i need to concatinate all those variables and create a new variable(location) when the value is '1' with their labels.&lt;BR /&gt;Below is the example:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;DATA Have;&lt;BR /&gt;INPUT VAR1 VAR2 VAR3 VAR4;&lt;BR /&gt;label &lt;BR /&gt;VAR1 = "loc1"&lt;BR /&gt;VAR2 = "loc2" &lt;BR /&gt;VAR3 = "loc3"&lt;BR /&gt;VAR4 = "loc4" &lt;BR /&gt;;&lt;BR /&gt;DATALINES;&lt;BR /&gt;0 0 0 0&lt;BR /&gt;0 1 0 0&lt;BR /&gt;1 1 1 1&lt;BR /&gt;1 1 0 1&lt;BR /&gt;1 0 1 0&lt;BR /&gt;0 0 0 1&lt;BR /&gt;1 0 0 0&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;New created column will be like this:&lt;/P&gt;
&lt;P&gt;Location&lt;/P&gt;
&lt;P&gt;loc2&lt;BR /&gt;loc1,loc2,loc3,loc4&lt;BR /&gt;loc1,loc2,loc4&lt;BR /&gt;loc1,loc3&lt;BR /&gt;loc4&lt;BR /&gt;loc1&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Can you please guide me how this can be done efficiently.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;BR /&gt;sasg.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2020 21:43:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652381#M195842</guid>
      <dc:creator>sasg</dc:creator>
      <dc:date>2020-06-01T21:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: Concatinate multiple column labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652383#M195844</link>
      <description>&lt;P&gt;Where does the information that var1=loc1 come from? Is that in another table? Is it systematic somehow or is loc1, loc4 not representative of your real data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2020 21:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652383#M195844</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-06-01T21:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: Concatinate multiple column labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652387#M195845</link>
      <description>&lt;P&gt;Please describe how you intend to display that "label".&lt;/P&gt;
&lt;P&gt;If I understand that you potentially will combine 140 labels that could theoretically be 140*256 characters + 139 separators= 35979 characters. Which exceeds the maximum length of a single variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a way with your example:&lt;/P&gt;
&lt;PRE&gt;DATA Have;
   INPUT VAR1 VAR2 VAR3 VAR4;
   length labelvar $ 100;
   array v var1-var4;
   do i=1 to dim(v);
      if v[i]=1 then labelvar=catx(',',labelvar,strip(vlabel(v[i])));
   end;&lt;BR /&gt;   drop i;
label
VAR1 = "loc1"
VAR2 = "loc2"
VAR3 = "loc3"
VAR4 = "loc4"
;
DATALINES;
0 0 0 0
0 1 0 0
1 1 1 1
1 1 0 1
1 0 1 0
0 0 0 1
1 0 0 0
;&lt;/PRE&gt;
&lt;P&gt;You will need to assign a length to the labelvar long enough to hold the longest expected result.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2020 22:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652387#M195845</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-01T22:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: Concatinate multiple column labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652406#M195856</link>
      <description>Thank you for the response, but in my actual scenario dataset already exist and labels for var1,  var2  are loc1 and loc2..instead of specifying labels is there anyway i can read from dataset itself.</description>
      <pubDate>Tue, 02 Jun 2020 01:51:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652406#M195856</guid>
      <dc:creator>sasg</dc:creator>
      <dc:date>2020-06-02T01:51:33Z</dc:date>
    </item>
    <item>
      <title>Re: Concatinate multiple column labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652410#M195857</link>
      <description>&lt;P&gt;Below some code that dynamically determines the required length for your location variable.&lt;/P&gt;
&lt;P&gt;I've made the assumption that all your variables are of the same data type (either all numeric or all character).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.Have;
  INPUT VAR1 VAR2 VAR3 VAR4;
  label
    VAR1 = "loc1"
    VAR2 = "loc2"
    VAR3 = "loc3"
    VAR4 = "loc4"
  ;
  var5=var4;
  DATALINES;
0 0 0 0
0 1 0 0
1 1 1 1
1 1 0 1
1 0 1 0
0 0 0 1
1 0 0 0
;
&lt;BR /&gt;%let req_len=1;
proc sql noprint;
  select max(1,req_len) into :req_len trimmed
  from
    (
      select sum(ifn(lengthn(label)&amp;gt;0,lengthn(label)+1,0)) as req_len
      from dictionary.columns
      where libname='WORK' and memname='HAVE' and upcase(name) like 'VAR%'
    )
  ;
quit;

data want(drop=_:);
  set have;
  array a_vars {*} var:;
  length Location $&amp;amp;req_len;
  do _i=1 to dim(a_vars);
    if a_vars[_i]=1 and vlabel(a_vars[_i]) ne vname(a_vars[_i]) 
      then location=catx(',',location,vlabel(a_vars[_i]));
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jun 2020 02:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652410#M195857</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-02T02:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: Concatinate multiple column labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652445#M195879</link>
      <description>&lt;P&gt;I would use an array and the VLABEL function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array vars var1-var4;
  length location $200;
  do _N_=1 to dim(vars);
    if vars(_N_)=1 then
      call catx(',',location,vlabel(vars(_N_)));
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jun 2020 07:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652445#M195879</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-02T07:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Concatinate multiple column labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652577#M195936</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21176"&gt;@sasg&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you for the response, but in my actual scenario dataset already exist and labels for var1, var2 are loc1 and loc2..instead of specifying labels is there anyway i can read from dataset itself.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SET statement to use an existing data set instead of datalines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic of the Array assignment, creating the labelvar and Do loop are exactly the same&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2020 15:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatinate-multiple-column-labels/m-p/652577#M195936</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-02T15:08:01Z</dc:date>
    </item>
  </channel>
</rss>

