cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
ENTHU
Level IV

Help needed with nested if and else statement

I'm using if else statement to bucket certain FS*/SS* as CAT1 and CAT2 .All remaining FS*/SS* that do not belong to CAT1/CAT2 need to be classified as CAT3.

Having trouble with writing else part for CAT3.Any help appreciated.

 

cdt = open("temp\data.csv");

cdt=current data table();
sum_lst2 = {};
sum_lst3 = {};
sum_lst4 = {};
col_lst2 = cdt << get column names( Numeric );
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If( Contains( {"SS008","SS009","SS010","SS015","SS053","FS9429","FS9792","FS9906","FS9908", "FS1302","FS1305","FS1308","FS1360","FS1373","FS2744","FS4621","FS4625","FS4630","FS4640","FS4681","FS4830"}, colname ) | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450"),
		Insert Into( sum_lst2, col_lst2[i] )
	); ///Cat1//	
	
	If( Contains( {"SS098","SS090"}, colname ) | (colname >= "FS9700" & colname <= "FS9791") | (colname >= "FS9793" & colname <= "FS9799"),
		Insert Into( sum_lst4, col_lst2[i] )
	);//Cat2//
	If( Contains( {"SS019","SS027","SS035","FS9901"}, colname ) | (colname >= "FS3451" & colname <= "FS3499"),
		Insert Into( sum_lst3, col_lst2[i] )
	);//Cat3
);
ItemFS0100FS0847FS0875FS0876FS0877FS1059FS1061FS1090FS1315FS1501FS1503FS1505FS1537FS1539FS1541FS1543FS1559FS1590FS1930FS1933FS2706FS2740FS2742FS2745FS3416FS3417FS3418FS3422FS3423FS3455FS3469FS3471FS3492FS3493FS5391FS9429FS9792FS9901FS9906SS001SS008SS010SS013SS015SS019SS027SS034SS053SS094SS097SS099
11321                3                   11321   3     1
26002                72 14   5  14    11126002 0 9510 113
3562    1            9  11  111211112   21562 1  9211   3
45272 21 21 7 24332 241823  12 1 1 4 1113  252753 452041013 2
5322 1     1 2     1 7   3          2    33221 13732  03

 

1 ACCEPTED SOLUTION

Accepted Solutions
Joseph_Reese
Level IV

Re: Help needed with nested if and else statement

How about something like the below? I didn't actually see any "else" statements in your script since you used the glue operator between If() statements. See below where I have nested all your If() statements and then had the final else as the Insert Into the sum_lst4 list (assuming that was where you wanted the Cat4 column names.

 

cdt = Current Data Table();
sum_lst2 = {};
sum_lst3 = {};
sum_lst4 = {};
col_lst2 = cdt << get column names( Numeric );
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If(
		Contains(
			{"SS008", "SS009", "SS010", "SS015", "SS053", "FS9429", "FS9792", "FS9906", "FS9908", "FS1302", "FS1305", "FS1308", "FS1360", "FS1373",
			"FS2744", "FS4621", "FS4625", "FS4630", "FS4640", "FS4681", "FS4830"},
			colname
		) | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450"),
		Insert Into( sum_lst2, col_lst2[i] ), ///Cat1//	
	
		If( Contains( {"SS098", "SS090"}, colname ) | (colname >= "FS9700" & colname <= "FS9791") | (colname >= "FS9793" & colname <= "FS9799"),
			Insert Into( sum_lst4, col_lst2[i] ), //Cat2//
			
			If( Contains( {"SS019", "SS027", "SS035", "FS9901"}, colname ) | (colname >= "FS3451" & colname <= "FS3499"),
				Insert Into( sum_lst3, col_lst2[i] ),
				Insert Into( sum_lst4, col_lst2[i] )///final nested else statement for Cat4
			);//Cat3
		)
	);
);

View solution in original post

9 REPLIES 9
Joseph_Reese
Level IV

Re: Help needed with nested if and else statement

How about something like the below? I didn't actually see any "else" statements in your script since you used the glue operator between If() statements. See below where I have nested all your If() statements and then had the final else as the Insert Into the sum_lst4 list (assuming that was where you wanted the Cat4 column names.

 

cdt = Current Data Table();
sum_lst2 = {};
sum_lst3 = {};
sum_lst4 = {};
col_lst2 = cdt << get column names( Numeric );
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If(
		Contains(
			{"SS008", "SS009", "SS010", "SS015", "SS053", "FS9429", "FS9792", "FS9906", "FS9908", "FS1302", "FS1305", "FS1308", "FS1360", "FS1373",
			"FS2744", "FS4621", "FS4625", "FS4630", "FS4640", "FS4681", "FS4830"},
			colname
		) | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450"),
		Insert Into( sum_lst2, col_lst2[i] ), ///Cat1//	
	
		If( Contains( {"SS098", "SS090"}, colname ) | (colname >= "FS9700" & colname <= "FS9791") | (colname >= "FS9793" & colname <= "FS9799"),
			Insert Into( sum_lst4, col_lst2[i] ), //Cat2//
			
			If( Contains( {"SS019", "SS027", "SS035", "FS9901"}, colname ) | (colname >= "FS3451" & colname <= "FS3499"),
				Insert Into( sum_lst3, col_lst2[i] ),
				Insert Into( sum_lst4, col_lst2[i] )///final nested else statement for Cat4
			);//Cat3
		)
	);
);
ENTHU
Level IV

