<?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: Identify variable with same value in consecutive months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441409#M110430</link>
    <description>&lt;P&gt;Use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;duration = intck("month", lag(purchase_date), purchase_date);
if not first.fruit and duration &amp;lt;= 1 then flag = 1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Mar 2018 22:38:26 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-03-01T22:38:26Z</dc:date>
    <item>
      <title>Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441378#M110407</link>
      <description>&lt;P&gt;This is a sample of my dataset.&amp;nbsp; I want to identify any ID where they purchased the same fruit in consecutive months at any time during a given date range.&amp;nbsp; We can use 01/01/2017 - 12/31/2017 as an example (this date range will be expanded at a later time).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this example, ID 111 and 222 should be returned only or flagged in some way that I can identify as such.&amp;nbsp; Looking to code this in PROC SQL, however, it its easier to use a data step instead that will work too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="354"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="92"&gt;ID&lt;/TD&gt;
&lt;TD width="112"&gt;PURCHASE_DATE&lt;/TD&gt;
&lt;TD width="150"&gt;FRUIT&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;111&lt;/TD&gt;
&lt;TD&gt;01/20/2017&lt;/TD&gt;
&lt;TD&gt;Oranges&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;111&lt;/TD&gt;
&lt;TD&gt;02/01/2017&lt;/TD&gt;
&lt;TD&gt;Oranges&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;222&lt;/TD&gt;
&lt;TD&gt;04/24/2017&lt;/TD&gt;
&lt;TD&gt;Apples&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;222&lt;/TD&gt;
&lt;TD&gt;05/12/2017&lt;/TD&gt;
&lt;TD&gt;Apples&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;222&lt;/TD&gt;
&lt;TD&gt;08/28/2017&lt;/TD&gt;
&lt;TD&gt;Apples&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;222&lt;/TD&gt;
&lt;TD&gt;10/10/2017&lt;/TD&gt;
&lt;TD&gt;Apples&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;333&lt;/TD&gt;
&lt;TD&gt;03/14/2016&lt;/TD&gt;
&lt;TD&gt;Pears&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;333&lt;/TD&gt;
&lt;TD&gt;09/23/2016&lt;/TD&gt;
&lt;TD&gt;Pears&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 01 Mar 2018 21:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441378#M110407</guid>
      <dc:creator>PhatRam33</dc:creator>
      <dc:date>2018-03-01T21:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441382#M110410</link>
      <description>&lt;P&gt;Whats your expected output from the input data set?&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 21:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441382#M110410</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-01T21:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441383#M110411</link>
      <description>&lt;P&gt;It would be easier if you would reconsider the definition of "consecutive".&amp;nbsp; How about within 30 days?&amp;nbsp; After all, consecutive months would treat these differently:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;03/01/2016 to 04/30/2016 = consecutive&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;01/31/2016 to 03/01/2016 = not consecutive, even though the duration between purchases is smaller&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 21:15:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441383#M110411</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-01T21:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441386#M110413</link>
      <description>&lt;P&gt;Yes, I would define as consecutive months would be within 30 days.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 21:19:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441386#M110413</guid>
      <dc:creator>PhatRam33</dc:creator>
      <dc:date>2018-03-01T21:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441387#M110414</link>
      <description>&lt;P&gt;OK, here's a way to set the flags:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by id fruit purchase_date;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id fruit;&lt;/P&gt;
&lt;P&gt;duration = dif(purchase_date);&lt;/P&gt;
&lt;P&gt;if first.fruit = 0 and duration &amp;lt;= 30 then flag=1;&lt;/P&gt;
&lt;P&gt;drop duration;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once the observations have been flagged, you still have to decide what should happen next.&amp;nbsp; And your dates must be valid SAS dates, not character strings.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 21:27:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441387#M110414</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-01T21:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441398#M110421</link>
      <description>&lt;P&gt;Thank you very much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was able to get this to work and formatted my date field as such with a separate data step:&lt;/P&gt;
&lt;P&gt;purchase_date=datepart(purchase_date);&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;format&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; purchase_date &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;date9.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;111 returned with value of 1 for the second row and 222 with a value of 1 for the 4th row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For now I was just planning on creating a proc sql&amp;nbsp; and query on any Ids where they were flagged with a value of 1 and pull in any additional detail from another data set.&amp;nbsp; This is a work in progress, however wanted to get this first foundational step complete.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did have a question though, if say the date fell on 01/01/2017 and the next date occurred on 02/25/2017 would fall greater than 30 days, would I need to expand my criteria greater than 30 or is there a way that sas can format the dates as months and then look for consecutive in that manner.&amp;nbsp; Sorry for the loaded question.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441398#M110421</guid>
      <dc:creator>PhatRam33</dc:creator>
      <dc:date>2018-03-01T22:11:50Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441406#M110427</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;try this,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE ;
format PURCHASE_DATE mmddyy10.;
INFILE DATALINES DLM=",";
INPUT ID :3. PURCHASE_DATE : mmddyy10. FRUIT $;
DATALINES;
111,01/20/2017,Oranges
111,02/01/2017,Oranges
222,04/24/2017,Apples
222,05/12/2017,Apples
222,08/28/2017,Apples
222,10/10/2017,Apples
333,03/14/2016,Pears
333,09/23/2016,Pears
;
RUN;

PROC SORT DATA=HAVE;
BY ID FRUIT PURCHASE_DATE ;
RUN;
%let Start_Date=01JAN2017;
%let End_Date=31DEC2017;

DATA WANT;
FORMAT Lag_Date mmddyy10.;
SET HAVE;
BY ID FRUIT PURCHASE_DATE ;
Lag_Date=LAG(PURCHASE_DATE);
VAL=(MONTH(PURCHASE_DATE)+YEAR(PURCHASE_DATE))-(MONTH(LAG(PURCHASE_DATE))+YEAR(LAG(PURCHASE_DATE)));
IF FIRST.FRUIT=0 and "&amp;amp;Start_Date"D&amp;lt;=LAG(PURCHASE_DATE)&amp;lt;="&amp;amp;End_Date"D and val=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:27:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441406#M110427</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-01T22:27:28Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441409#M110430</link>
      <description>&lt;P&gt;Use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;duration = intck("month", lag(purchase_date), purchase_date);
if not first.fruit and duration &amp;lt;= 1 then flag = 1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441409#M110430</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-01T22:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441412#M110431</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would your code still work if say the end_date occurred in a different year than the start_date as say 01/31/2018?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441412#M110431</guid>
      <dc:creator>PhatRam33</dc:creator>
      <dc:date>2018-03-01T22:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441413#M110432</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;.&amp;nbsp; Similar question to what I asked &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;, would this work if my end date fell under a different year such as one in 2018?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate all the help everyone! I am truly grateful!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441413#M110432</guid>
      <dc:creator>PhatRam33</dc:creator>
      <dc:date>2018-03-01T22:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441418#M110434</link>
      <description>&lt;P&gt;Don't be afraid to do your own testing, it is an excellent learning opportunity. Yes, SAS date functions work across years.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 23:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441418#M110434</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-01T23:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: Identify variable with same value in consecutive months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441564#M110492</link>
      <description>&lt;P&gt;Many thanks will do just that and mark solution later today.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 13:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-variable-with-same-value-in-consecutive-months/m-p/441564#M110492</guid>
      <dc:creator>PhatRam33</dc:creator>
      <dc:date>2018-03-02T13:46:57Z</dc:date>
    </item>
  </channel>
</rss>

