<?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: Looping and defining macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261140#M50756</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79805"&gt;@buechler66&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;sorry, I had just updated my original post to make it more clear. &amp;nbsp;What are your thoughts on then referencing the macro variable for each record in the next dataset?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What are you actually going to do? You example data step code will result in an empty dataset after two interations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you provide some details on what you are attempting it may be that what you are looking at may be accomplished using Call Execute as it is very good way to use a control data set containg variable values that need to be used in other code.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Apr 2016 17:20:22 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-04-04T17:20:22Z</dc:date>
    <item>
      <title>Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261116#M50745</link>
      <description>&lt;P&gt;I have dataset Rules with the variable Rule. &amp;nbsp;I need to loop thru each record and make Rule a macro variable to be substituted for a variable&amp;nbsp;in my next datastep. &amp;nbsp;The number of records in Rules may vary day-to-day. Is there a best way to do this? &amp;nbsp;It's some type of nested loop I think.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;* For each record in Rules create a macro variable... ;&lt;/P&gt;&lt;P&gt;data Rules;&lt;BR /&gt;set Rules;&lt;BR /&gt;CALL SYMPUT(rule, rule);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;* Then how do I reference it for each record in my next dataset??? ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data&amp;nbsp;MyRuleQuery;&lt;BR /&gt;set MyRuleQuery;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if MyRuleQuery_Name = &amp;amp;rule;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 16:35:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261116#M50745</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-04-04T16:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261117#M50746</link>
      <description>&lt;P&gt;Proc SQL is probably your best bet.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/67912/HTML/default/viewer.htm#n1y2jszlvs4hugn14nooftfrxhp3.htm" target="_self"&gt;See the documentation&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A useful feature in newer versions of SAS, means that you don't need to specify the number of macro variables, it creates the amount required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;

select name into :name1-
from sashelp.class;
quit;

%let num_obs = &amp;amp;sqlobs;

%put &amp;amp;name1.;
%put &amp;amp;name19.;

