<?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: New Ins and Outs- Automated(Loop) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/606002#M175915</link>
    <description>This helps but not completely...I want to run the code for 6 months ...like below code but can't figure out how to get the previous month date .&lt;BR /&gt;&lt;BR /&gt;%macro date_loop(start,end);&lt;BR /&gt;%let start=%sysfunc(inputn(&amp;amp;start,anydtdte9.));&lt;BR /&gt;%let end=%sysfunc(inputn(&amp;amp;end,anydtdte9.));&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),date9.);&lt;BR /&gt;%put &amp;amp;date;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table NEW_&amp;amp;date. as&lt;BR /&gt;select curr.accno,curr.balance,curr.END_OF_MONTH&lt;BR /&gt;from Monthly_report (where=(END_OF_MONTH= "&amp;amp;date"d)) curr&lt;BR /&gt;left join&lt;BR /&gt;Monthly_report (where=(END_OF_MONTH= "&amp;amp;date"d)) prev /* here needs to update */&lt;BR /&gt;on curr.ACCNO = prev.ACCNO&lt;BR /&gt;where prev.ACCNO is missing;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend date_loop;&lt;BR /&gt;&lt;BR /&gt;%date_loop(31JAN2019,31OCT2019)</description>
    <pubDate>Thu, 21 Nov 2019 03:50:34 GMT</pubDate>
    <dc:creator>BaalaRaaji</dc:creator>
    <dc:date>2019-11-21T03:50:34Z</dc:date>
    <item>
      <title>New Ins and Outs- Automated(Loop)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/605981#M175905</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have got 18 months data as a monthly report.&lt;/P&gt;&lt;P&gt;Every month i need to get the New Ins (Accounts in Current month but Not in Last month) and New Outs( Account is Last month but not in Current month)..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the basic query i have written but its find of manual every month to run and save the data to the historic table . As am using this data separately in tableau to get the charts by months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is my query..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Monthly report */&lt;/P&gt;&lt;P&gt;data Monthly_report;&lt;BR /&gt;set Final_analysisSEPT19;&lt;BR /&gt;where end_of_month &amp;gt;='30JUN2018'D ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;DATA TEST;&lt;BR /&gt;format today date9.;&lt;BR /&gt;today=today();&lt;BR /&gt;call symputx ('last_month',put(intnx('month',today,-1,'e'),date9.)); /* 31OCT2019 */&lt;BR /&gt;call symputx ('lastp_month',put(intnx('month',today,-2,'e'),date9.)); /* 30SEP2019 */&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;%put &amp;amp;last_month. &amp;amp;lastp_month.;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/* New ins and New Outs by each Monthly- monthly report */&lt;/P&gt;&lt;P&gt;data current;&lt;BR /&gt;set Monthly_report ;&lt;BR /&gt;where end_of_month = "&amp;amp;last_month."d;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data last;&lt;BR /&gt;set Monthly_report ;&lt;BR /&gt;where end_of_month = "&amp;amp;lastp_month."d;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table New_in as&lt;BR /&gt;select * from current where accno not in (Select accno from last); quit;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table New_out as&lt;BR /&gt;select * from last where accno not in (Select accno from current); quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data IMP.New_in_&amp;amp;last_month.;&lt;BR /&gt;length Flag $15. Flag1 $15.;&lt;BR /&gt;SET New_in;&lt;BR /&gt;Flag='Monthly_Ins';&lt;BR /&gt;Flag1='Total_Ins';&lt;BR /&gt;Date="&amp;amp;last_month."d;&lt;BR /&gt;format Date date9.;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data IMP.New_in_&amp;amp;last_month.;&lt;BR /&gt;length Flag $15. Flag1 $15.;&lt;BR /&gt;SET New_out;&lt;BR /&gt;Flag='Monthly_Outs';&lt;BR /&gt;Flag1='Total_Outs';&lt;BR /&gt;Date="&amp;amp;last_month."d;&lt;BR /&gt;format Date date9.;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA IMP.TOTAL_INS_OUTS;&lt;BR /&gt;SET IMP.New_in_&amp;amp;last_month. IMP.New_in_&amp;amp;last_month.;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA IMP.TOTAL_INS_OUTS_HIST;&lt;BR /&gt;SET XIMP.TOTAL_INS_OUTS_HIST IMP.TOTAL_INS_OUTS;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can we write the above query in loop or automatically runs for 6 months New Ins and Outs.&lt;/P&gt;&lt;P&gt;Any ideas is appreciated.thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 01:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/605981#M175905</guid>
      <dc:creator>BaalaRaaji</dc:creator>
      <dc:date>2019-11-21T01:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: New Ins and Outs- Automated(Loop)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/605996#M175911</link>
      <description>&lt;P&gt;Does this help?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;

  select into max(END_OF_MONTH) into :last_month
  from FINAL_ANALYSISSEPT19 ; /* This is an awful table name. At least try FINAL_ANALYSIS_201909 */

  %let last_monthf=&amp;amp;sysfunc(intnx(month,&amp;amp;last_month, 0,e),date9.); 
  %let prev_monthf=&amp;amp;sysfunc(intnx(month,&amp;amp;last_month,-1,e),date9.);
  %put &amp;amp;last_month= &amp;amp;last_monthf= &amp;amp;prev_monthf=;

  create table NEW as
  select curr.*
  from FINAL_ANALYSIS_201909 (where=(END_OF_MONTH= "&amp;amp;last_monthf"d))  curr
         left join
       FINAL_ANALYSIS_201909 (where=(END_OF_MONTH= "&amp;amp;prev_monthf"d))  prev
         on curr.ACCNO = prev.ACCNO
   where prev.ACCNO is missing;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 03:17:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/605996#M175911</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-11-21T03:17:25Z</dc:date>
    </item>
    <item>
      <title>Re: New Ins and Outs- Automated(Loop)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/606002#M175915</link>
      <description>This helps but not completely...I want to run the code for 6 months ...like below code but can't figure out how to get the previous month date .&lt;BR /&gt;&lt;BR /&gt;%macro date_loop(start,end);&lt;BR /&gt;%let start=%sysfunc(inputn(&amp;amp;start,anydtdte9.));&lt;BR /&gt;%let end=%sysfunc(inputn(&amp;amp;end,anydtdte9.));&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),date9.);&lt;BR /&gt;%put &amp;amp;date;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table NEW_&amp;amp;date. as&lt;BR /&gt;select curr.accno,curr.balance,curr.END_OF_MONTH&lt;BR /&gt;from Monthly_report (where=(END_OF_MONTH= "&amp;amp;date"d)) curr&lt;BR /&gt;left join&lt;BR /&gt;Monthly_report (where=(END_OF_MONTH= "&amp;amp;date"d)) prev /* here needs to update */&lt;BR /&gt;on curr.ACCNO = prev.ACCNO&lt;BR /&gt;where prev.ACCNO is missing;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend date_loop;&lt;BR /&gt;&lt;BR /&gt;%date_loop(31JAN2019,31OCT2019)</description>
      <pubDate>Thu, 21 Nov 2019 03:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/606002#M175915</guid>
      <dc:creator>BaalaRaaji</dc:creator>
      <dc:date>2019-11-21T03:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: New Ins and Outs- Automated(Loop)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/606007#M175918</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example using sample data that might lead in the right direction:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Getting data to simplify example...;
