<?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: read variables from dataset and pass it to a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446183#M111903</link>
    <description>&lt;P&gt;Look up CALL EXECUTE() instead.&lt;/P&gt;
&lt;P&gt;The documentation example is what you're looking to accomplish.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/199490"&gt;@HGimenez&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi everybody!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i want to read rows from a dataset and pass them to a macro. In the macro, I need to create files. This code works OK:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro getAccount (file=, accountID=);&lt;BR /&gt; filename request &amp;amp;file;&lt;BR /&gt; data _null_;&lt;BR /&gt; file request;&lt;BR /&gt; put '&amp;lt;accountID&amp;gt;' &amp;amp;accountID '&amp;lt;/accountID&amp;gt;';&lt;BR /&gt; run;&lt;BR /&gt;%mend getAccount;&lt;BR /&gt;&lt;BR /&gt;OPTIONS MPRINT;&lt;BR /&gt;%getAccount (file='c:\1234.xml', accountID='1234');&lt;BR /&gt;%getAccount (file='c:\5678.xml', accountID='5678');&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when I try to read the variable from a dataset and pass them to the macro, it fails:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA accounts;
    INPUT accountNumber BEST4. fileString $ CHAR12.;
DATALINES4;
1234 c:\1234.xml
5678 c:\5678.xml
;;;;

%macro getAccount (file=, accountID=);
	filename request 'C:\request.xml';
		data _null_;
		file request;
		put '&amp;lt;accountID&amp;gt;' &amp;amp;accountID '&amp;lt;/accountID&amp;gt;';
		run;
%mend getAccount;

OPTIONS MPRINT;
data _null_;
	set accounts;
	accountString = put (accountNumber, 4.);
	%getAccount (file=fileString, accountID=accountString);
	run;
	&lt;/PRE&gt;
&lt;P&gt;any ideas?? Thanks a lot!!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Mar 2018 14:36:48 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-03-16T14:36:48Z</dc:date>
    <item>
      <title>read variables from dataset and pass it to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446181#M111902</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi everybody!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i want to read rows from a dataset and pass them to a macro. In the macro, I need to create files. This code works OK:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro getAccount (file=, accountID=);&lt;BR /&gt; filename request &amp;amp;file;&lt;BR /&gt; data _null_;&lt;BR /&gt; file request;&lt;BR /&gt; put '&amp;lt;accountID&amp;gt;' &amp;amp;accountID '&amp;lt;/accountID&amp;gt;';&lt;BR /&gt; run;&lt;BR /&gt;%mend getAccount;&lt;BR /&gt;&lt;BR /&gt;OPTIONS MPRINT;&lt;BR /&gt;%getAccount (file='c:\1234.xml', accountID='1234');&lt;BR /&gt;%getAccount (file='c:\5678.xml', accountID='5678');&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I try to read the variable from a dataset and pass them to the macro, it fails:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA accounts;
    INPUT accountNumber BEST4. fileString $ CHAR12.;
DATALINES4;
1234 c:\1234.xml
5678 c:\5678.xml
;;;;

%macro getAccount (file=, accountID=);
	filename request 'C:\request.xml';
		data _null_;
		file request;
		put '&amp;lt;accountID&amp;gt;' &amp;amp;accountID '&amp;lt;/accountID&amp;gt;';
		run;
%mend getAccount;

OPTIONS MPRINT;
data _null_;
	set accounts;
	accountString = put (accountNumber, 4.);
	%getAccount (file=fileString, accountID=accountString);
	run;
	&lt;/PRE&gt;&lt;P&gt;any ideas?? Thanks a lot!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2018 14:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446181#M111902</guid>
      <dc:creator>HGimenez</dc:creator>
      <dc:date>2018-03-16T14:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: read variables from dataset and pass it to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446183#M111903</link>
      <description>&lt;P&gt;Look up CALL EXECUTE() instead.&lt;/P&gt;
&lt;P&gt;The documentation example is what you're looking to accomplish.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/199490"&gt;@HGimenez&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi everybody!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i want to read rows from a dataset and pass them to a macro. In the macro, I need to create files. This code works OK:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro getAccount (file=, accountID=);&lt;BR /&gt; filename request &amp;amp;file;&lt;BR /&gt; data _null_;&lt;BR /&gt; file request;&lt;BR /&gt; put '&amp;lt;accountID&amp;gt;' &amp;amp;accountID '&amp;lt;/accountID&amp;gt;';&lt;BR /&gt; run;&lt;BR /&gt;%mend getAccount;&lt;BR /&gt;&lt;BR /&gt;OPTIONS MPRINT;&lt;BR /&gt;%getAccount (file='c:\1234.xml', accountID='1234');&lt;BR /&gt;%getAccount (file='c:\5678.xml', accountID='5678');&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when I try to read the variable from a dataset and pass them to the macro, it fails:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA accounts;
    INPUT accountNumber BEST4. fileString $ CHAR12.;
