<?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: deriving a variable for 'seasons' from data data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/564062#M158186</link>
    <description>&lt;P&gt;I favor the second option with a function and format created from it.&lt;/P&gt;
&lt;P&gt;The only thing I would do different is using a select() instead of the if/then/else chain:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib= work.funcs.season;
function whatseason (date) $ 15;
  length return $ 15;
  /*code*/
  select (month(date));
    when (1, 2, 12) return="Summer";
    when (3, 4, 5) return="Autumn";
    when (6, 7, 8) return="Winter";
    when (9, 10, 11) return="Spring";
    otherwise return="????";
  end;
  return(return);
endsub;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Jun 2019 12:56:24 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-06-06T12:56:24Z</dc:date>
    <item>
      <title>deriving a variable for 'seasons' from data data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/563923#M158131</link>
      <description>&lt;P&gt;Open to suggestions, please:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I need to derive a variable for seasons, so that Dec-Jan-Feb="Summer" mar-apr-may="autumn"&amp;nbsp; jun-jul-aug="winter' and sep-oct-nov="spring"&lt;BR /&gt;The data for dates that I have is this:&lt;BR /&gt;&lt;BR /&gt;DATETIME_OF_SERVICE (datetime20.)&lt;BR /&gt;date_of_service (date9.)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;All my attempts have failed miserably so far - any handy hints on this one?&lt;/P&gt;&lt;P&gt;may thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 23:50:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/563923#M158131</guid>
      <dc:creator>drshashlik</dc:creator>
      <dc:date>2019-06-05T23:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: deriving a variable for 'seasons' from data data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/563933#M158138</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value mth2sson 1-3= 'Winter' 4-6='Spring';
run;
data WANT; 
  X=put(month(datepart('01jan2019:0:0'dt)),mth2sson.);
  Y=put(month('01apr2019'd),mth2sson.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;X&amp;nbsp; &amp;nbsp; &amp;nbsp; Y&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Winter Spring&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 00:31:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/563933#M158138</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-06-06T00:31:54Z</dc:date>
    </item>
    <item>
      <title>Re: deriving a variable for 'seasons' from data data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/563934#M158139</link>
      <description>&lt;P&gt;There might be a couple of different ways to do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One way would be to create a format for it. The problem is that you will have to create four ranges for each year, this is not that hard, but you need to know the year ranges so to speak. So, basically you have a macro that takes start year and end year, creates all the rows needed in a table and then do a proc format with cntlin that points to the table you just created. Good thing with this is that you have no need for extra columns in your data.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro createFormat(startYear,endYear);
	data _format;
		length label $ 20;
		fmtname = "seasons";
		%do _i=&amp;amp;startYear %to &amp;amp;endYear;
			%do _month = 1 %to 12;
				start = mdy(&amp;amp;_month,1,&amp;amp;_i);
				end=intnx("month",start,0,"E");;
				month = month(start);
				if month in (1, 2, 12) then label="Summer";
				else if month in (3, 4, 5) then label="Autumn";
				else if month in (6, 7, 8) then label="Winter";
				else if month in (9, 10, 11) then label="Spring";
				output;
			%end;
		%end;
		HLO = "O";
		start = .;
		end = .;
		label = "Unknown";
		output;
		keep fmtname start end label;
	run;
	proc format cntlin=_format;
	run;
%mend createFormat;
%createFormat(2000,2020);

data want;
	input mydate date9.;
	format mydate seasons.;
datalines;
15dec2017
15mar2017
15Jun2017
15Sep2017
.
;;; 
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The second way would be to create a new column with the month number in it and then create a format that just look at the month number and reformat this. But, you have to add a column to all the tables that you want to have this season variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can also use proc fcomp to create a function that is used to evaluate the date for you.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib= work.funcs.season;
	function whatseason (date) $ 15;
		length return $ 15;
		/*code*/
		if month(date) in (1, 2, 12) then return="Summer";
		else if month(date) in (3, 4, 5) then return="Autumn";
		else if month(date) in (6, 7, 8) then return="Winter";
		else if month(date) in (9, 10, 11) then return="Spring";
		else return="????";
		return(return);
	endsub;
run;
options cmplib=work.funcs;

proc format;
	value season (default=20) other = [whatseason()];
run;

data want;
	input mydate date9.;
	format mydate season.;
datalines;
15dec2017
15mar2017
15Jun2017
15Sep2017
.
;;; 
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 01:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/563934#M158139</guid>
      <dc:creator>heffo</dc:creator>
      <dc:date>2019-06-06T01:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: deriving a variable for 'seasons' from data data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/564062#M158186</link>
      <description>&lt;P&gt;I favor the second option with a function and format created from it.&lt;/P&gt;
&lt;P&gt;The only thing I would do different is using a select() instead of the if/then/else chain:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib= work.funcs.season;
function whatseason (date) $ 15;
  length return $ 15;
  /*code*/
  select (month(date));
    when (1, 2, 12) return="Summer";
    when (3, 4, 5) return="Autumn";
    when (6, 7, 8) return="Winter";
    when (9, 10, 11) return="Spring";
    otherwise return="????";
  end;
  return(return);
endsub;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jun 2019 12:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/deriving-a-variable-for-seasons-from-data-data/m-p/564062#M158186</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-06T12:56:24Z</dc:date>
    </item>
  </channel>
</rss>

