<?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: Turn a date into new variable as season in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763006#M30416</link>
    <description>&lt;P&gt;The result of a logical expression like you have, ( A or B or C), is always going to be either TRUE (which SAS represent by the number 1) or FALSE ( the number 0) so it will most likely never be equal to the value of SEASON.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to test if a variable is one of a series of values either put the OR operator between each of the individual comparison , (x=a or x=b or x=c), or use the IN operator, (x in (a b c)).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you need to test the value that is stored in the variable, not the way the value is displayed when printed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FORMATS convert values to text.&amp;nbsp; When you attach them to a variable it changes how the values are displayed, but it does not change the value that is stored.&amp;nbsp; So your month variable does not contain the string 'January'. It contains a number like 21,918 which represents some date in January.&lt;/P&gt;</description>
    <pubDate>Sat, 21 Aug 2021 02:21:48 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-08-21T02:21:48Z</dc:date>
    <item>
      <title>Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763001#M30412</link>
      <description>&lt;P&gt;*SAS Question: How do I create a new variable "Season" to categorize my month values by season as a character variable?&lt;BR /&gt;I can't seem to get the season to populate correctly, instead i recieve a sas number in the new variable no matter what format I choose or how I add the new season variable*;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*This code chunk works*;&lt;/P&gt;&lt;P&gt;*DISTRIBUTION BY MONTH;&lt;BR /&gt;DATA want;&lt;BR /&gt;SET have;&lt;BR /&gt;datepart=datepart(startdate);&lt;BR /&gt;format datepart date9.;&lt;/P&gt;&lt;P&gt;MonthYear = datepart(startdate);&lt;BR /&gt;format Monthyear MONYY.;&lt;/P&gt;&lt;P&gt;Month = datepart(startdate);&lt;BR /&gt;format Month MonName.;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*Thus code chunk does not work*;&lt;/P&gt;&lt;P&gt;DATA want (KEEP= STARTDATE DATEPART MONTHYEAR MONTH SEASON);&lt;BR /&gt;SET have;&lt;BR /&gt;&lt;BR /&gt;SEASON=put(MONTH, $6.);&lt;BR /&gt;&lt;BR /&gt;IF SEASON= "DEC" OR "JAN" OR "FEB" THEN SEASON = "WINTER";&lt;BR /&gt;IF SEASON= "MAR" OR "APR" OR "MAY" THEN SEASON = "SPRING";&lt;BR /&gt;IF SEASON= "JUN" OR "JUL" OR "AUG" THEN SEASON = "SUMMER";&lt;BR /&gt;IF SEASON= "SEPT" OR "OCT" OR "NOV" THEN SEASON = "AUTUMN";&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;_______________VARIABLE PROPERTIES&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;STARTDATE&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PROPERTY VALUE&lt;/P&gt;&lt;P&gt;LABEL STARTDATE&lt;/P&gt;&lt;P&gt;NAME STARTDATE&lt;/P&gt;&lt;P&gt;LENGTH 8&lt;/P&gt;&lt;P&gt;TYPE NUMERIC&lt;/P&gt;&lt;P&gt;FORMAT DATETIME19.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DATEPART&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PROPERTY VALUE&lt;/P&gt;&lt;P&gt;LABEL DATEPART&lt;/P&gt;&lt;P&gt;NAME DATEPART&lt;/P&gt;&lt;P&gt;LENGTH 8&lt;/P&gt;&lt;P&gt;TYPE NUMERIC&lt;/P&gt;&lt;P&gt;FORMAT DATE9.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;MONTHYEAR&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PROPERTY VALUE&lt;/P&gt;&lt;P&gt;LABEL MONTHYEAR&lt;/P&gt;&lt;P&gt;NAME MONTHYEAR&lt;/P&gt;&lt;P&gt;LENGTH 8&lt;/P&gt;&lt;P&gt;TYPE NUMERIC&lt;/P&gt;&lt;P&gt;FORMAT MONYY.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;MONTH&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PROPERTY VALUE&lt;/P&gt;&lt;P&gt;LABEL MONTH&lt;/P&gt;&lt;P&gt;NAME MONTH&lt;/P&gt;&lt;P&gt;LENGTH 8&lt;/P&gt;&lt;P&gt;TYPE NUMERIC&lt;/P&gt;&lt;P&gt;FORMAT MONNAME&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SEASON&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PROPERTY VALUE&lt;/P&gt;&lt;P&gt;LABEL SEASON&lt;/P&gt;&lt;P&gt;NAME SEASON&lt;/P&gt;&lt;P&gt;LENGTH 6&lt;/P&gt;&lt;P&gt;TYPE CHAR&lt;/P&gt;&lt;P&gt;FORMAT ---&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-inline" image-alt="Screen Shot 2021-08-20 at 5.52.04 PM.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62793i74E77049E3548B04/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2021-08-20 at 5.52.04 PM.png" alt="Screen Shot 2021-08-20 at 5.52.04 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Aug 2021 23:54:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763001#M30412</guid>
      <dc:creator>Deanna_Payne</dc:creator>
      <dc:date>2021-08-20T23:54:21Z</dc:date>
    </item>
    <item>
      <title>Re: Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763003#M30413</link>
      <description>&lt;P&gt;Why are you trying to use a character format with the numeric variable month?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you comparing the value of SEASON to the logical expression (&lt;SPAN&gt;"DEC" OR "JAN" OR "FEB"&amp;nbsp;) ?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The easiest way to convert a date to season is to take the QTR number of the next month.&amp;nbsp; That way DEC-FEB will map to QTR #1.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Example code&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input dt :datetime. ;
  date1=datepart(dt);
  date2=date1;
  format dt datetime19. date1 date9. date2 monyy7. ;
  month_num = month(date1);
  length season $6 ;
  select (qtr(intnx('month',date1,1)));
   when (1) season='WINTER';
   when (2) season='SPRING';
   when (3) season='SUMMER';
   when (4) season='AUTUMN';
  end;
