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

Datatable indexing Problem within a Formula

I am trying to apply the log10 function to an entire column within a data table.  The result will be in a new column with a new name.  The new column and name are generated by I get an error in the formula portion.  Below are the results of the script and the error message.

jump1.JPGjump2.JPG

 

This is the script:

 

Names Default To Here(1);
dt = As Table(J(5, 5, Random Uniform()), <<column names({"X1", "X2", "X3", "X4", "X5"}));
ColNames = dt << Get Column Names(String);

For(i = 1, i <= N Items(ColNames), i++, 
     // Get name of column to work    
    	ex2 = (ColNames[i]);

    //place prefix on column name
   	ex = "Ln_";

    //concatenate prefix and column name
   	ex3 = ex || (Char(ex2));

    //Create new column and put results on log of original column in new column
   	dt << New Column(ex3, "Numeric", formula(Ln(:ex2)));

);
 
Thanks for your help in advance.
Ray
1 ACCEPTED SOLUTION

Accepted Solutions
vince_faller
Super User (Alumni)

Re: Datatable indexing Problem within a Formula

Here's just the script.  But definitely the article is more in depth.  

 

Names Default To Here(1);
dt = As Table(J(5, 5, Random Uniform()), <<column names({"X1", "X2", "X3", "X4", "X5"}));
ColNames = dt << Get Column Names(String);

For(i = 1, i <= N Items(ColNames), i++, 
    col_name = ColNames[i];
    Eval(
		EvalExpr(// if you just evaluate this block you'll see that the col_name var has been replaced
			dt << New Column("Ln_"||col_name, numeric, formula(
				ln(Expr(Column(dt, col_name))[]) 
			));
		)
	);
);
Vince Faller - Predictum

View solution in original post

2 REPLIES 2
jthi
Super User

Re: Datatable indexing Problem within a Formula

You need to evaluate the column name into the formula. This is a good post about it Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute 

-Jarmo
vince_faller
Super User (Alumni)

Re: Datatable indexing Problem within a Formula

Here's just the script.  But definitely the article is more in depth.  

 

Names Default To Here(1);
dt = As Table(J(5, 5, Random Uniform()), <<column names({"X1", "X2", "X3", "X4", "X5"}));
ColNames = dt << Get Column Names(String);

For(i = 1, i <= N Items(ColNames), i++, 
    col_name = ColNames[i];
    Eval(
		EvalExpr(// if you just evaluate this block you'll see that the col_name var has been replaced
			dt << New Column("Ln_"||col_name, numeric, formula(
				ln(Expr(Column(dt, col_name))[]) 
			));
		)
	);
);
Vince Faller - Predictum