DATALINES4;
1234 c:\1234.xml
5678 c:\5678.xml
;;;;

%macro getAccount (file=, accountID=);
	filename request 'C:\request.xml';
		data _null_;
		file request;
		put '&amp;lt;accountID&amp;gt;' &amp;amp;accountID '&amp;lt;/accountID&amp;gt;';
		run;
%mend getAccount;

OPTIONS MPRINT;
data _null_;
	set accounts;
	accountString = put (accountNumber, 4.);
	%getAccount (file=fileString, accountID=accountString);
	run;
	&lt;/PRE&gt;
&lt;P&gt;any ideas?? Thanks a lot!!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2018 14:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446183#M111903</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-16T14:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: read variables from dataset and pass it to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446189#M111905</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your macro contains a data step. You are already in a data step when calling it&lt;/P&gt;
&lt;P&gt;so this can't work.&lt;/P&gt;
&lt;P&gt;The "call execute(string)" instruction allow to put the string in argument on a stack&lt;/P&gt;
&lt;P&gt;that will be executed at the end of the data step. This way, you can generate sas programs&lt;/P&gt;
&lt;P&gt;using data from an input dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not tested :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	set accounts;
	accountString = put (accountNumber, 4.);
	call execute(cats('%getAccount (file="',fileString, '", accountID="',accountString,'");'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Mar 2018 14:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446189#M111905</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-03-16T14:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: read variables from dataset and pass it to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446197#M111910</link>
      <description>&lt;P&gt;Untested, but should work ... and much simpler once you have the information in a DATA set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;set accounts;&lt;/P&gt;
&lt;P&gt;file dummy filevar=fileString;&lt;/P&gt;
&lt;P&gt;put '&amp;lt;accountID&amp;gt;' accountNumber '&amp;lt;accountID&amp;gt;';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I couldn't tell if you wanted to adjust the number of blanks before or after AccountNumber, but that part is pretty easy.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2018 14:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446197#M111910</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-16T14:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: read variables from dataset and pass it to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446477#M112021</link>
      <description>&lt;P&gt;Thanks a lot!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a viable solution. However, my actual macro is way longer than the one I posted.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that I put a data _null_ step inside another data _null_, and they don't seem to share variables. Another solution would be to remove the data _null_ from the macro&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your input&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 19:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446477#M112021</guid>
      <dc:creator>HGimenez</dc:creator>
      <dc:date>2018-03-17T19:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: read variables from dataset and pass it to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446479#M112022</link>
      <description>&lt;P&gt;Thanks a lot! Yes, call execute () is the best solution. The reply I marked as solution is based on call execute ()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your help&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 20:02:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446479#M112022</guid>
      <dc:creator>HGimenez</dc:creator>
      <dc:date>2018-03-17T20:02:38Z</dc:date>
    </item>
    <item>
      <title>Re: read variables from dataset and pass it to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446522#M112035</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/199490"&gt;@HGimenez&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks a lot!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a viable solution. However, my actual macro is way longer than the one I posted.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is that I put a data _null_ step inside another data _null_, and they don't seem to share variables. Another solution would be to remove the data _null_ from the macro&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again for your input&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It is not that they don't share variables. It is that they don't overlap in TIME. SAS runs one step at a time.&amp;nbsp; When the compiler see that your code is starting a new step (DATA or PROC statement) then it knows that you have finished defining the previous step and it is time to run it.&amp;nbsp; &amp;nbsp;When you placed your macro call in the middle of a data step it was the same thing as if you placed the code that that macro generated in the middle of the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you write code like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set xxxx ;
  %mymacro;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And your macro generates a PROC step what the SAS compiler sees is:&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;data _null_;
   set xxxx ;
proc ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So it sees the PROC step starting and stops compiling the data step and runs it. Then it compiles the PROC step and runs that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 03:06:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-variables-from-dataset-and-pass-it-to-a-macro/m-p/446522#M112035</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-18T03:06:58Z</dc:date>
    </item>
  </channel>
</rss>

