<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script recode values for multiple columns using Associative Array and loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Script-recode-values-for-multiple-columns-using-Associative/m-p/649126#M84160</link>
    <description>&lt;P&gt;How do you build your mapping for column names? Are the original columns always in same format? I make the assumption in my example script that they are, so those can be looped over&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

// this is original datatable 
dt_data = Open("help_data.jmp");

// this is mapping file
dt_mapping = Open("help_med_code.jmp");

//The associative array 
aa_mapping = Associative Array(:number, :med);
aa_mapping &amp;lt;&amp;lt; Set Default Value("MISSING MAPPING INFO");

// build column name mapping
aa_colnames = Associative Array();
For Each({col_name}, dt_data &amp;lt;&amp;lt; Get Column Names("Character", "String"), 
	If(Starts With(col_name, "med_"),
		aa_colnames[col_name] = Substitute(col_name, "med_m", "drugname_");
	)
);

For Each({{orig_col, new_col}}, aa_colnames,
	dt_data &amp;lt;&amp;lt; New Column(new_col, Character, Nominal, &amp;lt;&amp;lt; Set Each Value(
		aa_mapping[Column(dt_data, orig_col)[Row()]]
	));
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 22 Jun 2023 13:56:22 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2023-06-22T13:56:22Z</dc:date>
    <item>
      <title>Script recode values for multiple columns using Associative Array and loop</title>
      <link>https://community.jmp.com/t5/Discussions/Script-recode-values-for-multiple-columns-using-Associative/m-p/647163#M84150</link>
      <description>&lt;P&gt;Hello, JMP user community,&lt;/P&gt;&lt;P&gt;Very often I need to re-code values and I like to use a file with mapping keys (attached, help_med_code.jmp).&lt;/P&gt;&lt;P&gt;From this mapping data I made the associative array and to re-code values in my dataset (help_data.jmp) I re-code values for each column.&lt;/P&gt;&lt;P&gt;Sometimes I have a lot of columns and using the loop would be preferable.&lt;/P&gt;&lt;P&gt;However, my script for loop doesn’t work (attached, help2.jsl).&lt;/P&gt;&lt;P&gt;Please, help to use the associative array in the loop. Other suggestions for re-coding are also will be greatly appreciated.&lt;/P&gt;&lt;P&gt;I hope I didn’t make any silly typo(s) and didn’t repeat the question if it had been already answered here. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Best to you all,&lt;/P&gt;&lt;P&gt;Valentina&lt;/P&gt;&lt;P&gt;JMP® 16.2.0 (570548)&lt;BR /&gt;Microsoft Windows 10 Enterprise 64-bit (10.0.19044.0)&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2023 21:03:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Script-recode-values-for-multiple-columns-using-Associative/m-p/647163#M84150</guid>
      <dc:creator>Valentina</dc:creator>
      <dc:date>2023-06-21T21:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: Script recode values for multiple columns using Associative Array and loop</title>
      <link>https://community.jmp.com/t5/Discussions/Script-recode-values-for-multiple-columns-using-Associative/m-p/649126#M84160</link>
      <description>&lt;P&gt;How do you build your mapping for column names? Are the original columns always in same format? I make the assumption in my example script that they are, so those can be looped over&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

// this is original datatable 
dt_data = Open("help_data.jmp");

// this is mapping file
dt_mapping = Open("help_med_code.jmp");

//The associative array 
aa_mapping = Associative Array(:number, :med);
aa_mapping &amp;lt;&amp;lt; Set Default Value("MISSING MAPPING INFO");

// build column name mapping
aa_colnames = Associative Array();
For Each({col_name}, dt_data &amp;lt;&amp;lt; Get Column Names("Character", "String"), 
	If(Starts With(col_name, "med_"),
		aa_colnames[col_name] = Substitute(col_name, "med_m", "drugname_");
	)
);

For Each({{orig_col, new_col}}, aa_colnames,
	dt_data &amp;lt;&amp;lt; New Column(new_col, Character, Nominal, &amp;lt;&amp;lt; Set Each Value(
		aa_mapping[Column(dt_data, orig_col)[Row()]]
	));
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Jun 2023 13:56:22 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Script-recode-values-for-multiple-columns-using-Associative/m-p/649126#M84160</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-06-22T13:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Script recode values for multiple columns using Associative Array and loop</title>
      <link>https://community.jmp.com/t5/Discussions/Script-recode-values-for-multiple-columns-using-Associative/m-p/650603#M84181</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;I got data together with labels/dictionary for the data.&lt;/P&gt;&lt;P&gt;I am glad the script works.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2023 21:48:05 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Script-recode-values-for-multiple-columns-using-Associative/m-p/650603#M84181</guid>
      <dc:creator>Valentina</dc:creator>
      <dc:date>2023-06-22T21:48:05Z</dc:date>
    </item>
  </channel>
</rss>