Re: Help needed with nested if and else statement

I mistakenly copied wrong code in my question.Below is the code I wrote for if/else implementation but column CAT3 only has zeros.Is there an issue with the placement of else statement.

 

To reiterate I'm trying to bucket this into 2 categories,whatever values do not belong to CAT1/2 need to be bucketed as CAT3.

 

cdt = open("temp\Rawdata.csv");
cdt = Current Data Table();
sum_lst2 = {};
sum_lst3 = {};
sum_lst4 = {};
col_lst2 = cdt << get column names( Numeric );
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If(
		Contains(
			{"SS008", "SS009", "SS010", "SS015", "SS053", "FS9429", "FS9792", "FS9906", "FS9908", "FS1302", "FS1305", "FS1308", "FS1360", "FS1373",
			"FS2744", "FS4621", "FS4625", "FS4630", "FS4640", "FS4681", "FS4830"},
			colname
		) | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450"),
		Insert Into( sum_lst2, col_lst2[i] ), ///CAT1//	
	
		If( Contains( {"SS098", "SS090"}, colname ) | (colname >= "FS9700" & colname <= "FS9791") | (colname >= "FS9793" & colname <= "FS9799"),
			Insert Into( sum_lst4, col_lst2[i] ), //CAT2//
			
			
			
			
			),
			Insert Into( sum_lst3, col_lst2[i] )///CAT3
		)
	);
