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

I need to populate a new column based on results of an if/then statement

Hi - I am trying to create and populate a group of new columns based on results of an if/then statement.  

 

STEP 1:

I need to create the new empty columns that will be populated.  (55 in all) 

(Each new column is a numeric, continuous field.)  

The name of each column will correspond to the name of each of the 55 compounds in the group followed by "_ppt".  E.g. the compound named "4:2 FTS" results in a new column named [4:2 FTS_ppt].

 

STEP 2:  

Row by row, I would populate the new columns using an if/then statement that evaluates three existing columns associated with each of the 55 compounds in the group.   Specifically,

if  [RES_VALUE_4:2 FTS] >=  [RL_4:2 FTS]       THEN      [4:2 FTS_ppt] = [RES_VALUE_4:2 FTS]

 

if [RES_VALUE_4:2 FTS] <  [RL_4:2 FTS]     AND     DETECT_FLAG_4:2 FTS] = N      AND    [RL_4:2 FTS] <= 3    THEN  [4:2 FTS_ppt] = 0.0009.    

 

if [RES_VALUE_4:2 FTS] <  [RL_4:2 FTS]     AND     DETECT_FLAG_4:2 FTS] = N      AND    [RL_4:2 FTS] > 3    THEN  [4:2 FTS_ppt] = NULL.

 

Here are the existing column names with a suffix that is the compound name.  I am using JMP Pro 17.0.

learning_JSL_1-1693949204065.pnglearning_JSL_2-1693949229746.png

learning_JSL_3-1693949262413.png

 

Any guidance would be most appreciated!

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: I need to populate a new column based on results of an if/then statement

Two things needed to be changed.  

  1. The value of the variable x that is used in the formula needs to be initialized for each rows execution.
  2. The formula execution need to be UNsuppressed.
Names Default To Here( 1 );
groupList = {"RES_VALUE", "RES_TEXT", "DETECT_FLAG", "RL", "DILUTION_FACTOR"};

dt = Current Data Table();

// Find all of the different compounds.  This is done by using the "RES_VALUE" columns
// and stripping off the compound names from each of them
compList = {};
For( i = 1, i <= N Cols( dt ), i++,
	colName = Column( dt, i ) << get name;
	If( Contains( colName, "RES_VALUE" ) == 1,
		Insert Into( compList, Substr( colName, 11 ) )
	);
);

// Create the columns
For Each( {comp, i}, compList,
	Eval(
		Parse(
			"dt << New Column( \!"" || compList[i] || "_ppt" ||
			"\!",
						numeric,
						continuous, formula(
						    x=.;
							If( :\!"RES_VALUE "
			 || comp || "\!"n >= :\!"RL " || comp || "\!"n,
								x = :\!"RES_VALUE " || comp ||
			"\!"n
							);
							If(
								:\!"RES_VALUE " ||
			comp || "\!"n < :\!"RL " || comp || "\!"n & :\!"DETECT_FLAG " || comp || "\!"n == \!"N\!" &
								:\!"RL "
			 || comp || "\!"n <= 3,
								x = 0.0009
							);
							If(
								:\!"RES_VALUE "
			 || comp || "\!"n < :\!"RL " || comp || "\!"n & :\!"DETECT_FLAG " || comp || "\!"n == \!"N\!" &
								:\!"RL "
			 || comp || "\!"n > 3,
								x = .
							);
							x;
						)
					);"
		)
	)
);
dt << Suppress Formula Eval( 0 );
Jim

View solution in original post

8 REPLIES 8
txnelson
Super User

