<?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: If Then based on Date format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523559#M142251</link>
    <description>&lt;P&gt;Thank you for the help.&lt;/P&gt;</description>
    <pubDate>Wed, 26 Dec 2018 17:22:10 GMT</pubDate>
    <dc:creator>uopsouthpaw</dc:creator>
    <dc:date>2018-12-26T17:22:10Z</dc:date>
    <item>
      <title>If Then based on Date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523557#M142249</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm attempting to write a macro that will read in Excel data sets (Monthly data) and stack them to a Master data set for trending purposes.&amp;nbsp; I'm using Proc Import, and that reads the format in from Excel.&amp;nbsp; I'm was trying to stay away from using the Data step and manually code all input, format, and informats if possible.&amp;nbsp; The problem I have run into is that the data sets have "Date Opened" saved as one of two different formats (either MMDDYY10. or Datetime16.) as read in by the Proc Import route.&amp;nbsp; Is there a way to do conditional formatting (I don't care for the time portion, only need the date) such as below?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If "the format of [Date Opened] = MMDDYY10. then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Year_c = Year(Date Opened);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Month_c = Month(Date Opened);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Year_Month = Cat(Year_c,"_",Month_c);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else if "the format of [Date Opened] = Datetime16. then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Year_c = Year(datepart(Date Opened));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Month_c = Month(datepart(Date Opened));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Year_Month = Cat(Year_c,"_",Month_c);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since [Date Opened] has a length of 8, then I can't separate them by length.&amp;nbsp; I'm attempting not to have to open excel and change the format.&amp;nbsp; Or is essentially the data step my only route.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Wes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Dec 2018 17:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523557#M142249</guid>
      <dc:creator>uopsouthpaw</dc:creator>
      <dc:date>2018-12-26T17:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: If Then based on Date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523558#M142250</link>
      <description>&lt;P&gt;You can use the VFORMAT function to get the format of a variable.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Dec 2018 17:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523558#M142250</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-12-26T17:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: If Then based on Date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523559#M142251</link>
      <description>&lt;P&gt;Thank you for the help.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Dec 2018 17:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523559#M142251</guid>
      <dc:creator>uopsouthpaw</dc:creator>
      <dc:date>2018-12-26T17:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: If Then based on Date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523584#M142254</link>
      <description>&lt;P&gt;A length of 8 just means that you have numeric field.&amp;nbsp; Both DATE and DATETIME values are stored as numbers, but the meaning of the numbers are different.&amp;nbsp; DATE values are days and DATETIME values are seconds.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on the range of dates you are expecting you might try just testing if the number is too large to be a DATE value.&amp;nbsp; For example if you expect to have dates between 1900 and today that would be stored as numbers between -21,914 and 21,544.&amp;nbsp; If you used the DATEPART() function to treat those numbers as number of seconds then you would get a value of -1 or 0 (depending on if the date reas before 1960 or not).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;66    data have ;
67       date_opened = date();
68       output ;
69       date_opened = datetime();
70       output;
71    run;

NOTE: The data set WORK.HAVE has 2 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


72
73    data want;
74     set have;
75
76    if datepart(Date_Opened) in (-1,0) then Year_Month = substr(put(Date_Opened,yymmdd10.),1,7);
77    else Year_Month = substr(put(datepart(Date_Opened),yymmdd10.),1,7);
78
79    put (_all_) (=);
80    run;

date_opened=21544 Year_Month=2018-12
date_opened=1861458217.6 Year_Month=2018-12&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Dec 2018 20:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-based-on-Date-format/m-p/523584#M142254</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-26T20:44:51Z</dc:date>
    </item>
  </channel>
</rss>

