<?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 can I create a macro/ loop to complete this task? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457261#M284439</link>
    <description>&lt;P&gt;Sorry, your problem here is data modelling and handling.&amp;nbsp; Using macro may work around the solution, but it will be far from ideal.&amp;nbsp; For instance why do you need three sql steps (or in fact how ever many you need).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* set all like data together */
data hl;
  set dim.hl: indsname=tmp;
  fd=substr(indsname,4,6);  /* gets the 201802 part */
run;

proc sql;
  create table want as
  select a.*,
         b.fd as ym 
  from   mylib.detail a
  left join hl b
  on     a.f1=b.f1
  and    strip(put(a.f2,best.))=b.fd;
quit;          &lt;/PRE&gt;
&lt;P&gt;Do you see what I have done here, rather than continuously looping over various datasets, I have put them together, and with a simple bit of logic provided the means to join both datasets together in one step = far less resource intensive, simpler coding, less disk space.&amp;nbsp; If you find your having to write macro, or do lots of the same datasteps, then you can immediately tell that your data modelling is incorrect.&lt;/P&gt;</description>
    <pubDate>Wed, 25 Apr 2018 13:24:56 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-04-25T13:24:56Z</dc:date>
    <item>
      <title>How can I create a macro/ loop to complete this task?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457244#M284436</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;Looking for some assistance in rewriting this code to use a macro and/or do loops as the process is too long &amp;amp; cumbersome.&lt;/P&gt;&lt;P&gt;I'm using SAS EG7&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test_201801 &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f1,f2,f3,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'201801'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; YM&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; mylib.detail a&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;left&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;join&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; dim.hl_20180131 b&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;on&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a.f1=b.f1&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f2=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;201801&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test_201802 &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f1,f2,f3,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'201802'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; YM&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; mylib.detail a&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;left&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;join&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; dim.hl_20180228 b&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;on&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a.f1=b.f1&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f2=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;201802&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test_201803 &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f1,f2,f3,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'201803'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; YM&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; mylib.detail a&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;left&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;join&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; dim.hl_20180331 b&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;on&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a.f1=b.f1&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f2=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;201803&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;proc sql;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;create table final as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;select * from test_201801&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;union&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;select * from test_201802&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;union&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;select * from test_201803;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thank you for your assistance.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Apr 2018 12:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457244#M284436</guid>
      <dc:creator>DeepaG</dc:creator>
      <dc:date>2018-04-25T12:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a macro/ loop to complete this task?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457253#M284437</link>
      <description>&lt;P&gt;check something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Age_dist(Age_all);
%do i=1 %to %SYSFUNC(countw("&amp;amp;age_all"));
%let Age_=%scan("&amp;amp;age_all",&amp;amp;i,"|");
proc sql;
create table test_&amp;amp;Age_ as 
select * 
	from sashelp.class
	where age=&amp;amp;Age_;
quit;
%end;
data final;
set test_:;
run;
%mend Age_dist;
%Age_dist(12|13|14);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Apr 2018 12:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457253#M284437</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-04-25T12:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a macro/ loop to complete this task?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457258#M284438</link>
      <description>&lt;P&gt;Thank you, that will work for me!&lt;/P&gt;</description>
      <pubDate>Wed, 25 Apr 2018 13:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457258#M284438</guid>
      <dc:creator>DeepaG</dc:creator>
      <dc:date>2018-04-25T13:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a macro/ loop to complete this task?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457261#M284439</link>
      <description>&lt;P&gt;Sorry, your problem here is data modelling and handling.&amp;nbsp; Using macro may work around the solution, but it will be far from ideal.&amp;nbsp; For instance why do you need three sql steps (or in fact how ever many you need).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* set all like data together */
data hl;
  set dim.hl: indsname=tmp;
  fd=substr(indsname,4,6);  /* gets the 201802 part */
run;

proc sql;
  create table want as
  select a.*,
         b.fd as ym 
  from   mylib.detail a
  left join hl b
  on     a.f1=b.f1
  and    strip(put(a.f2,best.))=b.fd;
quit;          &lt;/PRE&gt;
&lt;P&gt;Do you see what I have done here, rather than continuously looping over various datasets, I have put them together, and with a simple bit of logic provided the means to join both datasets together in one step = far less resource intensive, simpler coding, less disk space.&amp;nbsp; If you find your having to write macro, or do lots of the same datasteps, then you can immediately tell that your data modelling is incorrect.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Apr 2018 13:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457261#M284439</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-25T13:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a macro/ loop to complete this task?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457371#M284440</link>
      <description>&lt;P&gt;If you don't need those datasets you can try in single process in order creating multiple datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Age_All=12,13,14;

PROC SQL;
create table final as 
select *,put(age,3.) format=$3. as Age_Char
	from sashelp.class
	where age in (&amp;amp;Age_All);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is similar what the other process is doing, if your target is to create only final table and don't need the rest of the tables.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Apr 2018 15:44:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457371#M284440</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-04-25T15:44:50Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a macro/ loop to complete this task?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457642#M284441</link>
      <description>&lt;P&gt;Thanks RW9, I see your point here, macro not really required...&lt;/P&gt;&lt;P&gt;I have implemented your idea into my code, and it does work.&lt;/P&gt;&lt;P&gt;Thanks everyone for the help.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 09:19:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-macro-loop-to-complete-this-task/m-p/457642#M284441</guid>
      <dc:creator>DeepaG</dc:creator>
      <dc:date>2018-04-26T09:19:51Z</dc:date>
    </item>
  </channel>
</rss>

