<?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: Grab the month and year of a date variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244780#M268525</link>
    <description>&lt;P&gt;Without looking over your shoulder and seeing the steps you applied, we're guessing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's my guess.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You applied the datepart function an extra time.&amp;nbsp; It was already applied once, converting the datetime variable to a date.&amp;nbsp; Then you applied it a second time, converting the date to January 1, 1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try printing the variable without a format and see what the actual value is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
    <pubDate>Wed, 20 Jan 2016 18:11:51 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-01-20T18:11:51Z</dc:date>
    <item>
      <title>Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244762#M268516</link>
      <description>&lt;P&gt;Say we have a time variable, in date9, or datetime18. format. I would like to grab only the month and year part of the value and save it. For example, turn 31Dec2015 to Dec2015. datepart() function grabs the specific date of a time value, is there something similar to that but only grabs the month and year? Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 17:14:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244762#M268516</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2016-01-20T17:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244765#M268517</link>
      <description>&lt;P&gt;SAS has&amp;nbsp;functions for both year and month if you pass it a numeric date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;year(date) or&lt;/P&gt;
&lt;P&gt;month(date)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These will be numeric results.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 17:20:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244765#M268517</guid>
      <dc:creator>JoshB</dc:creator>
      <dc:date>2016-01-20T17:20:01Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244769#M268518</link>
      <description>&lt;P&gt;You already know how to apply the DATEPART function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;d = datepart(dt_var);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try applying the right format to the result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;format d monyy7.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 17:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244769#M268518</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-20T17:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244770#M268519</link>
      <description>&lt;P&gt;Thank you. They grab only the year or the month of a time value respectively, and save it as regular numeric format, not date format. Is there another function that grabs both the year and month of a certain time value, and preferably save it as a date format?&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 17:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244770#M268519</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2016-01-20T17:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244771#M268520</link>
      <description>&lt;P&gt;With the DT version of that format no need for new variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;2579  data _null_;
2580     x = datetime();
2581     put x=dtmonyy7.;
2582     run;

x=JAN2016&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jan 2016 17:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244771#M268520</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-01-20T17:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244772#M268521</link>
      <description>&lt;P&gt;I tried it. I comes out to be always JAN1960, double click on it shows "1/1/1960". What may be causing this?&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 17:45:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244772#M268521</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2016-01-20T17:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244774#M268522</link>
      <description>&lt;P&gt;SAS Date values are the number of days since 1/1/1960. If your "date" values show as 1/1/1960 then the way you created them was incorrect as they are resolving to something between 0 and 2. Print the data or change the display with a format like best10. and see what the values are then.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 17:53:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244774#M268522</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-20T17:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244776#M268523</link>
      <description>I did this:&lt;BR /&gt;&lt;BR /&gt;data test;&lt;BR /&gt;set test;&lt;BR /&gt;put date=MONYY7.;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;However, the "date" variable has not changed from before, still shows the entire date and seems to be date9. format.</description>
      <pubDate>Wed, 20 Jan 2016 17:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244776#M268523</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2016-01-20T17:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244777#M268524</link>
      <description>&lt;P&gt;Because you have a datetime variable use dtFORMAT name instead of just the format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 18:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244777#M268524</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-20T18:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244780#M268525</link>
      <description>&lt;P&gt;Without looking over your shoulder and seeing the steps you applied, we're guessing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's my guess.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You applied the datepart function an extra time.&amp;nbsp; It was already applied once, converting the datetime variable to a date.&amp;nbsp; Then you applied it a second time, converting the date to January 1, 1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try printing the variable without a format and see what the actual value is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 18:11:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244780#M268525</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-20T18:11:51Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244782#M268526</link>
      <description>&lt;P&gt;Here's a sample that demonstrates the issue:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
date = dhms(today(), 11, 15, 0); /*Datetime variable*/
dtdate =datepart(date); /*Date variable*/

