<?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: how to refer macro variable name or value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946535#M370666</link>
    <description>dose that mean "&amp;amp;colvar.=A1" and "&amp;amp;colvar. in (1,2)" are correct way to refer a macro variable name and value, but they should not be used in the same step using and to connect the conditions?</description>
    <pubDate>Mon, 07 Oct 2024 14:09:47 GMT</pubDate>
    <dc:creator>stataq</dc:creator>
    <dc:date>2024-10-07T14:09:47Z</dc:date>
    <item>
      <title>how to refer macro variable name or value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946533#M370664</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I refer a macro variable and its value in a data step?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &amp;amp;colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if &amp;amp;colvar.=A1 and &amp;amp;colvar. in (1,2) then do;...&lt;/PRE&gt;
&lt;P&gt;how to refer a macro variable name, and how to refer a value that belong to a variable?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 13:52:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946533#M370664</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-10-07T13:52:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to refer macro variable name or value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946534#M370665</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I refer a macro variable and its value in a data step?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &amp;amp;colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if &amp;amp;colvar.=A1 and &amp;amp;colvar. in (1,2) then do;...&lt;/PRE&gt;
&lt;P&gt;how to refer a macro variable name, and how to refer a value that belong to a variable?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If I understand your question correctly, you simply code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if symget('colvar'='A1') then if &amp;amp;colvar. in (1,2) then do;...&lt;/PRE&gt;
&lt;P&gt;Note that all values of &amp;amp;colvar must exist as variables in the data set since there's code to test them, or SAS wil complain.&lt;/P&gt;
&lt;P&gt;Otherwise a better method would be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%if &amp;amp;colvar.=A1 %then %do;&lt;BR /&gt;  if &amp;amp;colvar. in (1,2) then do;...end;&lt;BR /&gt;%end;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Here, only the appropriate test is inserted in the data step.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 14:02:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946534#M370665</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2024-10-07T14:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to refer macro variable name or value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946535#M370666</link>
      <description>dose that mean "&amp;amp;colvar.=A1" and "&amp;amp;colvar. in (1,2)" are correct way to refer a macro variable name and value, but they should not be used in the same step using and to connect the conditions?</description>
      <pubDate>Mon, 07 Oct 2024 14:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946535#M370666</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-10-07T14:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to refer macro variable name or value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946536#M370667</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I refer a macro variable and its value in a data step?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &amp;amp;colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if &amp;amp;colvar.=A1 and &amp;amp;colvar. in (1,2) then do;...&lt;/PRE&gt;
&lt;P&gt;how to refer a macro variable name, and how to refer a value that belong to a variable?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please remember that when you run your SAS code, macro variables are replaced by their values. So when you run the code above, it actually becomes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if A1=A1 and A1 in (1,2) then do; ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which doesn't seem like a sensible thing to do, because A1 is always equal to A1. It sounds like you want, if I am understanding you correctly, to test to see if macro variable &amp;amp;colvar is equal to A1 and then test if data set variable A1 is in (1,2). If that's correct then the second bit of code from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;is what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;dose that mean "&amp;amp;colvar.=A1" and "&amp;amp;colvar. in (1,2)" are correct way to refer a macro variable name and value, but they should not be used in the same step using and to connect the conditions?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that it seems like you are using %IF and IF interchangeably in your code and in your thinking. They are not interchangeable. %IF will only test value of macro variables (%IF cannot test values of data set variables) and IF tests values of data set variables or constants.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 14:12:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946536#M370667</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-10-07T14:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: how to refer macro variable name or value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946540#M370670</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;dose that mean "&amp;amp;colvar.=A1" and "&amp;amp;colvar. in (1,2)" are correct way to refer a macro variable name and value, but they should not be used in the same step using and to connect the conditions?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;"&amp;amp;colvar.=A1" and "&amp;amp;colvar. in (1,2)"&lt;/P&gt;
&lt;P&gt;are obviously 2 different ways to test since you're testing against different values (A1 and 1,2)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first test would typically be macro language test (where I used &lt;CODE class=" language-sas"&gt;%if&lt;/CODE&gt;) to&lt;U&gt; know the value given to the macro variable&lt;/U&gt;, while the second test tests a data set variable whose name is &amp;amp;colvar, i.e A1 for example (and &lt;CODE class=" language-sas"&gt;if&lt;/CODE&gt; is used)&lt;U&gt; to know the value of the data set variable&lt;/U&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 14:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946540#M370670</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2024-10-07T14:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: how to refer macro variable name or value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946542#M370671</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I refer a macro variable and its value in a data step?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &amp;amp;colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if &amp;amp;colvar.=A1 and &amp;amp;colvar. in (1,2) then do;...&lt;/PRE&gt;
&lt;P&gt;how to refer a macro variable name, and how to refer a value that belong to a variable?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;FILTER to do what?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What SAS code do you want to run when COLVAR is A1?&lt;/P&gt;
&lt;P&gt;What SAS code do you want to run when COLVAR is something else?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If by filter you mean subset then perhaps what you want to do is conditionally generate a WHERE statement?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
%if %qupcase(&amp;amp;colvar)=A1 %then %do;
  where A1 in (1,2) ;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that WANT is either a full copy of HAVE or a subset of HAVE with just the observations that have a value of A1 that is either 1 or 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to test the value of a macro variable in SAS code then you can either convert the value to a string literal by enclosing it quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if upcase("&amp;amp;colvar")='A1' then ....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or use the SYMGET() function to retrieve its value (which avoids issues when the value contains double quote characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if upcase(symget('colvar'))='A1' then ....&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2024 14:51:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946542#M370671</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-07T14:51:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to refer macro variable name or value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946544#M370672</link>
      <description>&lt;P&gt;Can you post a small example, showing what you're trying to do.&amp;nbsp; Just a dataset with one or two variables and five rows would suffice.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And also show the DATA step code you want to generate, and the macro language code.&amp;nbsp; If you're building a macro, you want to start by writing the data step code without macro language elements, then you can use the macro language to generate that data step code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 14:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-refer-macro-variable-name-or-value/m-p/946544#M370672</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-10-07T14:53:04Z</dc:date>
    </item>
  </channel>
</rss>

