<?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: Excluding First Month of Data from Analysis in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303194#M64364</link>
    <description>&lt;P&gt;Hi Patrick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks - this is useful too. I will hold on to this code in the event that I need it. For now, ballardw's code works well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you both!&lt;/P&gt;</description>
    <pubDate>Fri, 07 Oct 2016 15:18:02 GMT</pubDate>
    <dc:creator>ZDR</dc:creator>
    <dc:date>2016-10-07T15:18:02Z</dc:date>
    <item>
      <title>Excluding First Month of Data from Analysis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303172#M64356</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm struggling to figure out something which I suspect is very simple. I have a piece of code that compares the value of one variable to the value of that variable for a transaction in the previous month. If the criteria is met, a variable called flag1 is given a value of 1. Otherwise, it is 0. The problem I face is that obviously, for the first month of data, there is no previous month to compare it to.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code works. However, the way I've coded it, the first month of data is always December 2015. So the value for Dec 2015 is ignored. But in practice, the first month of data may well be earlier, or later, than December 2015. So I need the code to dynamically search for the first month of data and exclude it. I have variables for Month (1,2,...12) and Year (2015, 2016).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dataset;&lt;BR /&gt;set dataset;&lt;BR /&gt;by CH_ID temp_trans_date ;&lt;BR /&gt;if first.temp_trans_date then do;&lt;BR /&gt;if temp_trans_date &amp;gt; 20425 AND tr_sum &amp;gt; 1.15*(lag(tr_sum)) then flag1 = 1;&lt;BR /&gt;else flag1=0 ;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Like I said it works, but instead of&amp;nbsp;&lt;SPAN&gt;temp_trans_date &amp;gt; 20425 I need the logic to be along the lines of IF (not first month and year of data) AND...&lt;/SPAN&gt;&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;&lt;SPAN&gt;Any help would be much appreciated. Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 14:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303172#M64356</guid>
      <dc:creator>ZDR</dc:creator>
      <dc:date>2016-10-07T14:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding First Month of Data from Analysis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303175#M64357</link>
      <description>&lt;P&gt;One way to get the end of the month for the earliest data into a macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select intnx('month',min(date),0,'E') into: MinDate
   from dataset;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use in a comparison such as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if temp_trans_date &amp;gt; &amp;amp;MinDate. AND tr_sum &amp;gt; 1.15*(lag(tr_sum)) then flag1 = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;after the Proc Sql.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 14:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303175#M64357</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-07T14:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding First Month of Data from Analysis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303180#M64358</link>
      <description>&lt;P&gt;Thanks ballardw. I will try this out and let you know if it works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just so I understand the SQL here -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;intnx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'month'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;date&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'E'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;into&lt;/SPAN&gt;: MinDate&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- is 'month' the numeric month variable? &amp;nbsp;&lt;/P&gt;&lt;P&gt;- is min(date) a command? Or is date a variable name?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be good to understand what the code is doing so I can use it more comfortably in future.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 14:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303180#M64358</guid>
      <dc:creator>ZDR</dc:creator>
      <dc:date>2016-10-07T14:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding First Month of Data from Analysis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303182#M64359</link>
      <description>&lt;P&gt;I'm a little bit concerned about your usage of a lag() function in an IF condition but as you write your code works as such I won't go further into details about this one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The option&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;&amp;nbsp;proposes will give you the earliest date in the whole of your data. In case you have for all CH_ID's the same earliest date OR you need the earliest date per CH_ID then one of the following two code options should do the job as well.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset;
  set dataset;
  by CH_ID temp_trans_date;
  retain first_temp_trans_date;
  if _n_=1 then first_temp_trans_date=temp_trans_date;
  if first.temp_trans_date then
    do;
      if temp_trans_date &amp;gt; first_temp_trans_date AND tr_sum &amp;gt; 1.15*(lag(tr_sum)) then
        flag1 = 1;
      else flag1=0;
      output;
    end;
run;

data dataset;
  set dataset;
  by CH_ID temp_trans_date;
  retain first_temp_trans_date;
  if first.ch_id then first_temp_trans_date=temp_trans_date;
  if first.temp_trans_date then
    do;
      if temp_trans_date &amp;gt; first_temp_trans_date AND tr_sum &amp;gt; 1.15*(lag(tr_sum)) then
        flag1 = 1;
      else flag1=0;
      output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Oct 2016 15:24:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303182#M64359</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-07T15:24:24Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding First Month of Data from Analysis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303187#M64360</link>
      <description>&lt;P&gt;The function INTNX is used to increment dates. The first parameter is what value is incremented. This may be a variable or a literal as in this case. Othe options are like 'year', 'week', and more complicated examples. Time variables get to add in hours, minutes and seconds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The 0 says to increment 0 months but the last parameter is alignment. 'E' means end of period, 'B' would be beginning and 'S' means same.&lt;/P&gt;
&lt;P&gt;So the function says "look at the&amp;nbsp;loweds value of the date variable and find the end of the month that value is in".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The min(date), just in case that isn't clear, is telling SQL to find the lowest value (MIN aggregate&amp;nbsp;function)&amp;nbsp;of the specified variable. I used date to indicate any generic SAS date valued variable. It could have been temp_trans_date, datevisit, dateofbirth etc.&lt;/P&gt;
&lt;P&gt;I may have been assuming that is what you meant by "first". If your definition of "first" is different then you'll need to be much more specific about a definition.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 15:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303187#M64360</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-07T15:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding First Month of Data from Analysis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303194#M64364</link>
      <description>&lt;P&gt;Hi Patrick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks - this is useful too. I will hold on to this code in the event that I need it. For now, ballardw's code works well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you both!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 15:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-First-Month-of-Data-from-Analysis/m-p/303194#M64364</guid>
      <dc:creator>ZDR</dc:creator>
      <dc:date>2016-10-07T15:18:02Z</dc:date>
    </item>
  </channel>
</rss>

