<?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: How to change date character into numeric dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748277#M235007</link>
    <description>&lt;P&gt;The DATA stepis very good at working sequences, so use that.&lt;/P&gt;
&lt;P&gt;First, let us read the data so that we have usable SAS dates:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data item1;
input ID $ Transaction_number $ Date1 :yymmdd8.;
format date1 yymmdd10.;
datalines;
1 1000 20190112
1 1001 20190121
1 1002 20200111
2 1003 20190102
2 1004 20200110
3 1005 20210123
5 1006 20210102 
6 1007 20200101
;

data item2;
input ID $ Transaction_number $ Date2 :yymmdd8.;
format date2 yymmdd10.;
datalines;
1 2000 20190211
1 2001 20210102
2 2002 20200521
3 2003 20210101
4 2004 20200101
5 1006 20210102
5 2005 20210202
;

data item3;
input ID $ Transaction_number $ Date3 :yymmdd8.;
format date3 yymmdd10.;
cards;
1 3000 20210411
1 3001 20200102
2 3002 20200521
3 3003 20200101
4 3004 20190101
5 3006 20200102
5 3005 20210202
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, combine all datasets into one and sort:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all;
length dname $41;
set
  item1 (rename=(date1=date))
  item2 (rename=(date2=date))
  item3 (rename=(date3=date))
  indsname=dname
;
length item $32;
item = scan(dname,2,".");
run;

proc sort data=all;
by id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the individual dataset are already sorted by id and date, you can use a BY in the DATA step to "interleave" the observations, so you do not need the extra sort.&lt;/P&gt;
&lt;P&gt;Now, a data step will find your hits:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set all;
by id;
retain yn_2 yn_3 date_1;
if first.id
then do;
  yn_2 = 0;
  yn_3 = 0;
  date_1 = .;
end;
if date_1 = .
then do;
  if item = "ITEM1" then date_1 = date;
end;
else do;
  diff = intck('year',date_1,date,"c");
  if diff le 1
  then do;
    if item = "ITEM2" then yn_2 = 1;
    if item = "ITEM3" then yn_3 = 1;
  end;
end;
if last.id;
keep id yn_2 yn_3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To see the inner workings of this step, comment the last two statements, so all observations and variables can be seen.&lt;/P&gt;</description>
    <pubDate>Wed, 16 Jun 2021 08:35:22 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-06-16T08:35:22Z</dc:date>
    <item>
      <title>How to change date character into numeric dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748259#M234994</link>
      <description>&lt;P&gt;Hello all.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA item1;
   INPUT ID $ Transaction_number $ Date1;
CARDS;
  1 1000 20190112
  1 1001 20190121
  1 1002 20200111
  2 1003 20190102
  2 1004 20200110&lt;BR /&gt;  3 1005 20210123&lt;BR /&gt;  5 1006 20210102&lt;BR /&gt;  6 1007 20200101
; RUN;
DATA item2;
   INPUT ID $ Transaction_number $ Date2;
CARDS;
   1 2000 20190211
   1 2001 20210102
   2 2002 20200521&lt;BR /&gt;   3 2003 20210101&lt;BR /&gt;   4 2004 20200101&lt;BR /&gt;   5 1006 20210102&lt;BR /&gt;   5 2005 20210202&lt;BR /&gt;; RUN;DATA item3;
   INPUT ID $ Transaction_number $ Date3;
CARDS;
   1 3000 20210411
   1 3001 20200102
   2 3002 20200521&lt;BR /&gt;   3 3003 20200101&lt;BR /&gt;   4 3004 20190101&lt;BR /&gt;   5 3006 20200102&lt;BR /&gt;   5 3005 20210202&lt;BR /&gt;; RUN;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am working with a data that looks something like this.&lt;/P&gt;&lt;P&gt;The dates in the above data in my dataset were characters so I converted them into numerical values by doing this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA item1; set item1;
   date_1 = INPUT(Date1, YYMMDD8.);
   FORMAT date_1 YYMMDDN8.;
   PUT date_1=;
