Subscribe Bookmark RSS Feed

adding prefix to Character column values in an efficient way

newbie_alex

Contributor

Joined:

May 18, 2017

Hello,

I have a character column with an unknown number of different values.

 

I would like to add a string prefix to each value but I struggle with doing so in an efficient way without creating a new column and without a for loop because it is slow.

 

Example for a single value:

Value is "12"

new value should be "PREFIX 12"

 

Is there a way to do this with JMP 12?

 

Here is the code without the step to add the prefix:

Names Default to Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt:Age << Data Type( Character );
// Get the values of age into a vector
ageVals = dt:Age << getValues;
// do not know what to do here. would like to add a PREFIX without a for loop (which would be slow)
???
// Put the new values back into the column
dt:Age << setValues(ageVals);

 

Best regards,

Alex

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson

Super User

Joined:

Jun 22, 2012

Solution

I believe the quickest way to do this would be

Names Default to Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt:Age << Data Type( Character );
for each row(
	dt:Age = "PREFIX " || dt:Age
);
Jim
3 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

Solution

I believe the quickest way to do this would be

Names Default to Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt:Age << Data Type( Character );
for each row(
	dt:Age = "PREFIX " || dt:Age
);
Jim
newbie_alex

Contributor

Joined:

May 18, 2017

Thank you.

 

That is actually quicker than expected even for my big data set.

 

Would you please help me understand why this is quicker than a regular for loop?

txnelson

Super User

Joined:

Jun 22, 2012

I suspect the reason this is faster, is because it is a function that has very specific actions, and therefore has been optimized.

Jim