<?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: Executing Macro from Data step iteratively and combing each result in one dataset in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372852#M65189</link>
    <description>&lt;P&gt;1. Don't do it this at, do it all at once as others have indicated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Add a constant prefix so you can append data all at once after.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Add an append step into your macro to append to a master table each time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A good paper to read is Dont be Loopy by David Cassell. It goes over why you shouldn't loop and how to do simulations in SAS.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Jul 2017 20:28:09 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-07-03T20:28:09Z</dc:date>
    <item>
      <title>Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372812#M65180</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Problem Definition:&lt;/STRONG&gt;&lt;/U&gt; In my&amp;nbsp;code, I have a Macro (claims_extraction) which connects to Database (Netezza) and extracts data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I have a list of Drugs, Which I defined as a Macro variable (drug_list). The macro should run for each drug in the list and return one dataset as the output. I have been struggling to combine the dataset created&amp;nbsp;by macro execution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;Here is how my macro looks like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro claims_extraction(dataset,drug_name);
Proc sql;
&amp;amp;DWQUERYCONNECT.;
create table &amp;amp;dataset. as
select * &amp;amp;DWFROMCONNECT. 
(
select QNXT_CLAIM_ID, .......&amp;lt;other variables and table joins&amp;gt;
where upper(drugs.DRUG_NAME) like %UPCASE(&amp;amp;drug_name.)
order by mhn, svc_date;
);
quit;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the drug_list macro variable:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let drug_list = '%Advair%' '%Dulera%' '%Symbicort%' '%Breo%Ellipta%';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am using call execute routine to iteratively execute the macro:&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	do i = 1 to countw("&amp;amp;drug_list",' ');
	 call execute('%claims_extraction(abc,'||scan("&amp;amp;drug_list",i,' ')||');');
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It works all fine without any error. But it creates one dataset for each drug and since I have 'abc' name final argument, each drug overwrites the previous result and end up only with the records for the&amp;nbsp;last drug from the list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Question&lt;/U&gt;&lt;/STRONG&gt;: How can I modify my data step to give me one dataset as result for all the drugs on the list?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Abhi.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 19:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372812#M65180</guid>
      <dc:creator>Abhi_Garg</dc:creator>
      <dc:date>2017-07-03T19:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372816#M65181</link>
      <description>&lt;P&gt;Try change your last part as to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	do i = 1 to countw("&amp;amp;drug_list",' ');
             drug_name = scan("&amp;amp;drug_list",i,' ');
	     call execute('%claims_extraction(abc'||compress(drug_name,"%")||','||drug_name||');');
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then each dataset will be 'abc'||drug_name.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 19:27:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372816#M65181</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-07-03T19:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372822#M65183</link>
      <description>&lt;P&gt;Surely you would be better of running the macro once, hitting the database once, and avoiding the complications of CALL EXECUTE.&amp;nbsp; Why not change the original query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Expand the WHERE conditions so they get all the drugs you want,&lt;/LI&gt;
&lt;LI&gt;Change the ORDER BY so that the drug name is the first item&amp;nbsp; in the list&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;For example just call the macro once without CALL EXECUTE, but change the ending:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let n_drugs = %sysfunc(countw(&amp;amp;drug_list));&lt;/P&gt;
&lt;P&gt;where&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i = 1 %to &amp;amp;n_drugs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %let next_drug = %scan(&amp;amp;DRUG_LIST, &lt;FONT color="#339966"&gt;&amp;amp;i,&lt;/FONT&gt; %str( ));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; upper (drugs.DRUG_NAME) like %UPCASE(&amp;amp;next_drug.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %if &amp;amp;i &amp;lt; &amp;amp;n_drugs %then or;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;order by drug_name, mhn, svc_date;&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, 03 Jul 2017 19:54:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372822#M65183</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-03T19:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372823#M65184</link>
      <description>Nope it won't work. Compress removes the % from the drugs but then the macro variable for data set name resolves to abc'Advair', which is illegal SAS dataset name and hence entire code fails.</description>
      <pubDate>Mon, 03 Jul 2017 19:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372823#M65184</guid>
      <dc:creator>Abhi_Garg</dc:creator>
      <dc:date>2017-07-03T19:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372837#M65185</link>
      <description>&lt;P&gt;You should probably change your process to just query the database once.&lt;/P&gt;
&lt;P&gt;But to your actual question just add PROC APPEND steps to aggregate the results. You might want to make sure the target aggregate file does not exist before starting.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc delete data=want ; run;
data _null_;
  do i = 1 to countw("&amp;amp;drug_list",' ');
    call execute('%claims_extraction(abc,'||scan("&amp;amp;drug_list",i,' ')||');');
    call execute('proc append base=want data=abc force; run;');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jul 2017 19:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372837#M65185</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-03T19:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372839#M65186</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/152205"&gt;@Abhi_Garg&lt;/a&gt; wrote:&lt;BR /&gt;Nope it won't work. Compress removes the % from the drugs but then the macro variable for data set name resolves to abc'Advair', which is illegal SAS dataset name and hence entire code fails.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;then compress the apostoph too:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	do i = 1 to countw("&amp;amp;drug_list",' ');
             drug_name = scan("&amp;amp;drug_list",i,' ');
	     call execute('%claims_extraction(abc'||compress(drug_name,"%'")||','||drug_name||');');
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jul 2017 19:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372839#M65186</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-07-03T19:53:03Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372852#M65189</link>
      <description>&lt;P&gt;1. Don't do it this at, do it all at once as others have indicated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Add a constant prefix so you can append data all at once after.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Add an append step into your macro to append to a master table each time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A good paper to read is Dont be Loopy by David Cassell. It goes over why you shouldn't loop and how to do simulations in SAS.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 20:28:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372852#M65189</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-03T20:28:09Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372866#M65190</link>
      <description>I totally agree that I shouldn't query the database so many times. But I am kinda new to SAS, I am not sure how I can dynamically change my where clause for each of the drug in list. Can you please help me with changing my where clause?</description>
      <pubDate>Mon, 03 Jul 2017 21:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372866#M65190</guid>
      <dc:creator>Abhi_Garg</dc:creator>
      <dc:date>2017-07-03T21:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: Executing Macro from Data step iteratively and combing each result in one dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372867#M65191</link>
      <description>&lt;P&gt;I'm not sure if you're asking me or not ... these message boards don't really show the connections between posts. &amp;nbsp;But if you are asking about my program, it does that automatically. &amp;nbsp;Whatever is part of &amp;amp;DRUG_LIST will be added to the WHERE clause by the posted code.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 21:45:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Executing-Macro-from-Data-step-iteratively-and-combing-each/m-p/372867#M65191</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-03T21:45:29Z</dc:date>
    </item>
  </channel>
</rss>

