<?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: Date Extraction in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910478#M359053</link>
    <description>&lt;P&gt;You did the right thing for the month name.&amp;nbsp; &amp;nbsp;Generated the name using a date format.&lt;/P&gt;
&lt;P&gt;But you did the wrong thing for the day of the week name.&amp;nbsp; You converted the date into a number between 1 and 31 and then formatted using a date format.&amp;nbsp; So you get the day of the week for a date between 02JAN1960 and 01FEB1960 instead of for the actual date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the MONNAME and DOWNAME formats are weird. They will right align by default.&amp;nbsp; So use the -L format modifier to avoid having values with leading spaces.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dates_as_strings;
  today=date();
  format today date9.;
  year= put(today,year4.);
  month=put(today,monname.-L);
  day_of_week=put(today,downame.-L);
  put today= (_character_) (/=$quote.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;today=04JAN2024
year="2024"
month="January"
day_of_week="Thursday"
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 04 Jan 2024 15:38:51 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-01-04T15:38:51Z</dc:date>
    <item>
      <title>Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910458#M359048</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I was extracting Year, Month Name and Day Name from Date column. But the problem that I faced during the extraction of day name. In result till 31-01-2022 day name is showing correctly and when 01-02-2022 starts wrong day name is getting printed it continues till 31-01-2023 which is the last date in my dataset.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;For Example on 01-02-2022 day should be Tuesday but result I am getting is Saturday.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Contents of table" style="width: 359px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92268i449BD5CAB2831C3F/image-size/large?v=v2&amp;amp;px=999" role="button" title="c.JPG" alt="Contents of table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Contents of table&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Snapshot of result of I am getting" style="width: 754px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92271i8FB3370470773807/image-size/large?v=v2&amp;amp;px=999" role="button" title="r.JPG" alt="Snapshot of result of I am getting" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Snapshot of result of I am getting&lt;/span&gt;&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My Code:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA formated_date;
  SET date;
   Year = YEAR(Date);
   Month_Name = PUT(Date, MONNAME.);
   Day_Name = DAY(Date);
   FORMAT Day_Name downame9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please tell me why I am getting wrong results?&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 15:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910458#M359048</guid>
      <dc:creator>ketan_korde</dc:creator>
      <dc:date>2024-01-04T15:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910461#M359049</link>
      <description>&lt;P&gt;It would be helpful if you could show us (a portion of) the data in SAS data set DATE, along with the PROC CONTENTS of this data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, as a guess, I think you want this, as the DOWNAME format applies to SAS date values, not to the day number extracted to a variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   Day_Name = put(date,downame9.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;although I don't recommend doing it this way. You don't need to store the day_name as a character string at all, just leave it as a date, and apply formats to it as needed.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 15:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910461#M359049</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-04T15:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910468#M359051</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You for your reply&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you suggested, I updated my post by attaching POC CONTENTS and results snapshots.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 15:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910468#M359051</guid>
      <dc:creator>ketan_korde</dc:creator>
      <dc:date>2024-01-04T15:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910471#M359052</link>
      <description>&lt;P&gt;So&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/436399"&gt;@ketan_korde&lt;/a&gt;&amp;nbsp;this brings up the question ... why do you need YEAR in its own variable, why do you need MONTH in its own variable, and why do you need DAY in its own variable? Usually these steps are not needed, and you can just work with the SAS date value ... please explain why you are doing this, how will these new variables be used?&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 15:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910471#M359052</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-04T15:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910478#M359053</link>
      <description>&lt;P&gt;You did the right thing for the month name.&amp;nbsp; &amp;nbsp;Generated the name using a date format.&lt;/P&gt;
&lt;P&gt;But you did the wrong thing for the day of the week name.&amp;nbsp; You converted the date into a number between 1 and 31 and then formatted using a date format.&amp;nbsp; So you get the day of the week for a date between 02JAN1960 and 01FEB1960 instead of for the actual date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the MONNAME and DOWNAME formats are weird. They will right align by default.&amp;nbsp; So use the -L format modifier to avoid having values with leading spaces.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dates_as_strings;
  today=date();
  format today date9.;
  year= put(today,year4.);
  month=put(today,monname.-L);
  day_of_week=put(today,downame.-L);
  put today= (_character_) (/=$quote.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;today=04JAN2024
year="2024"
month="January"
day_of_week="Thursday"
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 15:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910478#M359053</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-04T15:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910484#M359057</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;, I recently started learning SAS Programming Language and was practicing different date functions and formats.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 16:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910484#M359057</guid>
      <dc:creator>ketan_korde</dc:creator>
      <dc:date>2024-01-04T16:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910490#M359062</link>
      <description>&lt;P&gt;Hii&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Today I learned something new.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 16:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910490#M359062</guid>
      <dc:creator>ketan_korde</dc:creator>
      <dc:date>2024-01-04T16:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910495#M359064</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/436399"&gt;@ketan_korde&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;, I recently started learning SAS Programming Language and was practicing different date functions and formats.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Okay, that's a good reason to learn how to use the formats on date values, but just so that you know, in most cases you are better off working with the actual date value, rather than putting "Wednesday" into a new variable, and also having "March" in a new variable. This is unnecessary and inefficient programming in most cases. Even if you wanted to compute averages by day of week, you can do this on the DATE value, without storing "Monday" "Tuesday" etc in its own variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=fake_dates;
     var date;
     format date downame.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally, you apply a format to the date, rather than create a new variable.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jan 2024 16:43:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910495#M359064</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-04T16:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: Date Extraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910496#M359065</link>
      <description>Okay, I will keep this in mind while working on data.&lt;BR /&gt;&lt;BR /&gt;Thank you for the notes.</description>
      <pubDate>Thu, 04 Jan 2024 16:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Extraction/m-p/910496#M359065</guid>
      <dc:creator>ketan_korde</dc:creator>
      <dc:date>2024-01-04T16:57:06Z</dc:date>
    </item>
  </channel>
</rss>

