<?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: Edit date format so can left join two variables from two different datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561310#M157114</link>
    <description>&lt;P&gt;or transform each a bit:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;on &lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;month_end&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;yymmn6&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; put(b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;tracking_month,6.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 May 2019 03:31:34 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-05-24T03:31:34Z</dc:date>
    <item>
      <title>Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561198#M157058</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two datasets (cd.dataset_one and&amp;nbsp;cd.dataset_two) and would like to create a table by left joining&amp;nbsp;cd.dataset_one onto&amp;nbsp;cd.dataset_one (the code I currently have is below). As I am matching the 'tracking_month' variable from dataset 1 and 'month_end' variable from dataset 2, would it be possible for someone to provide me with code to format the dates to recognise each other when I run the proc sql (the preference is to use the 'tracking_month' format so that dataset 2 has 'month_end' 30APR2017 converted to 201704, for example)? Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Current code:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data cd.dataset_two_update;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set&amp;nbsp;cd.dataset_two;&lt;/P&gt;&lt;P&gt;[&lt;FONT color="#FF0000"&gt;NOTE: Can the date format for 'month_end' variable be changed in this line of code here, or after 'run;' below so that the proc sql table below takes on the preferred date format?&lt;/FONT&gt;]&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;create table FLAG_NUMBERS as&lt;BR /&gt;&amp;nbsp; &amp;nbsp;select a.tracking_month, a.ACCT_ID, a.flag&lt;BR /&gt;&amp;nbsp; &amp;nbsp;from cd.dataset_two_update as a&lt;BR /&gt;&amp;nbsp; &amp;nbsp;left join cd.dataset_one as b&lt;BR /&gt;&amp;nbsp; &amp;nbsp;on a.month_end = b.tracking_month and a.ACCOUNT_ID = b.ACCT_ID&lt;BR /&gt;order by a.ACCT_ID&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;cd.dataset_one (dataset 1)&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tracking month format.PNG" style="width: 563px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29731i8191F74610EC56D3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tracking month format.PNG" alt="Tracking month format.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;cd.dataset_two (dataset 2)&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Month end format.PNG" style="width: 560px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29732i0D5266F6E7018C93/image-size/large?v=v2&amp;amp;px=999" role="button" title="Month end format.PNG" alt="Month end format.PNG" /&gt;&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 16:24:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561198#M157058</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-05-23T16:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561211#M157064</link>
      <description>&lt;P&gt;The value in your dataset_two is actually a DATE value which can be determined by the DATE format. The other value in the first set is a simple numeric value and not a date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your tracking_month variable does not have a day of the month so you would very likely not actually match the month_end.&lt;/P&gt;
&lt;P&gt;The first step would be to create a date value from the tracking month. That looks like you should be able to do that with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;input(put(tracking_month,6 -L), yymmn6.) which will create a date that has the first day of the month assumed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the actual month and year to match between the two then you would need to get the same actual date which would look like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;input(put(tracking_month,6 -L), yymmn6.) = intnx('month',month_end,0,'B')&lt;/PRE&gt;
&lt;P&gt;The intnx function is used to increment date, time or datetime values. The first parameter is the interval, month in this case, second is the starting date value, 3rd is the number of intervals to increment, 0 means the same month here, and the optional last parameter is alignment with 'B' means "beginning" so the intnx function returns the first day of the month in the same month and year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assigned formats of numeric values never affect comparisons as the actual values are used.&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 17:07:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561211#M157064</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-23T17:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561243#M157080</link>
      <description>&lt;P&gt;Tracking_Month does not look like a date. It looks like a 6 digit number.&amp;nbsp; Sounds like you want to treat the ones and tens place as a month number and the other four places as the year number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So either convert TRACK_MONTH to a date value that is the end of the month&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   on a.month_end = intnx('month',input(put(b.tracking_month,z6.),yymmn6.),0,'end')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or convert the date MONTH_END to a number using that&amp;nbsp; YYY,YMM style.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   on input(put(a.month_end,yymmn6.),6.) = b.tracking_month&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;1016  data one(keep=month_end) two(keep=tracking_month);
1017   month_end='30APR2017'd ;
1018   format month_end date9.;
1019   tracking_month=201704;
1020  run;

NOTE: The data set WORK.ONE has 1 observations and 1 variables.
NOTE: The data set WORK.TWO has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds


1021
1022  proc sql;
1023  create table test1 as select *
1024    from one a inner join two b
1025      on a.month_end = intnx('month',input(put(b.tracking_month,z6.),yymmn6.),0,'end')
1026  ;
NOTE: Table WORK.TEST1 created, with 1 rows and 2 columns.

1027  create table test2 as select *
1028    from one a inner join two b
1029      on input(put(a.month_end,yymmn6.),6.) = b.tracking_month
1030  ;
NOTE: Table WORK.TEST2 created, with 1 rows and 2 columns.

1031  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.03 seconds
      cpu time            0.03 seconds&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 20:04:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561243#M157080</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-23T20:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561302#M157113</link>
      <description>&lt;P&gt;If tracking_month is effectively a 6-digit number (e.g. 201804), I would rather deal with numbers (instead of going to characters and back)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select *
from one left join two  
    on intnx("month", month_end, 0, "beginning") = 
        mdy(mod(tracking_month, 100), 1, floor(tracking_month/100));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note Since you are joining on months, doing so on beginnings or ends is equivalent. In this case, joining on beginnings is a bit simpler because the day is always 1.&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 03:14:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561302#M157113</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-05-24T03:14:12Z</dc:date>
    </item>
    <item>
      <title>Re: Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561310#M157114</link>
      <description>&lt;P&gt;or transform each a bit:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;on &lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;month_end&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;yymmn6&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; put(b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;tracking_month,6.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 03:31:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561310#M157114</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-05-24T03:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561339#M157127</link>
      <description>Hi, thanks for your reply. I added in your code and edited it to reference the two datasets (adding a. and b.) but SAS provided me with the following error relating to the '6 -L' part of the code:&lt;BR /&gt;&lt;BR /&gt;on input(put(a.tracking_month,6 -L), yymmn6.) = intnx('month',b.month_end,0,'B')&lt;BR /&gt;_&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a format name, ?.&lt;BR /&gt;&lt;BR /&gt;ERROR 76-322: Syntax error, statement will be ignored.</description>
      <pubDate>Fri, 24 May 2019 09:39:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561339#M157127</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-05-24T09:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561344#M157130</link>
      <description>&lt;P&gt;[Edited]&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;on input(put(a.tracking_month,6. -L), yymmn6.) = intnx('month',b.month_end,0,'B')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which begs the question: Have you tried to understand the reply? &lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 10:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561344#M157130</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-05-24T10:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: Edit date format so can left join two variables from two different datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561414#M157160</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/266226"&gt;@jeremy4&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi, thanks for your reply. I added in your code and edited it to reference the two datasets (adding a. and b.) but SAS provided me with the following error relating to the '6 -L' part of the code:&lt;BR /&gt;&lt;BR /&gt;on input(put(a.tracking_month,6 -L), yymmn6.) = intnx('month',b.month_end,0,'B')&lt;BR /&gt;_&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a format name, ?.&lt;BR /&gt;&lt;BR /&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Post any error message with the entire data step or procedure call. Copy from the LOG and paste into a code box opened with the forum's {I} or "running man" icon. The _ character will be under the part with the problem most of the time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, because of a distraction on my part I missed the . after 6 that indicates the format to use for put&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;on input(put(a.tracking_month,6&lt;FONT size="4"&gt;&lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt; -L), yymmn6.) = intnx('month',b.month_end,0,'B')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since I did not have any actual data set (HINT)&amp;nbsp;to test code with I missed the error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 14:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Edit-date-format-so-can-left-join-two-variables-from-two/m-p/561414#M157160</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-24T14:41:00Z</dc:date>
    </item>
  </channel>
</rss>