Eval( Eval Expr( cdt << New Column( "CAT1", formula( Sum( Expr( sum_lst2 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT3", formula( Sum( Expr( sum_lst3 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT2", formula( Sum( Expr( sum_lst4 ) ) ) ) ) );
txnelson
Super User

Re: Help needed with nested if and else statement

Here is my reworking of your code, and it properly handles all 51 columns.  It inserts 12 columns into sum_lst2, 39 into sum_lst3 and zero into sum_lst4

Names Default To Here( 1 );
//cdt = open("temp\Rawdata.csv");
cdt = Current Data Table();
sum_lst2 = {};
sum_lst3 = {};
sum_lst4 = {};
col_lst2 = cdt << get column names( Numeric);
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If(
		Contains(
			{"SS008", "SS009", "SS010", "SS015", "SS053", "FS9429", "FS9792", "FS9906", "FS9908", "FS1302", "FS1305",
			"FS1308", "FS1360", "FS1373", "FS2744", "FS4621", "FS4625", "FS4630", "FS4640", "FS4681", "FS4830"},
			colname
		) | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450")
		,
			Insert Into( sum_lst2, col_lst2[i] )///CAT1//
	, // Else	
	
		Contains( {"SS098", "SS090"}, colname ) | (colname >= "FS9700" & colname <= "FS9791") | (colname >= "FS9793" &
		colname <= "FS9799")
		, 
			Insert Into( sum_lst4, col_lst2[i] ) //CAT2//		
	, // Else
			Insert Into( sum_lst3, col_lst2[i] )///CAT3
	);
); 

Eval( Eval Expr( cdt << New Column( "CAT1", formula( Sum( Expr( sum_lst2 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT3", formula( Sum( Expr( sum_lst3 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT2", formula( Sum( Expr( sum_lst4 ) ) ) ) ) );

Jim
Joseph_Reese
Level IV

Re: Help needed with nested if and else statement

I think you are very close! You closed off that second If() statement a bit too soon. I get all zeros for CAT2 but CAT3 has values now. If you are expecting values for CAT2 at this point then it may need a bit more work.

 

cdt = Current Data Table();
sum_lst2 = {};
sum_lst3 = {};
sum_lst4 = {};
col_lst2 = cdt << get column names( Numeric );
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If(
		Contains(
			{"SS008", "SS009", "SS010", "SS015", "SS053", "FS9429", "FS9792", "FS9906", "FS9908", "FS1302", "FS1305", "FS1308", "FS1360", "FS1373",
			"FS2744", "FS4621", "FS4625", "FS4630", "FS4640", "FS4681", "FS4830"},
			colname
		) | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450"),
		Insert Into( sum_lst2, col_lst2[i] ), ///CAT1//	
	
		If( Contains( {"SS098", "SS090"}, colname ) | (colname >= "FS9700" & colname <= "FS9791") | (colname >= "FS9793" & colname <= "FS9799"),
			Insert Into( sum_lst4, col_lst2[i] ), //CAT2//
			Insert Into( sum_lst3, col_lst2[i] )///CAT3
		)
	)
);
Eval( Eval Expr( cdt << New Column( "CAT1", formula( Sum( Expr( sum_lst2 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT3", formula( Sum( Expr( sum_lst3 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT2", formula( Sum( Expr( sum_lst4 ) ) ) ) ) );
ENTHU
Level IV

Re: Help needed with nested if and else statement

I figured what the issue is - For Cat3 I'm getting a huge value(in range e15 after fixing column format).The reason is I have a few more columns in the table.

For Cat3 along with leftover FS*,my script is adding up values from other columns.

In the else statement how do I consider only the columns with column names FS*? 

 

ItemLocationOperTOTAL_TESTEDTOTAL_GOODTOTAL_BADTest_datePkgdDrsPrgnmWeekFS0100FS0847FS0875FS0876FS0877FS1059FS1061FS1090FS1315FS1501FS1503FS1505FS1537FS1539FS1541FS1543FS1559FS1590FS1930FS1933FS2706FS2740FS2742FS2745FS3416FS3417FS3418FS3422FS3423FS3455FS3469FS3471FS3492FS3493FS5391FS9429FS9792FS9901FS9906
           1321                3                   1
           6002                72 14   5  14    1112
           562    1            9  11  111211112   21
           5272 21 21 7 24332 241823  12 1 1 4 1113  2
           322 1     1 2     1 7   3          2    3
Joseph_Reese
Level IV

Re: Help needed with nested if and else statement

For that last Else statement that you have (snippet shown below):

 

Insert Into( sum_lst3, col_lst2[i] )///CAT3

 How about just adding one last nested If() statement like below? Then don't have an Else statement

 

If( 
	Contains( colname, "FS" ),
	Insert Into( sum_lst3, col_lst2[i] )
)///CAT3, no ELSE statement
ENTHU
Level IV

Re: Help needed with nested if and else statement

This wont help because I need to exclude FS columns that are part of Cat 1 and Cat2.

Cat 1 -  

		{"SS008", "SS009", "SS010", "SS015", "SS053", "FS9429", "FS9792", "FS9906", "FS9908", "FS1302", "FS1305", "FS1308", "FS1360", "FS1373",
			"FS2744", "FS4621", "FS4625", "FS4630", "FS4640", "FS4681", "FS4830"},
			colname
		) | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450"),

Cat 2 - 

Contains( {"SS098", "SS090"}, colname ) | (colname >= "FS9700" & colname <= "FS9791") | (colname >= "FS9793" & colname <= "FS9799"),
			Insert Into( sum_lst4, col_lst2[i] )

 

Cat3 - all FS* cols that are of part of Cat1 and Cat2

Joseph_Reese
Level IV

Re: Help needed with nested if and else statement

If the script makes it to that last Else statement where it checks for FS in the column name, by definition that column has not been included in Cat1 or Cat2. So a column will not be put into both Cat1 and Cat3. If a Cat1 column gets identified, your code inserts it into the right list and then moves to the next iteration of the For() loop, it does not keep running down the chain because it doesn't need to run the Else portion.

 

I would give it a try and see if it works. Apologies I am not completely clear what you are asking but hopefully this does it!

ENTHU
Level IV

Re: Help needed with nested if and else statement

Thanks for all the suggestions.I could finally get it working by adding two else statements and adding contains condition in the last else statement

For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If(
		Contains({"FS9429", "FS9792", "FS9906","FS1156","FS1157"},colname) | (colname >= "FS800" & colname <= "FS899") |(colname >= "FS901" & colname <= "FS916") |
		(colname >= "FS954" & colname <= "FS965") |(colname >= "FS1059" & colname <= "FS1063") |(colname >= "FS1348" & colname <= "FS1363") |(colname >= "FS1374" & colname <= "FS1379") |
		 (colname >= "FS1500" & colname <= "FS1599") | (colname >= "FS2581" & colname <= "FS2586") | (colname >= "FS2900" & colname <= "FS2950") | (colname >= "FS3400" & colname <= "FS3450") |(colname >= "FS5300" & colname <= "FS5399") |
		 (colname >= "FS6700" & colname <= "FS6799"),
		Insert Into( sum_lst2, col_lst2[i] )///CAT1//
	
	, // Else	
	
		Contains( {"FS9700"}, colname ) | (colname >= "FS9701" & colname <= "FS9791") | (colname >= "FS9793" & colname <= "FS9799")
		, 
			Insert Into( sum_lst4, col_lst2[i] ) //CAT2//		
	, // Else
		Contains( colname, "FS" ) & (colname > "FS0100"),
			Insert Into( sum_lst3, col_lst2[i] )///CAT3
	);
); 

Eval( Eval Expr( cdt << New Column( "CAT1", formula( Sum( Expr( sum_lst2 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT3", formula( Sum( Expr( sum_lst3 ) ) ) ) ) );
Eval( Eval Expr( cdt << New Column( "CAT2", formula( Sum( Expr( sum_lst4 ) ) ) ) ) );