Re: I need to populate a new column based on results of an if/then statement

  1. The purpose of the Discussion Community is not to be a source for JMP users to get scripts written at their request.  It is there to assist users in learning JMP.
  2. Given the pseudo code you provided, it is clear that you need to take the time to read the JMP Scripting Guide.  It is available in the JMP Documentation Library under the Help pull down menu in JMP.
  3. Below is an attempt to help you with your issue.  It is not tested, since I do not have any of your data.  Please take the time to study and learn the code.  The functions are well documented in the Scripting Index, and in the JSL Scripting Reference.
    Names Default To Here( 1 );
    groupList = {"RES_VALUE", "RES_TEXT", "DETECT_FLAG", "RL", "DILUTION_FACTOR"};
    
    dt = Current Data Table();
    
    // Find all of the different compounds.  This is done by using the "RES_VALUE" columns
    // and stripping off the compound names from each of them
    compList = {};
    For( i = 1, i <= N Cols( dt ), i++,
    	colName = Column( dt, i ) << get name;
    	If( Contains( colName, "RES_VALUE" ) == 1,
    		Insert Into( compList, Substr( colName, 11 ) )
    	);
    );
    
    // Create the columns
    For Each( {comp, i}, compList,
    	Eval(
    		Parse(
    			"dt << New Column( \!"" || compList[i] || "_ppt" || "\!",
    						numeric,
    						continuous,
    						formula(
    							If( :\!"RES_VALUE "
    			 || comp || "\!"n >= :\!"RL " || comp || "\!"n,
    								x = :\!"RES_VALUE " || comp ||
    			"\!"n
    							);
    							If(
    								:\!"RES_VALUE " || comp ||
    			"\!"n < :\!"RL " || comp || "\!"n & :\!"DETECT_FLAG " || comp || "\!"n == \!"N\!" &
    								:\!"RL " || comp ||
    			"\!"n <= 3,
    								x = 0.0009
    							);
    							If(
    								:\!"RES_VALUE "
    			 || comp || "\!"n < :\!"RL " || comp || "\!"n & :\!"DETECT_FLAG " || comp || "\!"n == \!"N\!" &
    								:\!"RL "
    			 || comp || "\!"n > 3,
    								x = .
    							);
    							x;
    						)
    					);"
    		)
    	)
    );
Jim
learning_JSL
Level IV

Re: I need to populate a new column based on results of an if/then statement

Thanks Jim.  Yes, point well taken.  I need to spend more time in the foundations of jsl before jumping into my work.

 

The code above created all of the columns correctly but the formula is producing only null values (".").  I haven't been able to troubleshoot the error but will keep trying.  Thanks again. 

txnelson
Super User

Re: I need to populate a new column based on results of an if/then statement

I will take a look at it if you can supply some sample data.

Jim
learning_JSL
Level IV

Re: I need to populate a new column based on results of an if/then statement

Thanks Jim.  I attached the jmp table below that contains a small subset of rows from my dataset.

txnelson
Super User

Re: I need to populate a new column based on results of an if/then statement

Two things needed to be changed.  

  1. The value of the variable x that is used in the formula needs to be initialized for each rows execution.
  2. The formula execution need to be UNsuppressed.
Names Default To Here( 1 );
groupList = {"RES_VALUE", "RES_TEXT", "DETECT_FLAG", "RL", "DILUTION_FACTOR"};

dt = Current Data Table();

// Find all of the different compounds.  This is done by using the "RES_VALUE" columns
// and stripping off the compound names from each of them
compList = {};
For( i = 1, i <= N Cols( dt ), i++,
	colName = Column( dt, i ) << get name;
	If( Contains( colName, "RES_VALUE" ) == 1,
		Insert Into( compList, Substr( colName, 11 ) )
	);
);

// Create the columns
For Each( {comp, i}, compList,
	Eval(
		Parse(
			"dt << New Column( \!"" || compList[i] || "_ppt" ||
			"\!",
						numeric,
						continuous, formula(
						    x=.;
							If( :\!"RES_VALUE "
			 || comp || "\!"n >= :\!"RL " || comp || "\!"n,
								x = :\!"RES_VALUE " || comp ||
			"\!"n
							);
							If(
								:\!"RES_VALUE " ||
			comp || "\!"n < :\!"RL " || comp || "\!"n & :\!"DETECT_FLAG " || comp || "\!"n == \!"N\!" &
								:\!"RL "
			 || comp || "\!"n <= 3,
								x = 0.0009
							);
							If(
								:\!"RES_VALUE "
			 || comp || "\!"n < :\!"RL " || comp || "\!"n & :\!"DETECT_FLAG " || comp || "\!"n == \!"N\!" &
								:\!"RL "
			 || comp || "\!"n > 3,
								x = .
							);
							x;
						)
					);"
		)
	)
);
dt << Suppress Formula Eval( 0 );
Jim
learning_JSL
Level IV

Re: I need to populate a new column based on results of an if/then statement

Thank you Jim.  I've meant to circle back shortly after your reply but have been trying to dubug the code.  I have not gotten it to work for some reason.  I used the debugger and got this message:

 

learning_JSL_0-1694202790574.png

 

Just wanted you to know I received your input - I will keep trying.  Thanks.

learning_JSL
Level IV

Re: I need to populate a new column based on results of an if/then statement

learning_JSL_1-1694202910732.png

 

learning_JSL
Level IV

Re: I need to populate a new column based on results of an if/then statement

Yes!   I just got it to work.  Thanks again for your help on this!