cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
Newbie2Jumpie
Level IV

Add prefix to column names (scripting)

Hi

 

I have a a JMP dataset and want to a add the same prefix to all the column names in that dataset, my code below doesn't do quite what I want.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt2 = dt << For( i = 25, i <= N Cols( dt ), i++,
	cName = Column( dt, i ) << Get Name;
	newname = cName || "Prefix_";
	Column( dt, i ) << Set Name( newname );
);

 

Now, a second scenario:

Imagine the column "sex" in "Big Class" contains Fs only (or Ms).

How could I the access that contant and use it as a prefix by the help of a utility/macro variable in the code above?

Cheers, Newbie

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Add prefix to column names (scripting)

First, For() is a function, not a message for a data table, that does not return a reference to a new data table. Second, why start the loop index at 25? Third, why concatenate a prefix after the name? That result is a suffix.

 

Names Default to Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

prefix = dt:sex[1];

For( i = 1, i <= N Cols( dt ), i++,
	cName = Column( dt, i ) << Get Name;
	newname = prefix || cName;
	Column( dt, i ) << Set Name( newname );
);

 

View solution in original post

2 REPLIES 2

Re: Add prefix to column names (scripting)

First, For() is a function, not a message for a data table, that does not return a reference to a new data table. Second, why start the loop index at 25? Third, why concatenate a prefix after the name? That result is a suffix.

 

Names Default to Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

prefix = dt:sex[1];

For( i = 1, i <= N Cols( dt ), i++,
	cName = Column( dt, i ) << Get Name;
	newname = prefix || cName;
	Column( dt, i ) << Set Name( newname );
);

 

Newbie2Jumpie
Level IV

Re: Add prefix to column names (scripting)

I'm sorry, Mark. I didn't notice the buffer didn't contain=paste the most recent JSL code variant.

Anyway, I tweaked it a tiny bit...

 

Names Default to Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
prefix = dt:sex[1];
For( i = 1, i <= N Cols( dt ), i++,
cName = Column( dt, i ) << Get Name;
newname = prefix ||"_"||cName;
Column( dt, i ) << Set Name( newname ); );