<?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: Macro in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779389#M38254</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 



proc sql;

create table calc2 as

select DATE,
/*add in month for aggregates*/
put(date, yymmn6.) as month,

/*this variable should have a name*/
'00'n as randomVariable,

default,

total,


sum(default) as total_def,

default / sum(default) as dist_def

from calc1
/*group for each month*/
group by calculated month
/*all dates after june 2020*/
where date &amp;gt;= '01Jun2020'd;
;

quit;

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 09 Nov 2021 17:49:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-11-09T17:49:14Z</dc:date>
    <item>
      <title>Macro</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779350#M38246</link>
      <description>&lt;P&gt;I need to create in a table the results below for each month. Should I make a macro: Like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; dt = ("202006");&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#000080"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT face="Courier New" color="#000080"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;create&lt;/FONT&gt; &lt;FONT face="Courier New" color="#0000ff"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; calc2 &lt;/FONT&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; DATE,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#800080"&gt;'00'n&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;default,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;total,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt; sum(default) &lt;/FONT&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; total_def,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt; default / sum(default) &lt;/FONT&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; dist_def&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; calc1&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; put(DATE,&lt;/FONT&gt;&lt;FONT face="Courier New" color="#08726d"&gt;yymmn6.&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;) = &amp;amp;&lt;/FONT&gt;&lt;FONT face="Courier New" color="#08726d"&gt;dt.&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#000080"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Nov 2021 14:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779350#M38246</guid>
      <dc:creator>Thalitacosta</dc:creator>
      <dc:date>2021-11-09T14:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779354#M38247</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Making a macro that is looping over the months (and then appending all results) seems very sub-optimal to me.&lt;/P&gt;
&lt;P&gt;For every macro run / call, you are making another pass through the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you tell us how your input dataset is structured and what you exactly want to achieve, we can help you doing all this in one data step (for all months!).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Tue, 09 Nov 2021 15:10:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779354#M38247</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-09T15:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779389#M38254</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 



proc sql;

create table calc2 as

select DATE,
/*add in month for aggregates*/
put(date, yymmn6.) as month,

/*this variable should have a name*/
'00'n as randomVariable,

default,

total,


sum(default) as total_def,

default / sum(default) as dist_def

from calc1
/*group for each month*/
group by calculated month
/*all dates after june 2020*/
where date &amp;gt;= '01Jun2020'd;
;

quit;

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Nov 2021 17:49:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779389#M38254</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-09T17:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779390#M38255</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/391297"&gt;@Thalitacosta&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need to create in a table the results below for each month. Should I make a macro: Like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; dt = ("202006");&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#000080"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT face="Courier New" color="#000080"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;create&lt;/FONT&gt; &lt;FONT face="Courier New" color="#0000ff"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; calc2 &lt;/FONT&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; DATE,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#800080"&gt;'00'n&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;default,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;total,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt; sum(default) &lt;/FONT&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; total_def,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt; default / sum(default) &lt;/FONT&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; dist_def&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; calc1&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#0000ff"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; put(DATE,&lt;/FONT&gt;&lt;FONT face="Courier New" color="#08726d"&gt;yymmn6.&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;) = &amp;amp;&lt;/FONT&gt;&lt;FONT face="Courier New" color="#08726d"&gt;dt.&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#000080"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This will never work. When you say &lt;FONT face="courier new,courier"&gt;%let dt=("202006");&lt;/FONT&gt; this means that the value of macro variable &amp;amp;DT has parentheses and double quotes and digits. It will never match a DATE variable, because the parentheses and double quotes prevent any matching. It is not the same as if you use &lt;FONT face="courier new,courier"&gt;%let dt=202006&lt;/FONT&gt;; but even this won't match true date variables without extra work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I said in your earlier thread, you have to talk to SAS in the language it understands, and dates are the number of days since 01JAN1960. SAS doesn't understand anything else. So your WHERE statement should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where DATE between '01JUN2020'd and '31JUN2020'd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;because SAS understands '01JUN2020'd and '31JUN2020'd, it converts '01JUN2020'd to the number of days between 01JAN1960 and 01JUN2020 which is something SAS then can work with. It doesn't understand 202006 without you doing extra work. And it doesn't understand ("202006").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need a macro variable DT that changes you can do something like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dt=202006;
%let month=%sysfunc(inputn(&amp;amp;dt,yymmn6.));

/* Most of your PROC SQL goes here and then */
where intnx('month',date,0,'b')=&amp;amp;month;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Lastly, agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;, this type of looping that you are asking for is a poor programming practice in most cases, and very inefficient, but we don't really know what you are trying to do.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Nov 2021 18:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Macro/m-p/779390#M38255</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-09T18:05:20Z</dc:date>
    </item>
  </channel>
</rss>

