Subscribe Bookmark RSS Feed

JSL multiple column comparison to a fix number using for loop, and generate a new column for output

Highlighted
Stokes

New Contributor

Joined:

Sep 22, 2017

Hi JSL Experts, I am a starter of JSL, I want to perform multiple column comparision, the column name is, Col1, Col2, Col3 etc. I am using a For loop for this comparison (see below for whole scripts), and if it falls into the formula (See below), it  will output 0 or 1. 1. What I know is, If I put :Col1 into the script :Col1, the script works fine as below. 2. What I don’t know is, If I replace :Col1 into Column( dt, i ), the script doesn’t work. I also tried use, :Name, :"Name", but none of it works to replace :Col1. Anyone know how to do the multiple column comparision? Example of the changed parts: This works fine: New Column(Name, Character, "Nominal", Formula(If( 1 > :Col1 >0, 0,1)) This doesn’t works: New Column(Name, Character, "Nominal", Formula(If( 1 > :Column( dt, i ) >0, 0,1)) Example of the whole scripts: dt1 = table1; dt = table2; For( i = 1, i < N Col( dt ) + 1, i++, Name = Column( dt, i ) << get name; If( Contains( Name, "ABCD" ), For( j = 1, j <= N Row( dt1 ), j++, GGG = Column( dt1, "GGGer" )[j]; New Column( Name, Character, "Nominal", Formula( If( 1 > :Col1 > 0, "0", "Fail" ) ) ); ); ) ); );
1 REPLY
uday_guntupalli

Community Trekker

Joined:

Sep 15, 2014

Hello @Stokes , 
         Welcome to the JSL Community. The community should be able to help you address your problem. However, for your future posts , may I suggest a small thing, when you post code , kindly insert using one of the following options shown below. This will help format the code and make it easier for community users to understand your code and help answer your questions.

image.png

Now that we have gotten that out of the way , lets see how to address your problem: 
If I understand your question correctly - 

1. How to compare columns in a data table against any parameter ? 
2. Why does a column reference ( Col = Column(dt,i); ) not directly replace a column name (:Col1) in a function call ? 

 

I will try to address these questions as best as I can - so please let me know if I dont address anything in my response .

 

Clear Globals(); Clear Log(); 

// Open Sample Data 
dt1 = Open( "$SAMPLE_DATA/Big Class.jmp" ); 

// Acquire Column Names 
ColNames = dt1 << Get Column Names("String"); 

// Define a searchword and look for it in column names 
DesiredColsPos1 = list();  
DesiredColsPos2 = List(); 
KeyWord1 = "age"; 

for(i = 1 , i <= N Cols(dt1), i++,
		If(Contains(ColNames[i],KeyWord1),
			Insert Into(DesiredColsPos1,i); 
		  ); 
   );
   
// Display Result
Show(DesiredColsPos1);

// Alternatively 
for(i = 1 , i <= N Cols(dt1), i++,
		Col = Column(dt1,i); // This gets a reference for the column 
		Show(Col);
		ColName = Col << Get Name; 
		If(Contains(ColName,KeyWord1),
			Insert Into(DesiredColsPos2,i);
		  ); 
   );
   
// Display Result
Show(DesiredColsPos2);



 

Best
Uday