<?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: filtering by date using where statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602825#M174572</link>
    <description>&lt;P&gt;Correcting your Where statement(methinks the following should do)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where put(birthmo,yymmn6. -l) = '201801';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Nov 2019 19:24:20 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-11-08T19:24:20Z</dc:date>
    <item>
      <title>filtering by date using where statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602822#M174571</link>
      <description>&lt;P&gt;I've searched the sas community and have tried changing the format of my where statement and can't figure out why sas won't recognize the rows with the date '201801'. I'm a beginner coder and suspect its my lack of fully understanding date formats that is creating the problem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I have a date variable 'dob' which is formatted mm/dd/yyyy. I create a new variable 'birthmo' from the dob variable to create a date with the format yyyymm. The variable is created correctly (or so I think) and when I do a frequency on birthmo it shows 44k occurences of 201801. But when I try and filter the data using 'where birthmo = '201801'd;' I get this error:&lt;/P&gt;&lt;P&gt;ERROR: Invalid date/time/datetime constant '201801'd.&lt;BR /&gt;ERROR: Syntax error while parsing WHERE clause.&lt;/P&gt;&lt;P&gt;Or if I change the code to read 'where birthmo = 201801;' it comes up with an empty dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CODE:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data cohort ;&lt;BR /&gt;set m.mpcf_201801 m.mpcf_201802 m.mpcf_201803;&lt;BR /&gt;birthmo = dob;&lt;BR /&gt;FORMAT birthmo yymmn6.;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data cohort2;&lt;BR /&gt;set cohort;&lt;BR /&gt;where birthmo = '201801'd;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample DATA:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;individual id&lt;/TD&gt;&lt;TD&gt;dob&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1345&lt;/TD&gt;&lt;TD&gt;10/20/2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;315345&lt;/TD&gt;&lt;TD&gt;6/5/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;465&lt;/TD&gt;&lt;TD&gt;9/28/2010&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11354&lt;/TD&gt;&lt;TD&gt;1/1/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1341&lt;/TD&gt;&lt;TD&gt;1/23/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;352&lt;/TD&gt;&lt;TD&gt;1/24/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4652&lt;/TD&gt;&lt;TD&gt;2/8/2013&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;435&lt;/TD&gt;&lt;TD&gt;5/14/2009&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;122&lt;/TD&gt;&lt;TD&gt;1/18/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1253&lt;/TD&gt;&lt;TD&gt;6/19/2004&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;463&lt;/TD&gt;&lt;TD&gt;7/10/2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;78986&lt;/TD&gt;&lt;TD&gt;1/22/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25123&lt;/TD&gt;&lt;TD&gt;1/2/2012&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 19:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602822#M174571</guid>
      <dc:creator>chelseah</dc:creator>
      <dc:date>2019-11-08T19:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: filtering by date using where statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602825#M174572</link>
      <description>&lt;P&gt;Correcting your Where statement(methinks the following should do)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where put(birthmo,yymmn6. -l) = '201801';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 19:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602825#M174572</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-08T19:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: filtering by date using where statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602827#M174573</link>
      <description>&lt;P&gt;Literal values &lt;STRONG&gt;must be &lt;/STRONG&gt;in DATE9 or DATE7 appearance:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'01JAN2019'D or '01JAN19'd , a syntax rule because there are so many random collections of digits masquerading as dates. Did you that today , 8 Nov 2019 could be 2019312? or even worse 19312?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AND they must be a complete date, day month and year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or explicitly use the formatted value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where put(birthmo,yymmn6.) = '201810';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 19:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602827#M174573</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-08T19:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: filtering by date using where statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602831#M174577</link>
      <description>&lt;P&gt;Using the&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'xxxxxxx'd&amp;nbsp;&amp;nbsp; or "xxxxxxx"d&lt;/P&gt;
&lt;P&gt;is the SAS convention for specifying a &lt;EM&gt;&lt;STRONG&gt;date literal&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;&amp;nbsp; (like 5 is a &lt;EM&gt;&lt;STRONG&gt;numeric literal&lt;/STRONG&gt;&lt;/EM&gt; and "abc" is a &lt;EM&gt;&lt;STRONG&gt;character literal&lt;/STRONG&gt;&lt;/EM&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But there is only one&amp;nbsp;format allowed in a date literal.&amp;nbsp; In your case you would want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where '01jan2010'd &amp;lt;= dob &amp;lt;='31jan2010'd;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The important thing to understand is that is doesn't matter at all what the format of DOB is - the underlying numeric value (number of days after 01jan1060) is the same - it's just displayed differently according to the assigned format of DOB.&amp;nbsp; So specifying the date-literals in the where&amp;nbsp;statement&amp;nbsp;as I have merely inserts the number of days after jan 1, 1960&amp;nbsp;&amp;nbsp;that represent the dates 01jan2010 and 31jan2010.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also have&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where year(dob)=2018 and month(dob)=1;&lt;/P&gt;
&lt;P&gt;which may be much more intuitive, but requires executing&amp;nbsp;two functions for every observation.&amp;nbsp; Using the date literal bounds requires no such overhead - the actual numeric values are inserted once and for all when your program is being compiled, prior to execution.&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>Fri, 08 Nov 2019 19:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602831#M174577</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-08T19:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: filtering by date using where statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602833#M174579</link>
      <description>&lt;P&gt;It's like magic!&amp;nbsp; No need to create the birthmo variable either! Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 19:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602833#M174579</guid>
      <dc:creator>chelseah</dc:creator>
      <dc:date>2019-11-08T19:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: filtering by date using where statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602835#M174581</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/92233"&gt;@chelseah&lt;/a&gt;&amp;nbsp; That's right. While I am one extremely lazy person, please do pay attention to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;'s explanation of SAS dates that are merely numbers formatted for display while internal stored value is in tact. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 19:36:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602835#M174581</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-08T19:36:12Z</dc:date>
    </item>
    <item>
      <title>Re: filtering by date using where statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602838#M174584</link>
      <description>&lt;P&gt;Yes, I have taken trainings regarding dates and I realize how they are stored, but for some reason I can't seem to apply that knowledge to writing code. Once I think I understand, I come across a problem, like today. I'll keep trying to consume the literature and hopefully it will click one day.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 19:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/filtering-by-date-using-where-statement/m-p/602838#M174584</guid>
      <dc:creator>chelseah</dc:creator>
      <dc:date>2019-11-08T19:40:20Z</dc:date>
    </item>
  </channel>
</rss>

