<?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: call execute with macro as an argument in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300614#M63507</link>
    <description>&lt;P&gt;When SAS is parsing the code the character string %DATASETS does NOT trigger a macro call because it is enclosed in single quotes. &amp;nbsp;When the data step is running this code snippet&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;'%datasets(name='||strip(name)||');'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will generate the charater strings like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%datasets(name=Alfred);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will be passed&amp;nbsp;to CALL EXECUTE. to be parsed. Since SAS can now see the %DATASETS it will run the macro and store the generated code into the execution stack to run AFTER the data step stops. &amp;nbsp;You can tell because the lines in the SAS log that begin with + that show the code that CALL EXECUTE generated you will see the actual code the macro generates but you will&amp;nbsp;NOT see the macro call itself.&amp;nbsp;This can cause timing issue, especially if the macro code generates and uses macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can fix this by using the %NRSTR() macro quoting function in your original data step code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;'%nrstr(%datasets)(name='||strip(name)||');'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To see the difference try runing this simple example.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro datasets(name); %unquote(* NAME = &amp;amp;name ;) %mend datasets;
data _null_;
  set sashelp.class (obs=2);
  call execute('%datasets(name='||strip(name)||');');
  call execute('%nrstr(%datasets)(name='||strip(name)||');');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Results:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1    + * NAME = Alfred ;;
2    + %datasets(name=Alfred);
3    + * NAME = Alice ;;
4    + %datasets(name=Alice);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 25 Sep 2016 14:23:05 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2016-09-25T14:23:05Z</dc:date>
    <item>
      <title>call execute with macro as an argument</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300566#M63484</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the following code, can we say macro %datasets is enclosed in single quotes or three parts &amp;nbsp;ie. '&lt;SPAN&gt;%datasets(name= ',&amp;nbsp;strip(name), and &amp;nbsp;');' &amp;nbsp;are concatenated by ||. &amp;nbsp;Which one is correct?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set sashelp.class;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;call execute('%datasets(name='||strip(name)||');');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below example is taken from Carpenter's macro book where author has mentioned "Because the %PRTDSN is enclosed in single quotes, the text is not seen as macro reference when data step is compiled".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data &amp;nbsp;_null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set macro3.dbdir;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;call execute ('%prtdsn(dsn='||dsn||')');&lt;/P&gt;&lt;P&gt;run;&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;</description>
      <pubDate>Sat, 24 Sep 2016 23:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300566#M63484</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2016-09-24T23:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: call execute with macro as an argument</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300568#M63485</link>
      <description>&lt;P&gt;If it's not in double quotes SAS sees it as a string. The code above generates a string that is passed to call execute. In this case that string ends up resolving to a macro call but it certainly doesn't have to.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Sep 2016 23:21:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300568#M63485</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-24T23:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: call execute with macro as an argument</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300569#M63486</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp; For me, it is easier to understand as three strings concatenated by || as the argument of the call execute has to be string. But I might be misunderstanding it.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Sep 2016 23:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300569#M63486</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2016-09-24T23:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: call execute with macro as an argument</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300573#M63487</link>
      <description>&lt;P&gt;That's correct, but String + string = String&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2016 00:04:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300573#M63487</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-25T00:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: call execute with macro as an argument</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300578#M63489</link>
      <description>&lt;P&gt;You can use whatever wording you are comfortable with, but the process is this. &amp;nbsp;The DATA step puts together all the strings into a single string, before CALL EXECUTE sees it. &amp;nbsp;CALL EXECUTE sees just one string. &amp;nbsp;In a way, you can reason this out for yourself if you consider that CALL EXECUTE would not be expected to parse ||.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note, while Carpenter's book is correct, the execution phase of the DATA step does recognize the macro call (even if compilation does not). &amp;nbsp;CALL EXECUTE will execute macro language statements as soon as possible, without necessarily waiting for the current DATA step to finish. &amp;nbsp;It is a very tricky issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2016 01:44:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300578#M63489</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-25T01:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: call execute with macro as an argument</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300583#M63492</link>
      <description>&lt;P&gt;Simplify your code by being more explicit :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    set sashelp.class;
    macroCall = cats('%datasets(name=', name, ');');
    put macroCall= ; /* for debug */
    call execute(macroCall);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2016 02:22:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300583#M63492</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-09-25T02:22:18Z</dc:date>
    </item>
    <item>
      <title>Re: call execute with macro as an argument</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300614#M63507</link>
      <description>&lt;P&gt;When SAS is parsing the code the character string %DATASETS does NOT trigger a macro call because it is enclosed in single quotes. &amp;nbsp;When the data step is running this code snippet&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;'%datasets(name='||strip(name)||');'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will generate the charater strings like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%datasets(name=Alfred);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will be passed&amp;nbsp;to CALL EXECUTE. to be parsed. Since SAS can now see the %DATASETS it will run the macro and store the generated code into the execution stack to run AFTER the data step stops. &amp;nbsp;You can tell because the lines in the SAS log that begin with + that show the code that CALL EXECUTE generated you will see the actual code the macro generates but you will&amp;nbsp;NOT see the macro call itself.&amp;nbsp;This can cause timing issue, especially if the macro code generates and uses macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can fix this by using the %NRSTR() macro quoting function in your original data step code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;'%nrstr(%datasets)(name='||strip(name)||');'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To see the difference try runing this simple example.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro datasets(name); %unquote(* NAME = &amp;amp;name ;) %mend datasets;
data _null_;
  set sashelp.class (obs=2);
  call execute('%datasets(name='||strip(name)||');');
  call execute('%nrstr(%datasets)(name='||strip(name)||');');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Results:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1    + * NAME = Alfred ;;
2    + %datasets(name=Alfred);
3    + * NAME = Alice ;;
4    + %datasets(name=Alice);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2016 14:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-execute-with-macro-as-an-argument/m-p/300614#M63507</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-09-25T14:23:05Z</dc:date>
    </item>
  </channel>
</rss>

