<?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: If has a certain string, get the value from a macro variable whose name contains the same string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335493#M75948</link>
    <description>&lt;P&gt;Sounds like you are looking for something like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro test;
    %do _j=1 %to 5;
           data test_&amp;amp;_j;
           set train_&amp;amp;_j;
	
           Target = symget(catt(scan(Variable, 1,'_'),"_",scan(Variable, 2,'_')));
           run;		

	%end;
%mend test;
%test
&lt;/PRE&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Fri, 24 Feb 2017 03:28:55 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-02-24T03:28:55Z</dc:date>
    <item>
      <title>If has a certain string, get the value from a macro variable whose name contains the same string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335476#M75943</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* If Variable contains a certain string,&lt;BR /&gt; &lt;BR /&gt;give Target the same value as the macro variable (whose name has the same string) has */&lt;BR /&gt; &lt;BR /&gt;%let A_value = 1;
%let B_value = 2;
...
%let Z_value = 26;

%let A_count = 1.1;
%let B_count = 2.2;
...
%let Z_count = 26.26;


%macro test;
    %do _j=1 %to 5;
           data test_&amp;amp;_j;
           set train_&amp;amp;_j;
	
           if find(Variable, 'A') ge 1 then Target = &amp;amp;A_value;
           if find(Variable, 'B') ge 1 then Target = &amp;amp;B_value;
           ...
           if find(Variable, 'Z') ge 1 then Target = &amp;amp;Z_value;   

           run;		

	%end;
%mend test;

%test
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, the codes above are not efficient but hardcoded. Is there a more generic approach for the same purpose?&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 01:45:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335476#M75943</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-02-24T01:45:21Z</dc:date>
    </item>
    <item>
      <title>Re: If has a certain string, get the value from a macro variable whose name contains the same string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335479#M75944</link>
      <description>&lt;P&gt;I don't completely understand what you're trying to do, but if the letter values A-Z contain 1-26, and always will, this&amp;nbsp;&lt;EM&gt;should&lt;/EM&gt; work (since we don't have access to your&amp;nbsp;&lt;EM&gt;train_&lt;/EM&gt; datasets. (Assuming you're using ASCII - different values for EBCDIC!)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
%do _j = 1 %to 1;
data test_&amp;amp;_j;
set train_&amp;amp;_j;
do i = 65 to 90;
   if not find(variable, byte(i)) then
      continue;
   target = i - 64;
   leave;
   end;
run;
%mend test;

%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;65 to 90 are the ASCII values for A-Z;&amp;nbsp;&lt;EM&gt;byte(65)&lt;/EM&gt; returns&amp;nbsp;&lt;EM&gt;A&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;indexc&lt;/EM&gt; will work as well as&amp;nbsp;&lt;EM&gt;find&lt;/EM&gt;, here.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 02:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335479#M75944</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-24T02:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: If has a certain string, get the value from a macro variable whose name contains the same string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335480#M75945</link>
      <description>&lt;P&gt;What do the values of variable look like and what is the variable's length?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 02:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335480#M75945</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-24T02:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: If has a certain string, get the value from a macro variable whose name contains the same string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335489#M75946</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Laurie, thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So basically there is one column in the&amp;nbsp;original dataset, named 'Variable'. It has different character values, e.g. Apple, Banana, Carrot. Several macro variables have been created, e.g. Apple_MV (has a value of 100, numeric), Banana_MV (has a value of 200, numeric).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to add a new column to the original dataset, named 'Target'. For each row, if it is 'Apple' in Column 1, then the value in Column 2 is 100 (because Apple_MV = 100). if 'Banana' in Column 1, then the value in Column 2 is 200 (because Banana_MV = 200).&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 03:07:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335489#M75946</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-02-24T03:07:01Z</dc:date>
    </item>
    <item>
      <title>Re: If has a certain string, get the value from a macro variable whose name contains the same string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335490#M75947</link>
      <description>&lt;P&gt;So the values of column 1, Variable, are just simple English words, e.g. Apple, Banana. The type of this column is Character, and length (in bytes) is 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The macro variables could have names like 'Apple_value', 'Banana_value', 'Apple_count', and we are only getting the values from macro variables whose name ends with '_value' instead of '_count' here.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 03:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335490#M75947</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-02-24T03:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: If has a certain string, get the value from a macro variable whose name contains the same string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335493#M75948</link>
      <description>&lt;P&gt;Sounds like you are looking for something like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro test;
    %do _j=1 %to 5;
           data test_&amp;amp;_j;
           set train_&amp;amp;_j;
	
           Target = symget(catt(scan(Variable, 1,'_'),"_",scan(Variable, 2,'_')));
           run;		

	%end;
%mend test;
%test
&lt;/PRE&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 03:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335493#M75948</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-24T03:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: If has a certain string, get the value from a macro variable whose name contains the same string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335506#M75951</link>
      <description>It worked! Such beautiful.</description>
      <pubDate>Fri, 24 Feb 2017 05:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-has-a-certain-string-get-the-value-from-a-macro-variable/m-p/335506#M75951</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-02-24T05:06:29Z</dc:date>
    </item>
  </channel>
</rss>

