BookmarkSubscribeRSS Feed
drshashlik
Fluorite | Level 6

Open to suggestions, please: 

I need to derive a variable for seasons, so that Dec-Jan-Feb="Summer" mar-apr-may="autumn"  jun-jul-aug="winter' and sep-oct-nov="spring"
The data for dates that I have is this:

DATETIME_OF_SERVICE (datetime20.)
date_of_service (date9.)


All my attempts have failed miserably so far - any handy hints on this one?

may thanks!

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

One way:

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;

X      Y
Winter Spring

heffo
Pyrite | Level 9

There might be a couple of different ways to do this.

 

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. 

%macro createFormat(startYear,endYear);
	data _format;
		length label $ 20;
		fmtname = "seasons";
		%do _i=&startYear %to &endYear;
			%do _month = 1 %to 12;
				start = mdy(&_month,1,&_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; 

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. 

 

You can also use proc fcomp to create a function that is used to evaluate the date for you.

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; 

 

 

Kurt_Bremser
Super User

I favor the second option with a function and format created from it.

The only thing I would do different is using a select() instead of the if/then/else chain:

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1254 views
  • 1 like
  • 4 in conversation