Publication date: 07/24/2024

Recode Columns

Use the Recode Column message to change all of the values in a column at once. Specify the original column, or reference column, a list of transformations, and a target column. The function applies the transformations to the reference column, updating the target column. If the Update Properties argument is turned on, the following column properties are updated when a column is recoded: Value Labels, Value Scores, Value Order, Value Colors, Supercategories, Coding, and Missing Value Codes.

obj << Recode Column(<column reference>, {<transform>, ...}, <Update Properties(0|1)>, Target Column(<column reference> | <column name>))

Two special JSL variables are populated during the execution of Recode Column:

_rcNow

the current value of the input, after any potential previous transformations.

_rcOrig

the original value of the input.

Initially, before any transformations have taken place, _rcNow and _rcOrig have the same value.

For example, in Consumer Preferences.jmp, change the coding of the Gender column to 0|1 instead of 1|2, but keep the value labels the same.

dt = Open( "$SAMPLE_DATA/Consumer Preferences.jmp" );
col = New Column( :Gender );
dt << Recode Column(
	:Gender,
	{if(
		_rcNow == 1, 0,
		1,
	)},
	Update Properties( 1 ),
	Target Column( col )
);

You can also make the transformations to the original column. Recode the age column to age groups in Big Class.jmp.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << Recode Column(
	:age,
	{if(
		_rcNow >= 17, "Older",
		_rcNow >= 15, "Middle",
		"Younger"
	)},
	Target Column( :age)
);

In the following example, both _rcOrig and _rcNow are used to recode a column. Note how _rcOrig is used to avoid the new value of CAROL from being merged with the original value of CHRIS, and both being changed to ’Christine’.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
	dt << Begin Data Update;
	col1 = dt << New Column( dt:name );
	col1 << Set Name( "name Titlecase" );
	dt << Move Selected Columns( {col1}, after( dt:name ) );
	dt << Recode Column(
		dt:name,
		{Titlecase( _rcOrig ),
		Map Value(
		_rcOrig,
		{"CAROL", "Chris",
		"CHRIS", "Christine"},
		Unmatched( _rcNow ) //The value after the transformation.
		)},
			Update Properties( 1 ),
			Target Column( col1 )
	);
	dt << End Data Update;
Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).