<?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 resolve a macro variable to a range or list of values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505949#M135554</link>
    <description>&lt;P&gt;You have several issues here.&amp;nbsp; For starters, you have an extra set of parentheses ... one on the definition of &amp;amp;BIRTHRANGE, and another following the IN operator.&amp;nbsp; Regardless, IN does not work on a set of expressions, only on a list of hard-coded values.&amp;nbsp; And %EVAL does not execute in the middle of a DATA step.&amp;nbsp; It only executes ahead of time on the original value of &amp;amp;I to determine what statements belong in the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Luckily, you can avoid all of these issues by avoiding macro language entirely.&amp;nbsp; Here is the approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data tt;&lt;/P&gt;
&lt;P&gt;set t;&lt;/P&gt;
&lt;P&gt;i=1;&lt;/P&gt;
&lt;P&gt;array infect {2};&lt;/P&gt;
&lt;P&gt;array dates {2} var_dt vax_dt;&lt;/P&gt;
&lt;P&gt;if var=1 then do k=1 to dim(dates);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if (birth_dt &amp;lt;= dates{k} &amp;lt;= birth_dt + 1) then infect{k}=1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop k;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's easily expandable if you are actually dealing with more variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Oct 2018 14:35:02 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-10-19T14:35:02Z</dc:date>
    <item>
      <title>How to resolve a macro variable to a range or list of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505925#M135542</link>
      <description>&lt;P&gt;I'm not sure if I've worded the question correctly, but what I'm trying to do is use the in() operator to compare the var_dt to the birth_dt or the day after.&amp;nbsp; I think it's mainly a problem with not using actual values with the in() operator, but I'm not sure how to get the birth_dt and&amp;nbsp;&lt;SPAN&gt;birth_dt+1 to be resolved/evaluated as their internal values.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm guessing there needs to be something added to or changed regarding the %let statement, but any suggestions would be appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options minoperator mindelimiter=',';
data tt; set t;
%macro inf;
%let birthrange = (birth_dt, birth_dt+1);
%let i=1;
if (var=1 and var_dt in(&amp;amp;birthrange) then do; infect&amp;amp;i.=101; i=%eval(&amp;amp;i.+1); end;
if (vax=1 and vax_dt in(&amp;amp;birthrange) then do; infect&amp;amp;i.=102; i=%eval(&amp;amp;i.+1); end;
%mend;
%inf;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using SAS 9.4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 14:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505925#M135542</guid>
      <dc:creator>moreka</dc:creator>
      <dc:date>2018-10-19T14:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve a macro variable to a range or list of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505943#M135552</link>
      <description>&lt;P&gt;What is it your actually trying to achieve.&amp;nbsp; test data, required output etc.&amp;nbsp; The code you present is not a good method of working.&amp;nbsp; Never create macros within other stesp for instance, its just very messy.&amp;nbsp; Define macro variables at the top of a program outside the other code for readability.&lt;/P&gt;
&lt;P&gt;You also cannot mix datastep and macro language like that.&amp;nbsp; Macro is nothing more than a find replace text mechanism which generates code, it cannot interact with the compiled datastep language as it does not exist at that point.&amp;nbsp; So where is birthdt coming from, the dataset.&amp;nbsp; Then use datastep language:&lt;/P&gt;
&lt;PRE&gt;data tt; 
  set t;
  array infect{2} 8.;
  do i=birth_dt to birth_dt+1;
    if var=1 and var_dt in (i) then infect{i}=101; 
    if vax=1 and vax_dt in(i) then infect{i}=102;
  end;
run; &lt;/PRE&gt;
&lt;P&gt;Do note, its far simpler for you to post working test data, then show what you want than for us to try to guess.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 14:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505943#M135552</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-19T14:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve a macro variable to a range or list of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505949#M135554</link>
      <description>&lt;P&gt;You have several issues here.&amp;nbsp; For starters, you have an extra set of parentheses ... one on the definition of &amp;amp;BIRTHRANGE, and another following the IN operator.&amp;nbsp; Regardless, IN does not work on a set of expressions, only on a list of hard-coded values.&amp;nbsp; And %EVAL does not execute in the middle of a DATA step.&amp;nbsp; It only executes ahead of time on the original value of &amp;amp;I to determine what statements belong in the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Luckily, you can avoid all of these issues by avoiding macro language entirely.&amp;nbsp; Here is the approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data tt;&lt;/P&gt;
&lt;P&gt;set t;&lt;/P&gt;
&lt;P&gt;i=1;&lt;/P&gt;
&lt;P&gt;array infect {2};&lt;/P&gt;
&lt;P&gt;array dates {2} var_dt vax_dt;&lt;/P&gt;
&lt;P&gt;if var=1 then do k=1 to dim(dates);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if (birth_dt &amp;lt;= dates{k} &amp;lt;= birth_dt + 1) then infect{k}=1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop k;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's easily expandable if you are actually dealing with more variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 14:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505949#M135554</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-19T14:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve a macro variable to a range or list of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505954#M135559</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46780"&gt;@moreka&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm not sure if I've worded the question correctly, but what I'm trying to do is use the in() operator to compare the var_dt to the birth_dt or the day after.&amp;nbsp; I think it's mainly a problem with not using actual values with the in() operator, but I'm not sure how to get the birth_dt and&amp;nbsp;&lt;SPAN&gt;birth_dt+1 to be resolved/evaluated as their internal values.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm guessing there needs to be something added to or changed regarding the %let statement, but any suggestions would be appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options minoperator mindelimiter=',';
data tt; set t;
%macro inf;
%let birthrange = (birth_dt, birth_dt+1);
%let i=1;
if (var=1 and var_dt in(&amp;amp;birthrange) then do; infect&amp;amp;i.=101; i=%eval(&amp;amp;i.+1); end;
if (vax=1 and vax_dt in(&amp;amp;birthrange) then do; infect&amp;amp;i.=102; i=%eval(&amp;amp;i.+1); end;
%mend;
%inf;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using SAS 9.4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Assuming that birth_dt is an actual date value and in the data set&amp;nbsp;then the comparison :&lt;/P&gt;
&lt;PRE&gt;if (var=1 and (birth_dt le var_dt le (birth_dt + 1))   ) then ...

&lt;/PRE&gt;
&lt;P&gt;should work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 14:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-a-macro-variable-to-a-range-or-list-of-values/m-p/505954#M135559</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-19T14:42:49Z</dc:date>
    </item>
  </channel>
</rss>

