<?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 how do i get unique value from multiple variables into a macro-variable within one data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698061#M213440</link>
    <description>&lt;P&gt;&lt;BR /&gt;data ex1;&lt;BR /&gt;input var1 $ var2 $ var3 $;&lt;BR /&gt;cards;&lt;BR /&gt;%a b f&lt;BR /&gt;b %c d&lt;BR /&gt;%g f i&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;what i want is MACRvar=%a|%c|%g, i know&amp;nbsp;the transpose+sort nodupkey could achieve this, how to achieve this with a data step like, data _null_; set ex1... i might thought take use of retain, array...&lt;/P&gt;&lt;P&gt;thanks for your helping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for helping&lt;/P&gt;</description>
    <pubDate>Wed, 11 Nov 2020 02:19:47 GMT</pubDate>
    <dc:creator>tri_h</dc:creator>
    <dc:date>2020-11-11T02:19:47Z</dc:date>
    <item>
      <title>how do i get unique value from multiple variables into a macro-variable within one data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698061#M213440</link>
      <description>&lt;P&gt;&lt;BR /&gt;data ex1;&lt;BR /&gt;input var1 $ var2 $ var3 $;&lt;BR /&gt;cards;&lt;BR /&gt;%a b f&lt;BR /&gt;b %c d&lt;BR /&gt;%g f i&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;what i want is MACRvar=%a|%c|%g, i know&amp;nbsp;the transpose+sort nodupkey could achieve this, how to achieve this with a data step like, data _null_; set ex1... i might thought take use of retain, array...&lt;/P&gt;&lt;P&gt;thanks for your helping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for helping&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 02:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698061#M213440</guid>
      <dc:creator>tri_h</dc:creator>
      <dc:date>2020-11-11T02:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get unique value from multiple variables into a macro-variable within one data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698068#M213447</link>
      <description>&lt;P&gt;Declare your new variable (call it mv) as a character variable with a length you know is sufficient.&amp;nbsp; Then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ex1;
input var1 $ var2 $ var3 $;
cards;
%a b f
b %c d
%g f i
run;

data _null_;
  set ex1 end=end_of_ex;
  length mv $24;
  retain mv ;
  if var1=:'%' then mv=catx('|',mv,var1);
  if var2=:'%' then mv=catx('|',mv,var2);
  if var3=:'%' then mv=catx('|',mv,var3);
  if end_of_ex;
  call symput('mvar',mv);
run;

%put %superq(mvar);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I leave it to you to construct the array and corresponding loop.&amp;nbsp; Just note that the "=:" comparison operator is a convenient way to find values starting with a % sign.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note I used the %superq function in the %put statement, because otherwise I would have to use the macrovar&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;&amp;amp;mvar&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;(instead of &lt;EM&gt;&lt;STRONG&gt;mvar&lt;/STRONG&gt;&lt;/EM&gt; in the superq function).&amp;nbsp; This would cause the macro interpreter to attempt to resolve the %a%c%g and generate messages like "WARNING: Apparent invocation of macro A not resolved."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There may be a macro function such that I could use&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %put %somefunction(&amp;amp;mvar)&lt;/P&gt;
&lt;P&gt;without messages generated, but I am unaware of it.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 04:02:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698068#M213447</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-11T04:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get unique value from multiple variables into a macro-variable within one data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698071#M213449</link>
      <description>Sorry, The sample data isn't typical enough, think about:&lt;BR /&gt;data ex1;&lt;BR /&gt;input var1 $ var2 $ var3 $;&lt;BR /&gt;cards;&lt;BR /&gt;%a b f&lt;BR /&gt;b %c d&lt;BR /&gt;%g f %c&lt;BR /&gt;run;&lt;BR /&gt;there's a repeat ‘%c’, and i want to store it once...</description>
      <pubDate>Wed, 11 Nov 2020 04:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698071#M213449</guid>
      <dc:creator>tri_h</dc:creator>
      <dc:date>2020-11-11T04:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get unique value from multiple variables into a macro-variable within one data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698263#M213527</link>
      <description>&lt;P&gt;While we're at it, what order do you want to concatenate the desired values?&amp;nbsp; Order of appearance?&amp;nbsp; Alphabetical?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2020 01:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698263#M213527</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-12T01:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get unique value from multiple variables into a macro-variable within one data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698276#M213532</link>
      <description>Order of appearance would be better, i had tried an index function before the catx like:&lt;BR /&gt;if index(mv, arrayVARs)=0 then mv=catx('|', mv, arrayVARs), but it still add '%c' tiwce.</description>
      <pubDate>Thu, 12 Nov 2020 02:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698276#M213532</guid>
      <dc:creator>tri_h</dc:creator>
      <dc:date>2020-11-12T02:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get unique value from multiple variables into a macro-variable within one data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698296#M213539</link>
      <description>&lt;P&gt;To avoid duplicates, compare the candidate word with the current set of concatenated values using the FINDW function, which returns a 0 if the candidate is not found as a "word" in the concatenation.&amp;nbsp; "word" here means strings separated by '|':&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ex1;
input var1 $ var2 $ var3 $;
cards;
%a b f
b %c d
%g f %c
run;

data _null_;
  set ex1 end=end_of_ex;
  length mv $24;
  retain mv ;
  if var1=:'%' and findw(mv,trim(var1),'|')=0 then mv=catx('|',mv,var1);
  if var2=:'%' and findw(mv,trim(var2),'|')=0 then mv=catx('|',mv,var2);
  if var3=:'%' and findw(mv,trim(var3),'|')=0 then mv=catx('|',mv,var3);
  if end_of_ex;
  call symput('mvar',mv);
run;

%put %superq(mvar);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now it's getting busy enough that you'll want to use an array of var1 &amp;amp; var2 &amp;amp; var3, and program a loop.&amp;nbsp; Note the 2nd argument of the findw function is not var1, but trim(var1) - otherwise any trailing blanks in var1 will be included in the search for a matching string in the MV variable.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2020 04:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-unique-value-from-multiple-variables-into-a-macro/m-p/698296#M213539</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-12T04:29:40Z</dc:date>
    </item>
  </channel>
</rss>

