Choose Language Hide Translation Bar
Highlighted

## 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
Highlighted

## 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;

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

Learn it once, use it forever!
2 REPLIES 2
Highlighted

## 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;

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

Learn it once, use it forever!
Highlighted

## 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;
For( i = 1, i <= N Cols( dt ), i++,
cName = Column( dt, i ) << Get Name;
newname = prefix ||"_"||cName;
Column( dt, i ) << Set Name( newname ); );``````

Article Labels

There are no labels assigned to this post.
{"ft_lang_label":"__label__en","ft_lang_prob":0.61224574,"math_prob":0.9255442,"size":1906,"snap":"2020-34-2020-40","text_gpt3_token_len":572,"char_repetition_ratio":0.13249211,"word_repetition_ratio":0.63185376,"special_character_ratio":0.34312695,"punctuation_ratio":0.20140515,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9751468,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-09T20:45:20Z\",\"WARC-Record-ID\":\"<urn:uuid:d6f8318f-df2f-43b7-bed6-33cd742c1119>\",\"Content-Length\":\"681883\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c1157983-e039-4177-a260-b4b32911bf8e>\",\"WARC-Concurrent-To\":\"<urn:uuid:0cd80961-81b4-4115-8eeb-98a95a78e352>\",\"WARC-IP-Address\":\"208.74.205.23\",\"WARC-Target-URI\":\"https://community.jmp.com/t5/Discussions/Add-prefix-to-column-names-scripting/m-p/226713\",\"WARC-Payload-Digest\":\"sha1:RW3T3SR6XL7XZB2TTEPHOPTVZ6754QKZ\",\"WARC-Block-Digest\":\"sha1:7BY4ZQVOFPPRMKT3XURUQT25CWTQKGV5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738573.99_warc_CC-MAIN-20200809192123-20200809222123-00445.warc.gz\"}"}