Subscribe Bookmark RSS Feed

Formula for Grouping months by season

Highlighted
Onjai

Occasional Contributor

Joined:

Jul 12, 2017

Hi,

I thought this was a rather simple formula to create, but found myself receiving formatting errors.

I have a column, character type, that contains months (October, November, December, and so on...).  I created a new column called "season" and wrote an IF(Contains(...formula to claasifiy if the month is October Then = fall,  and so on through the list creating the 4 categories (summer, winter, spring, fall).  However, the CONTAINS will not take the OR.

i.e.

If(Contains(:month, "October" | "November"), "fall", Empty())

 

What am I missing?  Any assistance would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Take a look at the 'Match()' function in 'Help > Scripting Index'.

5 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Take a look at the 'Match()' function in 'Help > Scripting Index'.

Onjai

Occasional Contributor

Joined:

Jul 12, 2017

Ian,

Worked fine!  Nice and clean.

Thank you and the rest of the fine folks in the jmp community.

Cheers!

txnelson

Super User

Joined:

Jun 22, 2012

If(Contains(:month, "October") | Contains(:month, "November"), "fall", "")

The above is the correct syntax for the approach you were taking.  However, Ian's approach is a more readable, and therefore more proper method

Jim
pmroz

Super User

Joined:

Jun 23, 2011

You can search a column for a list of values.  Here's a formula that uses that approach.

dt = New Table( "test", Add Rows( 12 ), 
	New Column( "Month", Character, "Nominal",
		Set Values(
			{"January", "February", "March", "April", "May", "June", "July",
			"August", "September", "October", "November", "December"}
		)
	),
	New Column( "Season",
		Character,
		"Nominal",
		Formula(
			spring_list = {"March", "April", "May"};
			summer_list = {"June", "July", "August"};
			fall_list   = {"September", "October", "November"};
			winter_list = {"December", "January", "February"};
			If( Contains( spring_list, :Month ), "Spring",
				Contains( summer_list, :Month ), "Summer",
				Contains( fall_list,   :Month ), "Fall",
				Contains( winter_list, :Month ), "Winter",
				
			);
		)
	)
);
dale_lehman

Community Trekker

Joined:

Jan 29, 2015

As Jim points out, the sytax requires that the OR statement repeat the column = within the statement.  Match does work better, but why not just use the column Recode utility (which will produce the match formula)?