<?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: SAS doesn't return expected table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889260#M351319</link>
    <description>&lt;P&gt;You moved the QUIT statement before the %END.&amp;nbsp; It needs to be after the %END, as in Tom's post. As you have it, the QUIT executes after the first INSERT statement, and ends the PROC SQL step.&amp;nbsp; Then the second INSERT statement is an error, because it's not part of a PROC SQL step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also have a typo&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(Id) as Memebers  /*should be members*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I don't think the typo matters.&lt;/P&gt;</description>
    <pubDate>Mon, 14 Aug 2023 20:55:46 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-08-14T20:55:46Z</dc:date>
    <item>
      <title>SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889207#M351298</link>
      <description>&lt;DIV&gt;%LET StartDate = '01Jul2021'd;&lt;/DIV&gt;&lt;DIV&gt;%Let EndDate = '01Jun2023'd;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%macro Members(start,end);&lt;/DIV&gt;&lt;DIV&gt;%let dif=%sysfunc(intck(month,&amp;amp;start,&amp;amp;end));&lt;/DIV&gt;&lt;DIV&gt;%do i=0 %to &amp;amp;dif;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%let date=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i,e));&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;proc sql;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;create table Contacts as&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;select&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;put(&amp;amp;date., monyy7.) as Date,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;count(Id) as Members&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from Contact&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%end;&lt;/DIV&gt;&lt;DIV&gt;%mend Members;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%Members(&amp;amp;StartDate., &amp;amp;EndDate.)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I expect SAS to return 23 rows for 23 months from Jul2021 to Jun2023. However, it returned only 1 row of Jun2023 and I'm not sure why.&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 17:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889207#M351298</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-14T17:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889209#M351300</link>
      <description>&lt;P&gt;You told it to create a table named CONTACTS.&lt;/P&gt;
&lt;P&gt;If you do that 23 times only the last version of it will exist since the previous versions will have been replaced.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But since the table you are generating does not vary based on the date why both making the other 22 anyway?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are you really trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 17:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889209#M351300</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-14T17:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889210#M351301</link>
      <description>&lt;P&gt;Thanks for your input. I'm trying to create a table&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&amp;nbsp; Members&lt;/P&gt;&lt;P&gt;--------------------&lt;/P&gt;&lt;P&gt;Jul2021&amp;nbsp; &amp;nbsp; | x&lt;/P&gt;&lt;P&gt;Aug2021&amp;nbsp; | y&lt;/P&gt;&lt;P&gt;Sep2021&amp;nbsp; | z&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;Jun2023 | w&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 17:12:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889210#M351301</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-14T17:12:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889212#M351302</link>
      <description>&lt;P&gt;i just realized I missed a where condition that makes output dependent on the date&lt;BR /&gt;&lt;BR /&gt;%LET StartDate = '01Jul2021'd;&lt;BR /&gt;%Let EndDate = '01Jun2023'd;&lt;BR /&gt;&lt;BR /&gt;%macro Members(start,end);&lt;BR /&gt;%let dif=%sysfunc(intck(month,&amp;amp;start,&amp;amp;end));&lt;BR /&gt;%do i=0 %to &amp;amp;dif;&lt;BR /&gt;%let date=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i,e));&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table Contacts as&lt;BR /&gt;select&lt;BR /&gt;put(&amp;amp;date., monyy7.) as Date,&lt;BR /&gt;count(Id) as Members&lt;BR /&gt;from Contact&lt;BR /&gt;where CreatedDate &amp;lt;=&amp;amp;Date.&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend Members;&lt;BR /&gt;&lt;BR /&gt;%Members(&amp;amp;StartDate., &amp;amp;EndDate.)&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 17:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889212#M351302</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-14T17:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889213#M351303</link>
      <description>&lt;P&gt;First, you have not explained what calculations you are trying to do. So explain what you are trying to do, without talking about SAS code, in words.&amp;nbsp;In your table above, you don't say how to compute w x y and z. Are they simply counts? You haven't stated that, and we shouldn't have to guess.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macros are not a good choice to replace BY group processing, which seems like what you are trying to do (again, I'm guessing) and PROC SQL is not a good choice in loops to replace PROC FREQ.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems as if you are counting the number of ID values, PROC FREQ would be the way to go. But it's really not clear what else you are doing here, as we don't have data set CONTACT and you are not counting BY month in any way in your code. Maybe something like this (but this is a guess until you provide a portion of your data set named CONTACT):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let startdate = '01jul2021'd;
%let enddate = '01jun2023'd;
proc freq data=contact(where=(&amp;amp;startdate&amp;lt;=date&amp;lt;=&amp;amp;enddate));
    tables date;
    format date monyy7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, no macros needed, and no looping needed, and no SQL needed either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 19:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889213#M351303</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-14T19:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889214#M351304</link>
      <description>&lt;P&gt;So make the dataset once and then INSERT the new observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Members(start,end);
%local i date ;

data Contacts;
  length DATE $7 Members 8;
  stop;
run;

proc sql noprint;
%do i=0 %to %sysfunc(intck(month,&amp;amp;start,&amp;amp;end));
  %let date=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i,e));
insert into Contacts(Date,Members)
select put(&amp;amp;date., monyy7.) as Date
     , count(Id) as Members
from Contact
where CreatedDate &amp;lt;=&amp;amp;Date.
;
%end;
quit;

%mend Members;


%LET StartDate = '01Jul2021'd;
%Let EndDate = '01Jun2023'd;
%Members(&amp;amp;StartDate., &amp;amp;EndDate.)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Aug 2023 18:00:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889214#M351304</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-14T18:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889224#M351305</link>
      <description>&lt;P&gt;That's a good idea. I tried that but SAS threw an error after inserting the first row of Jul2021:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"ERROR 180-322: Statement is not valid or it is used out of proper order."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know what caused the error?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 18:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889224#M351305</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-14T18:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889226#M351306</link>
      <description>&lt;P&gt;You can't just show us error messages, and not the code that caused the error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show us the LOG for this step (every line in the log for this step, not partial logs for this step). Please copy the log as text and then paste it into the window that appears when you click on the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PaigeMiller_0-1663012019648.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75161i0E71B1489A6C9839/image-size/large?v=v2&amp;amp;px=999" role="button" title="PaigeMiller_0-1663012019648.png" alt="PaigeMiller_0-1663012019648.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also please show us a portion of the data in data set CONTACT.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 18:28:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889226#M351306</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-14T18:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889256#M351316</link>
      <description>&lt;P&gt;Sorry my first day here so I'm still learning things. Here is the code I ran using what Tom suggested [I'll post the log shortly. I can't seem to include it here]&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%LET StartDate = '01Jul2021'd;
%Let EndDate = '01Jun2023'd;

%macro Members(start,end);
%local i date ;

data Contacts;
  length DATE $7 Members 8;
  stop;
run;

proc sql noprint;
%do i=0 %to %sysfunc(intck(month,&amp;amp;start,&amp;amp;end));
  %let date=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i,e));
	insert into Contacts(Date,Members) 
	select 
		 put(&amp;amp;date., monyy7.) as Date,
		 count(Id) as Memebers
	from Contact where CreatedDate&amp;lt;=&amp;amp;date.
	;
	quit;

%end;
%mend Members;

%Members(&amp;amp;StartDate., &amp;amp;EndDate.)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 20:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889256#M351316</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-14T20:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889260#M351319</link>
      <description>&lt;P&gt;You moved the QUIT statement before the %END.&amp;nbsp; It needs to be after the %END, as in Tom's post. As you have it, the QUIT executes after the first INSERT statement, and ends the PROC SQL step.&amp;nbsp; Then the second INSERT statement is an error, because it's not part of a PROC SQL step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also have a typo&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(Id) as Memebers  /*should be members*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I don't think the typo matters.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 20:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889260#M351319</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-14T20:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889276#M351332</link>
      <description>&lt;P&gt;This is great and I was able to run it. However, I'm curious to know if there's a way I don't have to create a table and insert each row to it?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 04:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889276#M351332</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-15T04:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889278#M351334</link>
      <description>&lt;P&gt;Since it looks like you are just counting OBSERVATIONS (you use ID in the COUNT() function, but I doubt you have any observations that have a missing value of ID) you can probably just use a FORMAT.&lt;/P&gt;
&lt;P&gt;If you just want creation counts by month you can just use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=contact;
  where &amp;amp;startdate &amp;lt;= intnx('month',createddate,0,'b') &amp;lt;= &amp;amp;enddate ;
  tables createddate;
  format createddate monyy7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But since you appear to want OVERLAPPING intervals you will need to use a multilevel format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET StartDate = '01Jul2021'd;
%Let EndDate = '01Jun2023'd;

data format;
 fmtname='ASOF';
 hlo='SML';
 start=.;
 do offset=0 to intck('month',&amp;amp;StartDate., &amp;amp;EndDate.);
   end=intnx('month',&amp;amp;StartDate.,offset,'E');
   label=put(end,yymm7.);
   output;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can get your counts using a procedure like TABULATE, REPORT or MEANS that can use multilevel formats.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=contact nway noprint;
  class createddate / mlf ;
  format createddate asof.;
  output out=want n=Members;
run;

proc print;
  var createdate members;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 12:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889278#M351334</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-15T12:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889309#M351349</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/447199"&gt;@HG66&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Sorry my first day here so I'm still learning things. Here is the code I ran using what Tom suggested [I'll post the log shortly. I can't seem to include it here]&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I also asked you to explain what you are trying to do, I don't see that. I still don't know what this confusing loop using macros is trying to do. Please explain in words. Please explain what the output values of w x y and z are, you haven't told us.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 09:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889309#M351349</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-15T09:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889349#M351368</link>
      <description>&lt;P&gt;I have a startdate of Jul2021 and enddate of Jun2023. I want to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Create a CONTACT table that has Id, and other fields with some WHERE conditions (the &lt;U&gt;&lt;EM&gt;date&lt;/EM&gt;&lt;/U&gt;&amp;nbsp;lives here so I need something to loop from statdate to enddate). This could be a large table for each of the months between start and end dates or 23 tables for the 23 months.&lt;/LI&gt;&lt;LI&gt;COUNT table that counts the number of ID's in CONTACT table for each of the months.&lt;/LI&gt;&lt;LI&gt;Another table (which I did not mention in the post) from a different dataset A which will join the COUNT table and has WHERE conditions that looks into Id from CONTACT B (for example: where A. Contact in (select Id from B.CONTACT))&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;So desired results are&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Date&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Sum(A.Amount) – a field from dataset A&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Count (A.Contact) - this depends on table CONTACT so that’s why I want a whole table for 23 months or 23 separate tables&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Count (Id) for each of the months - Join the COUNT table on Date&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Jul2021&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10,000&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;100,000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;…&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;…&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;…&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;June2023&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;15,000&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;120,000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889349#M351368</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-15T14:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889352#M351369</link>
      <description>&lt;P&gt;Sorry, this is still hard to understand the big picture.&amp;nbsp; Could you post the code to generate example input datasets, and then show the output you would want from that example data?&amp;nbsp; Just a small example, so CARDS&amp;nbsp; data steps, with maybe 3 months of data, and and 3-5 IDs for each month.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:20:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889352#M351369</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-15T14:20:53Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889354#M351371</link>
      <description>&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;, this is still hard to comprehend. For example, you say:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a CONTACT table that has Id, and other fields with some WHERE conditions (the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;U&gt;&lt;EM&gt;date&lt;/EM&gt;&lt;/U&gt;&amp;nbsp;lives here so I need something to loop from statdate to enddate). This could be a large table for each of the months between start and end dates or 23 tables for the 23 months.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;You want code to &lt;FONT color="#FF0000"&gt;create&lt;/FONT&gt; a table? Or do you already have data in a data set, and you want to do analysis on it? Even this basic understanding eludes me. Also, we have asked to see your data, can you show us a portion of the data? (Or do we have to help you &lt;FONT color="#FF0000"&gt;create&lt;/FONT&gt; the data, which seems to be what you are saying)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any event, in this thread and future threads, please do not specify that a certain method be used (in this case, you have repeatedly said you want a loop). It may be that a loop is unnecessary, or that its inefficient. Just describe the problem clearly and completely, and ask for help with a solution.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889354#M351371</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-15T14:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889361#M351374</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;This is what i started with. For each of the Year&amp;amp;Month in Amount_By_Year table, I want the &amp;amp;members. for that period and Producers for that period (which is conditional on Contacts table). This code is not working as expected so that's why I need help. Thanks!&lt;/P&gt;&lt;PRE&gt;%let StartDate = '01Jul2021'd;
%let EndDate = '01Jun2023'd;

proc sql; 
create table Contacts as 
select Id, 
	   Name, 
           First_Issued_Policy__c
from Contact 
where ((RecordTypeId='hKQCQ' and CreatedDate&amp;lt;=&amp;amp;StartDate.)
     or (RecordTypeId='fgQCA' and CreatedDate&amp;lt;&amp;amp;StartDate. and (First_Issued_Policy__c&amp;gt;=&amp;amp;StartDate. or First_Issued_Policy__c is null))
;
quit;

proc sql; 
select count(Id) into :Members 
from Contacts;
quit;

proc sql; 
create table Amount_by_Year as 
select year(a.Record_Date__c) as Year, 
	   month(a.Record_Date__c) as Month, 
	   sum(b.Amount) as Amount, 
	   count(distinct b.Contact__c) as Producers, 
	   sum(b.Amount)/count(distinct b.Contact__c) as Avg_per_Producer,
	   &amp;amp;Members. as Members,
	   sum(b.Amount)/&amp;amp;Members. as Avg_per_Member
from Policy_Std a left join Policy_Agent_Std b on a.Id = b.Opportunity__c 
where b.Contact__c in (select Id from Contacts)
	and '01Jul2021'd &amp;lt;= a.Record_Date__c &amp;lt; '01Jul2023'd
group by 1, 2;
quit;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:49:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889361#M351374</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-15T14:49:25Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889363#M351376</link>
      <description>&lt;P&gt;Many of us refuse to download Microsoft Office documents as they can be security threats. I personally refuse to download ANY file attachments. Can you please show us the data, as text, for example following &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889363#M351376</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-15T14:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889364#M351377</link>
      <description>&lt;P&gt;&amp;nbsp;Yes. here it is&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Id&lt;/TD&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;RecordTypeId&lt;/TD&gt;&lt;TD&gt;CreatedDate&lt;/TD&gt;&lt;TD&gt;First_Issued_Policy__c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;OzQAO&lt;/TD&gt;&lt;TD&gt;John&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;hKQCQ&lt;/TD&gt;&lt;TD&gt;08APR2019:16:32:27&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;P4QAO&lt;/TD&gt;&lt;TD&gt;Michael&lt;/TD&gt;&lt;TD&gt;hJQCQ&lt;/TD&gt;&lt;TD&gt;08APR2019:19:14:37&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PEQA4&lt;/TD&gt;&lt;TD&gt;Sam&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;09APR2019:10:28:26&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PFQA4&lt;/TD&gt;&lt;TD&gt;Elia&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;09APR2019:10:28:26&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PGQA4&lt;/TD&gt;&lt;TD&gt;Julie&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;09APR2019:10:28:26&lt;/TD&gt;&lt;TD&gt;3-Jun-19&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PHQA4&lt;/TD&gt;&lt;TD&gt;Chris&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;29OCT2016:17:28:31&lt;/TD&gt;&lt;TD&gt;8-Jul-21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PIQA4&lt;/TD&gt;&lt;TD&gt;Tyler&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;09APR2019:10:28:26&lt;/TD&gt;&lt;TD&gt;21-Sep-22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PJQA4&lt;/TD&gt;&lt;TD&gt;Susan&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;09APR2019:10:28:26&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PKQA4&lt;/TD&gt;&lt;TD&gt;Laura&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;09APR2019:10:28:26&lt;/TD&gt;&lt;TD&gt;24-Jan-20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;PLQA4&lt;/TD&gt;&lt;TD&gt;Mike&lt;/TD&gt;&lt;TD&gt;fgQCA&lt;/TD&gt;&lt;TD&gt;09APR2019:10:28:26&lt;/TD&gt;&lt;TD&gt;11-Aug-22&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:51:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889364#M351377</guid>
      <dc:creator>HG66</dc:creator>
      <dc:date>2023-08-15T14:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS doesn't return expected table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889383#M351383</link>
      <description>&lt;P&gt;Please post the input data as CARDS data steps, like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Contact ;
  input ID : $5. Name : $8. RecordTypeID : $5. CreatedDate : datetime. FirstIssued : date9.;
  format FirstIssued date9. CreatedDate datetime. ;
  cards ;
OzQAO  John     hKQCQ  08APR2019:16:32:27  .
P4QAO  Michael  hJQCQ  08APR2019:19:14:37  .
PEQA4  Sam      fgQCA  09APR2019:10:28:26  .
PFQA4  Elia     fgQCA  09APR2019:10:28:26  .
PGQA4  Julie    fgQCA  09APR2019:10:28:26  3-Jun-19
PHQA4  Chris    fgQCA  29OCT2016:17:28:31  8-Jul-21
PIQA4  Tyler    fgQCA  09APR2019:10:28:26  21-Sep-22
PJQA4  Susan    fgQCA  09APR2019:10:28:26  .
PKQA4  Laura    fgQCA  09APR2019:10:28:26  24-Jan-20
PLQA4  Mike     fgQCA  09APR2019:10:28:26  11-Aug-22
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks like you have two other input datasets as well, Policy_Std and Policy_Agent_Std.&amp;nbsp; Can you post them (as CARDS data steps)?&lt;BR /&gt;&lt;BR /&gt;Then after you have posted the input datasets, please describe the output you want.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 16:24:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-doesn-t-return-expected-table/m-p/889383#M351383</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-15T16:24:38Z</dc:date>
    </item>
  </channel>
</rss>