cards;
01JAN2020:01:02
01FEB2020:01:02
01MAR2020:01:02
01APR2020:01:02
01MAY2020:01:02
01JUN2020:01:02
01JUL2020:01:02
01AUG2020:01:02
01SEP2020:01:02
01OCT2020:01:02
01NOV2020:01:02
01DEC2020:01:02
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&lt;SPAN&gt;Results&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;                                                       month_
 Obs                     dt        date1      date2      num     season
   1     01JAN2020:01:02:00    01JAN2020    JAN2020       1      WINTER
   2     01FEB2020:01:02:00    01FEB2020    FEB2020       2      WINTER
   3     01MAR2020:01:02:00    01MAR2020    MAR2020       3      SPRING
   4     01APR2020:01:02:00    01APR2020    APR2020       4      SPRING
   5     01MAY2020:01:02:00    01MAY2020    MAY2020       5      SPRING
   6     01JUN2020:01:02:00    01JUN2020    JUN2020       6      SUMMER
   7     01JUL2020:01:02:00    01JUL2020    JUL2020       7      SUMMER
   8     01AUG2020:01:02:00    01AUG2020    AUG2020       8      SUMMER
   9     01SEP2020:01:02:00    01SEP2020    SEP2020       9      AUTUMN
  10     01OCT2020:01:02:00    01OCT2020    OCT2020      10      AUTUMN
  11     01NOV2020:01:02:00    01NOV2020    NOV2020      11      AUTUMN
  12     01DEC2020:01:02:00    01DEC2020    DEC2020      12      WINTER&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Sat, 21 Aug 2021 02:10:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763003#M30413</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-21T02:10:37Z</dc:date>
    </item>
    <item>
      <title>Re: Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763004#M30414</link>
      <description>*Sorry I made the adjustment to this code chunk to reflect the month format seen in the picture-- Still doesn't work *;&lt;BR /&gt;&lt;BR /&gt;DATA WORK.CO_PATIENT_SPI_ALT3 (KEEP= STARTDATE DATEPART MONTHYEAR MONTH SEASON);&lt;BR /&gt;SET WORK.CO_PATIENT_SPI_ALT2;&lt;BR /&gt;&lt;BR /&gt;SEASON=put(MONTH, $6.);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;IF SEASON= "December" OR "January" OR "February" THEN SEASON = "WINTER";&lt;BR /&gt;IF SEASON= "March" OR "April" OR "May" THEN SEASON = "SPRING";&lt;BR /&gt;IF SEASON= "June" OR "July" OR "Aug" THEN SEASON = "SUMMER";&lt;BR /&gt;IF SEASON= "September" OR "October" OR "November" THEN SEASON = "AUTUMN";&lt;BR /&gt;RUN;</description>
      <pubDate>Sat, 21 Aug 2021 01:48:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763004#M30414</guid>
      <dc:creator>Deanna_Payne</dc:creator>
      <dc:date>2021-08-21T01:48:21Z</dc:date>
    </item>
    <item>
      <title>Re: Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763005#M30415</link>
      <description>It doesn't have to be logical expression but thats what I could think of to encompass the months I wanted. I am open to any suggestions.</description>
      <pubDate>Sat, 21 Aug 2021 01:50:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763005#M30415</guid>
      <dc:creator>Deanna_Payne</dc:creator>
      <dc:date>2021-08-21T01:50:00Z</dc:date>
    </item>
    <item>
      <title>Re: Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763006#M30416</link>
      <description>&lt;P&gt;The result of a logical expression like you have, ( A or B or C), is always going to be either TRUE (which SAS represent by the number 1) or FALSE ( the number 0) so it will most likely never be equal to the value of SEASON.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to test if a variable is one of a series of values either put the OR operator between each of the individual comparison , (x=a or x=b or x=c), or use the IN operator, (x in (a b c)).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you need to test the value that is stored in the variable, not the way the value is displayed when printed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FORMATS convert values to text.&amp;nbsp; When you attach them to a variable it changes how the values are displayed, but it does not change the value that is stored.&amp;nbsp; So your month variable does not contain the string 'January'. It contains a number like 21,918 which represents some date in January.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Aug 2021 02:21:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763006#M30416</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-21T02:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763007#M30417</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/344395"&gt;@Deanna_Payne&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;*Sorry I made the adjustment to this code chunk to reflect the month format seen in the picture-- Still doesn't work *;&lt;BR /&gt;&lt;BR /&gt;DATA WORK.CO_PATIENT_SPI_ALT3 (KEEP= STARTDATE DATEPART MONTHYEAR MONTH SEASON);&lt;BR /&gt;SET WORK.CO_PATIENT_SPI_ALT2;&lt;BR /&gt;&lt;BR /&gt;SEASON=put(MONTH, $6.);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;IF SEASON= "December" OR "January" OR "February" THEN SEASON = "WINTER";&lt;BR /&gt;IF SEASON= "March" OR "April" OR "May" THEN SEASON = "SPRING";&lt;BR /&gt;IF SEASON= "June" OR "July" OR "Aug" THEN SEASON = "SUMMER";&lt;BR /&gt;IF SEASON= "September" OR "October" OR "November" THEN SEASON = "AUTUMN";&lt;BR /&gt;RUN;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your Month value is the same as you created with "Month = datepart(startdate);" then Month would be numeric value of some sort and Put (month,$6) would never contain any text that looks at all like a month name.&lt;/P&gt;
