cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
MFVIT
Level IV

jsl script to recode multiple columns

Dear All,

I am trying to recode multiple columns by using the following script (also in attachment together with an example data table). I tried several changes but it does not work. The values in the columns just disappears instead of being recoded. 

I appreciate any help.

Many thanks,
Marcello.

 

 

dt3 = Current Data Table();
n3 = dt3 << Get Column Names();
Show( n3 );

lstColNames_n3 = dt3 << Get Column Names();
Show( lstColNames_n3 );


Show( lstCols_n3 );
lstCols_n3 = {};
For( i = 1, i <= N Items( lstColNames_n3 ), i++,
    colName = lstColNames_n3[i];
    col = Column( dt3, i );
    Insert Into( lstCols_n3, col );
);
Show( lstCols_n3 );

For Each Row(
    For( i = 1, i <= N Items( lstColNames_n3 ), i++,
        As Column( lstColNames_n3[i] ) = Match( As Column( lstColNames_n3[i] ),
            "1", "too low",
            "2", "too low",
            "3", "right",
            "4", "too high",
            "5", "too high"
        )
    )
);

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: jsl script to recode multiple columns

Here is the solution using the Recode feature written in JSL It is much faster than doing row by row processing

Names Default To Here( 1 );
dmdt = current data table();

nc = dmdt << get column names( Character );

For( i = 1, i <= N Items( nc ), i++,
	col = Column( dt, nc[i] );
	colname = col << getname();

	Eval(
		Parse(
			Eval Insert(
				"\[
		dmdt << Recode Column(
			:Name("^colname^"),
			{Map Value( _rcOrig, 
			{	"1", "too low",
				"2", "too low",
				"3", "right",
				"4", "too high",
				"5", "too high"}, 
			Unmatched( _rcNow ) )},
			Update Properties( 1 ),
			Target Column( :Name("^colname^") )
		);
	]\"
			)
		)
	);
);

  

Jim

View solution in original post

8 REPLIES 8
jthi
Super User

Re: jsl script to recode multiple columns

Do you have to recode values or could you possibly use column property Value Labels (this way you won't loose the original data), example: Recode multiple columns with JSL script 

 

Edit:

And if you have to recode them in place, there are multiple ways to do it (I would use Associative Array):

Names Default To Here(1);

dt3 = current data Table ();
n3 = dt3 << Get Column Names(string);
show(n3);
//create associative array to use matching keys to values
matchAa = Associative Array(
	{"1","2","3","4","5"}, 
	{"too low","too low","right","too high","too high"}
);
show(matchAa);

//loop over column names
For(i = 1, i <= N Items(n3), i++,
	//refer to column and use set each value to update values
	Column(dt3, n3[i]) << Set Each Value(
		matchAa[AsColumn(n3[i])[Row()]]
	);
);

And for your solution I think there is an issue in how you refer to values in Match function and you have to add [Row()] to get single values for Matching

Match(As Column( lstColNames_n3[i])[Row()], 

Edit2:

Added full example script as attachment

-Jarmo
MFVIT
Level IV

Re: jsl script to recode multiple columns

I would need to recode values. Thanks.
jthi
Super User

Re: jsl script to recode multiple columns

I edited my previous reply, it should help with the recoding
-Jarmo
MFVIT
Level IV

Re: jsl script to recode multiple columns

Thank you very much, your code with the associative array worked perfectly.

I tried also the other option, by adding 

Match(As Column( lstColNames_n3[i])[Row()], 

 but that did not work.

But I solved my problem.

Many thanks.

txnelson
Super User

Re: jsl script to recode multiple columns

Here is the solution using the Recode feature written in JSL It is much faster than doing row by row processing

Names Default To Here( 1 );
dmdt = current data table();

nc = dmdt << get column names( Character );

For( i = 1, i <= N Items( nc ), i++,
	col = Column( dt, nc[i] );
	colname = col << getname();

	Eval(
		Parse(
			Eval Insert(
				"\[
		dmdt << Recode Column(
			:Name("^colname^"),
			{Map Value( _rcOrig, 
			{	"1", "too low",
				"2", "too low",
				"3", "right",
				"4", "too high",
				"5", "too high"}, 
			Unmatched( _rcNow ) )},
			Update Properties( 1 ),
			Target Column( :Name("^colname^") )
		);
	]\"
			)
		)
	);
);

  

Jim
MFVIT
Level IV

Re: jsl script to recode multiple columns

Many thanks!

ThuongLe
Level IV

Re: jsl script to recode multiple columns

One work-around solution would be creating a new column with formula based on the column you want to recode.
Then delete the original column
Thuong Le
MFVIT
Level IV

Re: jsl script to recode multiple columns

Thanks!