put date = datetime21.;
put date = dtdate9.;
put date = dtmonyy7.;
put dtdate = date9.;
put dtdate = monyy7.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date=20JAN2016:11:15:00
date=20JAN2016
date=JAN2016
dtdate=20JAN2016
dtdate=JAN2016
NOTE: The data set WORK.SAMPLE has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 18:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244782#M268526</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-20T18:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244786#M268527</link>
      <description>that's indeed what i did. the raw data came in with date and time, i first did datetime() to get ride of the time part. later to aggregate over month, i want to make another variable capturing only the month and year.&lt;BR /&gt;&lt;BR /&gt;i have tried keeping the raw date_time variable, and making 2 variables, one with date9, format, the other with monyy7. format, both using datepart(date_time) function, but they come out the same, both still containing the day of the month. so how would i fix this?</description>
      <pubDate>Wed, 20 Jan 2016 18:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244786#M268527</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2016-01-20T18:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244788#M268528</link>
      <description>&lt;P&gt;While a number of the suggested approaches would work, I'll repeat my original idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After applying DATEPART only once, print the result using the MONYY7 format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 18:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244788#M268528</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-20T18:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244789#M268529</link>
      <description>by doing that i can get the variable to show something like "DEC2015", but double click on it it will still show the day of the month, which is what i want to lose, so i can aggregate over all days of the same month. right now when i do that, it will distinguish between 1/1/2014 and 1/2/2014 and refuse to add them up, even though the variable made with monyy7. only shows Jan2014.</description>
      <pubDate>Wed, 20 Jan 2016 18:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244789#M268529</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2016-01-20T18:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244790#M268530</link>
      <description>&lt;P&gt;That depends on the proc, PROC SQL will distinguish between the days, but PROC MEANS/FREQ will respect the format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, your question initially stated you wanted to create a date variable. A date variable requires a day.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see a few&amp;nbsp;options:&lt;/P&gt;
&lt;P&gt;1. Convert all to character fields - dates will not sort properly in your tables (APR is first month to show)&lt;/P&gt;
&lt;P&gt;2. Change all dates to either 1st or 15th of the month so they'll aggregate together.&lt;/P&gt;
&lt;P&gt;3. Use procs above, which respect date format.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 18:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244790#M268530</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-20T18:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244791#M268531</link>
      <description>&lt;P&gt;Even though the underlying value changes day by day, most SAS procedures that summarize will respect the format and group by the formatted value instead of the underlying value.&lt;/P&gt;
&lt;P&gt;To take things step by step and guarantee the result you want, create a new variable with the month and year.&amp;nbsp; After applying DATEPART once:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;category = put(date_only, monyy7.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, the values you get will not necessarily be in the order that you want.&amp;nbsp; You might want to try a format that contains the year followed by the month, like 1960-01 to keep the values in order.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 18:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244791#M268531</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-20T18:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244797#M268532</link>
      <description>&lt;P&gt;You don't specify if you want the output to be a date or character.&amp;nbsp; Here is a solution for both:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile cards dsd;&lt;BR /&gt;informat date1 date9.;&lt;BR /&gt;format date1 date9.;&lt;BR /&gt;input date1;&lt;BR /&gt;cards;&lt;BR /&gt;31Dec2015&lt;BR /&gt;;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;date2 = date1;&lt;BR /&gt;format date2 monyy7.;&lt;BR /&gt;date3=input(put(date2,monyy7.),$7.);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 19:09:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244797#M268532</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2016-01-20T19:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244830#M268533</link>
      <description>&lt;P&gt;Thank you all for helping out. It turns out proc sql has to interpret a date variable by the specific day, no matter how hard you try to hide it by using format. The solution is either to use proc means to get aggregates, or turn date variable into character variables. The former proved to be more natural, as it retains the chronological order of months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 21:33:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244830#M268533</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2016-01-20T21:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the month and year of a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244834#M268534</link>
      <description>&lt;P&gt;Or possibly leave the datetime value alone and use a format like DTMONYY7. when you need to use the date time grouped at month level.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 21:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grab-the-month-and-year-of-a-date-variable/m-p/244834#M268534</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-20T21:45:18Z</dc:date>
    </item>
  </channel>
</rss>