proc means nway missing noprint data=sashelp.prdsal2;
class state prodtype monyr;
var actual;
output out=sales(drop=_type_ _freq_ prodtype) sum=;
where prodtype='OFFICE';
run;

*Set end of current month.;
%let curr_month = '31DEC1995'd;
data _null_;
curr_month = &amp;amp;curr_month;
beg_month = "'"||put(intnx("month",&amp;amp;curr_month.,-6,"e"),date9.)||"'d";
call symput("beg_month",beg_month);
run;

*Limit sales to last 6 months.;
data have(where=(end_of_month between &amp;amp;beg_month. and &amp;amp;curr_month.));
set sales;
format end_of_month date9.;
end_of_month = intnx("month",monyr,0,"e");
if mod(_n_,5)=2 then delete; *simulate missing data -- this example set has an observation for each state and month...;
run;

*Macro;
%macro loop(dsin=/*input dataset*/, dsoutpfx=/*output data prefix*/);
%do i=1 %to 6;
data &amp;amp;dsoutpfx._IN_&amp;amp;i. &amp;amp;dsoutpfx._OUT_&amp;amp;i.;
merge 
	&amp;amp;dsin.(where=(end_of_month = intnx("month",&amp;amp;curr_month.,-&amp;amp;i.,"e")) in=OLD)
	&amp;amp;dsin.(where=(end_of_month = intnx("month",&amp;amp;curr_month.,-&amp;amp;i.+1,"e")) in=NEW);

by state;
if not(OLD and NEW);
if OLD and not NEW then output &amp;amp;dsoutpfx._OUT_&amp;amp;i.;
else if NEW and not OLD then output &amp;amp;dsoutpfx._IN_&amp;amp;i.;
run;
%end;
%mend;

%loop(dsin=have,dsoutpfx=outsets)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-unison&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 04:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/606007#M175918</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-11-21T04:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: New Ins and Outs- Automated(Loop)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/609578#M177488</link>
      <description>&lt;P&gt;Thank you unison.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That worked perfectly...&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2019 01:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-Ins-and-Outs-Automated-Loop/m-p/609578#M177488</guid>
      <dc:creator>BaalaRaaji</dc:creator>
      <dc:date>2019-12-05T01:41:35Z</dc:date>
    </item>
  </channel>
</rss>

