BookmarkSubscribe
Choose Language Hide Translation Bar
Paul_J
Community Trekker

Arbitrary Recode Script

I have a character variable with an unknown number of levels.  The levels have long text names and I'd like to recode them into something shorter, such as just 1, 2, 3, etc... 

I understand how to use Match to recode a given set of levels to other specific values, but would like to ask how to do this if the initial set is unknown.  That is, for each new data file the variable to recoded may have a different number of different entries.

Thanks!

 

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Arbitrary Recode Script

Here is a small piece of JSL that gives an example of creating a numerical value from the character values found in a column

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Animals.jmp" );

dt = New Column( "Animal Category",
	formula(
		If( Row() == 1,
			Summarize( bygroup = by( :species ) )
		);
		Loc( bygroup, :species )[1];
	)
);
Jim

View solution in original post

6 REPLIES 6
txnelson
Super User

Re: Arbitrary Recode Script

Here is a small piece of JSL that gives an example of creating a numerical value from the character values found in a column

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Animals.jmp" );

dt = New Column( "Animal Category",
	formula(
		If( Row() == 1,
			Summarize( bygroup = by( :species ) )
		);
		Loc( bygroup, :species )[1];
	)
);
Jim

View solution in original post

Paul_J
Community Trekker

Re: Arbitrary Recode Script

That worked great!   Thanks very much!

 

0 Kudos
Paul_J
Community Trekker

Re: Arbitrary Recode Script

Seems I spoke to soon ... that code is creating the desired new variable, but now all my code after that does not work.  In particular I tried to move the new variable, then exclude some rows.  My move and exclude worked fine before, but now the new variable becomes excluded and hidden?

Is there something that needs to be re-set or turned off?  (By the way, I'm using Version 12)

 

Thanks!

0 Kudos
Paul_J
Community Trekker

Re: Arbitrary Recode Script

PS., Seems somehow dt is being assigned that new column.  The original data table is no longer referenced by dt.  Is that right?

0 Kudos
Highlighted
txnelson
Super User

Re: Arbitrary Recode Script

oops,

the New Column statement should be

dt << New Column(.................
Jim
0 Kudos
Paul_J
Community Trekker

Re: Arbitrary Recode Script

A-ha!  I thought that "dt =" was unusual.  Turned out I got around it by writing "dt2 = ".  Likely a bit inefficient but it worked.  I've since changed it to "dt <<" and it works great as well.

 

Thanks Again!

0 Kudos