Hi all,
How can I write an If Else statement when an existing column is blank, then it will create another column. If it is existing and not blank, then there is no need to create an additional column.
Here's a snip of the code:
dtcol = dt << get column names( "String" );
If( Contains( dtcol, "Name" ),
/* but column is blank, create another column Name2 */
);
Here is a small script, that looks to see if there is a column named "name" and if not it creates a column named "name 2"
Names Default To Here( 1 );
dt = Current Data Table();
dtcol = dt << get column names( "String" );
If( Contains( dtcol, "Name" ),
dt << New Column( "name 2" )
);
I don't know is this is what you want.
I accidentally pushed the accepted solution. But my question is how to determine if the existing column is blank. If it is blank, then it will be the time to create another column (name 2).
Is your question that you want to check to see if all of the cells in a given column are blank?
Names Default To Here( 1 );
dt = Current Data Table();
// Character Columns
theString = Concat Items( Column( dt, myColumn ) << get values );
If( theString == "",
dt << New Column( "myColumn 2", character )
);
// Numeric Columns
theSum = Col Sum( dt:myColumn );
If( Is Missing( theSum ),
dt << New Column( "my Column 2" )
);
I tried the code for the character column but prompts this error:
String( " ... 2110 total characters ... " ) assigned.
Not working for me tho
If you have JMP16 this should work. Run it in parts to understand what it is doing
Names Default To Here(1);
dt = New Table("Untitled",
Add Rows(2),
Compress File When Saved(1),
New Column("NumVal", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2])),
New Column("NumMissing", Numeric, "Continuous", Format("Best", 12), Set Values([., .])),
New Column("CharVar", Character, "Nominal", Set Values({"a", "b"})),
New Column("CharMissing", Character, "Nominal", Set Values({"", ""}))
);
n_rows = N Rows(dt);
For Each({col_name}, dt << Get Column Names("String"),
If(Col N Missing(Column(dt, col_name)) == n_rows,
dt << New Column(col_name);
);
);