%put &amp;amp;num_obs;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 16:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261117#M50746</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-04T16:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261118#M50747</link>
      <description>&lt;P&gt;sorry, I had just updated my original post to make it more clear. &amp;nbsp;What are your thoughts on then referencing the macro variable for each record in the next dataset?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 16:16:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261118#M50747</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-04-04T16:16:00Z</dc:date>
    </item>
    <item>
      <title>Re: Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261120#M50749</link>
      <description>&lt;P&gt;Look at the documentation link. I would create a single macro variable that held all the rules, if it fit. You have a limit of 32k or 65k characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise you may want to create a macro to display the rules rather than macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you have to nest it in a macro and loop over the values, partly why I created a macrovariable to hold the number of rules.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to &amp;amp;num_obs;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;amp;&amp;amp;&amp;amp;rule&amp;amp;i.
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 16:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261120#M50749</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-04T16:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261136#M50754</link>
      <description>&lt;P&gt;Still processing your question, but it seems to me your final outcome will only need an inner SQL join? From the information I have registered, I fail to see the&amp;nbsp;necessity of using macro variable. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 17:16:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261136#M50754</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-04T17:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261140#M50756</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79805"&gt;@buechler66&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;sorry, I had just updated my original post to make it more clear. &amp;nbsp;What are your thoughts on then referencing the macro variable for each record in the next dataset?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What are you actually going to do? You example data step code will result in an empty dataset after two interations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you provide some details on what you are attempting it may be that what you are looking at may be accomplished using Call Execute as it is very good way to use a control data set containg variable values that need to be used in other code.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 17:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261140#M50756</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-04T17:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261150#M50762</link>
      <description>&lt;P&gt;Ok, let me try to be more specific. I have two Proc SQL steps. &amp;nbsp;In the first, the QueryRules dataset has records with differenct 'Where' conditions (or snippets of SQL code) stored in the Rule variable. &amp;nbsp;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NVL(A.PREP_TYPE_CODE,-1) &amp;lt;&amp;gt; NVL(B.PREP_TYPE_CODE,-1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I'm wanting to define a macro variable for Rule in the first Proc SQL and then substitute that macro value into the Where condition of my second Proc SQL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I need to run the second Proc SQL for each record in the first Proc SQL substituting in the Rule value. Ulitimately I'd like to append all the second Proc SQL query results into one large dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this makes better sense.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;connect to oracle as db (user="me" password="mepw" path="medb");&lt;BR /&gt;create table QueryRules as&lt;/P&gt;&lt;P&gt;select * from connection to db&lt;BR /&gt;( select analysis_desc, rule, rule_order&lt;BR /&gt;into :anal_desc, :rule, :rule_order&lt;BR /&gt;from ivprl.analysis_ctrl&lt;BR /&gt;where trunc(upload_create_dt) = trunc(sysdate)&lt;BR /&gt;order by rule_order desc&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;disconnect from db;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;connect to oracle as db (user="me" password="mepw" path="medb");&lt;BR /&gt;create table MyRuleQuery as&lt;BR /&gt;select * from connection to db&lt;BR /&gt;( select &amp;amp;anal_desc as RULE_NM,&lt;BR /&gt;b.actual_dlvry_date as AD_DT,&lt;BR /&gt;b.imb_code,&lt;BR /&gt;&amp;amp;rule_order as rule_order&lt;BR /&gt;from ivprl.bi_spm_piece_iv_recon a, ivprl.bi_spm_piece_bids_recon b&lt;BR /&gt;where trunc(a.upload_create_dt) = trunc(sysdate)&lt;BR /&gt;and trunc(b.upload_create_dt) = trunc(sysdate)&lt;BR /&gt;and a.imb_code = b.imb_code&lt;BR /&gt;and a.imb_dlvry_zip_3 in(select imb_dlvry_zip_3 from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.ml_cl_code in(select ml_cl_code from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.ml_cat_code in(select ml_cat_code from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.imb_code not in(select imb_code from MyRuleQueryData where rule_order &amp;lt; 999.1)&lt;BR /&gt;&lt;STRONG&gt;and &amp;amp;rule&lt;/STRONG&gt;&lt;BR /&gt;);&lt;BR /&gt;disconnect from db;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 18:12:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261150#M50762</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-04-04T18:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: Looping and defining macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261204#M50777</link>
      <description>&lt;P&gt;Ok, I figured it out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;connect to oracle as db (user="ssbuechl" password="sha215rpe" path="ivasprd");&lt;BR /&gt;create table QueryRules as&lt;/P&gt;&lt;P&gt;select * from connection to db&lt;BR /&gt;( select analysis_desc, rule, rule_order&lt;BR /&gt;from ivprl.analysis_ctrl&lt;BR /&gt;where trunc(upload_create_dt) = trunc(sysdate)&lt;BR /&gt;and rule_order in(337,6)&lt;BR /&gt;order by rule_order desc&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;disconnect from db;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro BuildQueryData(analysis_desc= , rule= , rule_order= );&lt;BR /&gt;proc sql;&lt;BR /&gt;connect to oracle as db (user="ssbuechl" password="sha215rpe" path="ivasprd");&lt;BR /&gt;create table MyRuleQuery as&lt;BR /&gt;select * from connection to db&lt;BR /&gt;( select '&amp;amp;analysis_desc' as RULE_NM, b.actual_dlvry_date as AD_DT, b.imb_code, &amp;amp;rule_order as rule_order&lt;BR /&gt;from ivprl.bi_spm_piece_iv_recon a, ivprl.bi_spm_piece_bids_recon b&lt;BR /&gt;where trunc(a.upload_create_dt) = trunc(sysdate)&lt;BR /&gt;and trunc(b.upload_create_dt) = trunc(sysdate)&lt;BR /&gt;and a.imb_code = b.imb_code&lt;BR /&gt;and a.imb_dlvry_zip_3 in(select imb_dlvry_zip_3 from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.ml_cl_code in(select ml_cl_code from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.ml_cat_code in(select ml_cat_code from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.imb_code not in(select imb_code from MyRuleQueryData where rule_order &amp;lt; 999.1)&lt;BR /&gt;and &amp;amp;rule&lt;BR /&gt;);&lt;BR /&gt;disconnect from db;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;proc append base=FinalData data = MyRuleQuery;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%mend BuildQueryData;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;set QueryRules;&lt;BR /&gt;call execute('%BuildQueryData(analysis_desc='||analysis_desc||' , rule='||rule||' , rule_order='||rule_order||');');&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 19:32:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-and-defining-macro-variable/m-p/261204#M50777</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-04-04T19:32:55Z</dc:date>
    </item>
  </channel>
</rss>