&lt;P&gt;Plus Doesn't work because you have created Season to be 6 characters long with the $6. format. So can never be "December" (8 characters), "January" (7 characters), "February" (8 characters) and several other those values are also longer than the possible 6 characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's what you get with Season=put(month,$6.);&lt;/P&gt;
&lt;PRE&gt;142  data junk;
143     startdate='15Jan2021:12:20:30'dt;
144     month=datepart(startdate);
145     season=put(month,$6.);
WARNING: Variable month has already been defined as numeric.
146     put season=;
147  run;

season=22295
&lt;/PRE&gt;
&lt;P&gt;That 22295 is what your Season variable values look like. So you never get a month name to use.&lt;/P&gt;
&lt;P&gt;You should also pay attention to the Warning about Month as numeric. That is another clue that your should not be using the $6 format with the value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And did you even look at the output? You would see that Season looks like 5-digit numbers for a wide range of likely recent dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A generic comment:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;Doesn't work is awful vague.&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;Are there errors in the log?: Post the code and log in a code box opened with the "&amp;lt;&amp;gt;" to maintain formatting of error messages.&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;No output? Post any log in a code box.&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "&amp;lt;&amp;gt;" icon or attached as text to show exactly what you have and that we can test code against.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to see the text of a month from a date value you would use the MONNAME format. If you want to compare to the full name of a month then you would use&lt;/P&gt;
&lt;P&gt;Season=put(month, Monname9.);&amp;nbsp; (The reason the MONNAME format defaults to Monname9 is so all the characters of the month names in English appear)&lt;/P&gt;
&lt;P&gt;And your code would have to include "August" , not "Aug".&lt;/P&gt;
&lt;P&gt;Or use Monname3. and compare to "Dec" "Jan" "Feb" , the first three letters of the month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Aug 2021 15:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763007#M30417</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-21T15:11:23Z</dc:date>
    </item>
    <item>
      <title>Re: Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763092#M30423</link>
      <description>&lt;P&gt;Your IF statements are wrong, and SAS formats don’t change the underlying values. Instead, use the month numbers which is a bit easier. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;If season= “Jan” or “Feb” is wrong, it should use IN or be &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;If season = 'Jan' or season='Feb'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;This is better:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*extract month from the datetime variable startDate;
Month = month(datepart(startDate));

*categorize as seasons;
If month in (1, 2, 3) then Season = 'Winter';
Else if month in (4:6) then Season = 'Spring';&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Aug 2021 16:09:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763092#M30423</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-23T16:09:05Z</dc:date>
    </item>
    <item>
      <title>Re: Turn a date into new variable as season</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763146#M30424</link>
      <description>&lt;P&gt;As mentioned by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, learn to use SAS date values (which are days since 01JAN1960) and then compute time periods using SAS functions and formats, which rely on numeric values of SAS dates, rather than trying to create your own calculations using character strings. The MONTH function, working on valid SAS date values, which produces values 1 through 12 gets you there. SAS has done the hard work for you, so you don't have to.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 11:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-a-date-into-new-variable-as-season/m-p/763146#M30424</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-22T11:08:52Z</dc:date>
    </item>
  </channel>
</rss>