RUN;&lt;BR /&gt;DATA item2;&amp;nbsp;set&amp;nbsp;item2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;date_2&amp;nbsp;=&amp;nbsp;INPUT(Date2,&amp;nbsp;YYMMDD8.);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FORMAT&amp;nbsp;date_2&amp;nbsp;YYMMDD8.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;PUT&amp;nbsp;date_2=;&lt;BR /&gt;RUN;&lt;BR /&gt;DATA item3;&amp;nbsp;set&amp;nbsp;item3;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;date_3&amp;nbsp;=&amp;nbsp;INPUT(Date3,&amp;nbsp;YYMMDD8.);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FORMAT&amp;nbsp;date_3&amp;nbsp;YYMMDD8.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;PUT&amp;nbsp;date_3=;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am trying to create a dataset where I only include those who purchased item 1 and create variables YN_2 YN_3 to see if the individuals that purchased item 1 purchased item 2, 3 within a year since purchasing item 1.&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;PRE&gt;PROC SQL;
   CREATE TABLE YN_2 as
   SELECT SA.ID, SA.Transaction_number, CASE WHEN SA.Date1 &amp;lt;= SB.Date2 &amp;lt;= (SA.Date1 + 365) THEN 1&lt;BR /&gt;   ELSE 0 END AS YN_2&lt;BR /&gt;   FROM item1 AS SA&lt;BR /&gt;   LEFT JOIN item2 AS SB&lt;BR /&gt;   ON SA.ID = SB.ID&lt;BR /&gt;   ORDER BY ID&lt;BR /&gt;; QUIT;&lt;/PRE&gt;&lt;P&gt;I have been able to find YN_2 and YN_3 separately using the code above, but am struggling to put them all together. I would only like to keep the first dates of purchase in item1 but don't want to do the same for items 2 and 3 as the initial date might not fall within the 1 year period but have a future purchase that falls within the 1 year. The end product I would like is a data where all IDs observed are included and have YN_2, YN_3...YN_n(n being an arbitrary number) of all items that I have (more than the three I included above) among those who have purchased item 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data above is only a sample of the data and I'm sorry in advance if it feels too incomplete.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the help.&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;</description>
      <pubDate>Wed, 16 Jun 2021 06:48:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748259#M234994</guid>
      <dc:creator>ercksh8</dc:creator>
      <dc:date>2021-06-16T06:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to change date character into numeric dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748263#M234998</link>
      <description>&lt;P&gt;Which item1 date do you want to use as the reference date? The first or the last when an item1 was purchased?&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 07:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748263#M234998</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-16T07:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to change date character into numeric dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748274#M235006</link>
      <description>&lt;P&gt;I would like to use the earliest date as the index date.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 08:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748274#M235006</guid>
      <dc:creator>ercksh8</dc:creator>
      <dc:date>2021-06-16T08:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to change date character into numeric dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748277#M235007</link>
      <description>&lt;P&gt;The DATA stepis very good at working sequences, so use that.&lt;/P&gt;
&lt;P&gt;First, let us read the data so that we have usable SAS dates:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data item1;
input ID $ Transaction_number $ Date1 :yymmdd8.;
format date1 yymmdd10.;
datalines;
1 1000 20190112
1 1001 20190121
1 1002 20200111
2 1003 20190102
2 1004 20200110
3 1005 20210123
5 1006 20210102 
6 1007 20200101
;

data item2;
input ID $ Transaction_number $ Date2 :yymmdd8.;
format date2 yymmdd10.;
datalines;
1 2000 20190211
1 2001 20210102
2 2002 20200521
3 2003 20210101
4 2004 20200101
5 1006 20210102
5 2005 20210202
;

data item3;
input ID $ Transaction_number $ Date3 :yymmdd8.;
format date3 yymmdd10.;
cards;
1 3000 20210411
1 3001 20200102
2 3002 20200521
3 3003 20200101
4 3004 20190101
5 3006 20200102
5 3005 20210202
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, combine all datasets into one and sort:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all;
length dname $41;
set
  item1 (rename=(date1=date))
  item2 (rename=(date2=date))
  item3 (rename=(date3=date))
  indsname=dname
;
length item $32;
item = scan(dname,2,".");
run;

proc sort data=all;
by id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the individual dataset are already sorted by id and date, you can use a BY in the DATA step to "interleave" the observations, so you do not need the extra sort.&lt;/P&gt;
&lt;P&gt;Now, a data step will find your hits:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set all;
by id;
retain yn_2 yn_3 date_1;
if first.id
then do;
  yn_2 = 0;
  yn_3 = 0;
  date_1 = .;
end;
if date_1 = .
then do;
  if item = "ITEM1" then date_1 = date;
end;
else do;
  diff = intck('year',date_1,date,"c");
  if diff le 1
  then do;
    if item = "ITEM2" then yn_2 = 1;
    if item = "ITEM3" then yn_3 = 1;
  end;
end;
if last.id;
keep id yn_2 yn_3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To see the inner workings of this step, comment the last two statements, so all observations and variables can be seen.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 08:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-date-character-into-numeric-dates/m-p/748277#M235007</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-16T08:35:22Z</dc:date>
    </item>
  </channel>
</rss>

