cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-6268%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EDetermining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6268%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CP%3EI%20have%20a%20JMP%20script%20that%20creates%20a%20new%20column%20with%20a%20formula%20in%20it.%20I'm%20trying%20to%20find%20out%20how%20to%20detect%20if%20the%20column%20already%20exists%20so%20that%20I%20can%20skip%20creating%20it%2C%20and%20avoid%20generating%20extra%20columns.%20I'm%20sure%20it's%20there%2C%20but%20I'm%20not%20finding%20it%20in%20the%20literature%20that%20I%20have.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3EMike%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55586%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55586%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHere's%20a%20version%20of%20my%20logic%20that's%20case-insensitive.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20open(%22%24sample_data%5CBig%20Class.jmp%22)%3B%0Acol_name_list%20%3D%20dt%20%26lt%3B%26lt%3B%20get%20column%20names(string)%3B%0A%2F%2F%20Convert%20all%20column%20names%20to%20uppercase%20for%20case-insensitive%20search%0Afor%20(i%20%3D%201%2C%20i%20%26lt%3B%3D%20nitems(col_name_list)%2C%20i%2B%2B%2C%0A%20col_name_list%5Bi%5D%20%3D%20uppercase(col_name_list%5Bi%5D)%3B%0A)%3B%0Anew_column%20%3D%20%22BMI%22%3B%0A%2F%2F%20English%20BMI%20Formula%20BMI%20%3D%20(%20Weight%20in%20Pounds%20%2F%20(%20Height%20in%20inches%5E2%20)%20)%20x%20703%0Aif%20(!contains(col_name_list%2C%20uppercase(new_column))%2C%0A%20%20%20%20dt%20%26lt%3B%3CNEW%20column%3D%22%22%3E%3C%2FNEW%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55531%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55531%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EThanks%20ms!%3CBR%20%2F%3EOnly%20your%20example%20worked%20for%20me%2C%20because%20the%20other%20solution%20is%20case%20sensitive.%20I%20had%20to%20use%20yours%2C%20since%20I%20could%20not%20be%20sure%20that%20the%20case%20of%20my%20string%20characters%20are%20correct.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6276%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6276%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EPMroz%2C%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3ELet%20me%20start%20by%20confirming%20that%20the%20code%20you%20wrote%20worked%20for%20me%20as%20well%2C%20and%20the%20explaination%20I%20wrote%20is%20not%20consistent%20with%20JMP%209%20or%2010%20behavior.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EAfter%20reading%20your%20reply%2C%20I%20also%20tried%20several%20different%20combinations%20of%20contains%2C%20and%20if%20statements%20with%20lists%2C%20string%2C%20and%20numbers%20in%20JMP%209%20and%2010.%20Although%20my%20attempts%20were%20not%20exhaustive%2C%20all%20of%20my%20results%20are%20consistent%20with%20yours.%20Initially%2C%20when%20I%20read%20your%20first%20post%20I%20recalled%20some%20troubleshooting%20that%20I%20had%20perform%20years%20prior%20where%20I%20had%20adopted%20the%20the%20use%20of%20contains()%26gt%3B0.%20I%20am%20unable%20to%20recreate%20the%20scenario%3B%20however%2C%20I%20did%20find%20the%20following%20information%20in%20the%20JMP%20scripting%20guide.%20It%20details%20using%20contains%20with%20%26gt%3B0%2C%20which%20may%20be%20appropriate%20for%20their%20example%2C%20but%20does%20not%20explicitly%20state%20that%20it%20is%20or%20is%20not%20required.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EPage%20136%20of%20the%20JMP%20scripting%20guide%20version%2010%3A%3C%2FP%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3E%22To%20assess%20whether%20an%20item%20is%20in%20a%20list%2C%20use%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20LucidaSansTypewriter%3B%20font-size%3A%209pt%3B%22%3ELoc()%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3Eand%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20LucidaSansTypewriter%3B%20font-size%3A%209pt%3B%22%3EContains()%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3Ewith%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20LucidaSansTypewriter%3B%20font-size%3A%209pt%3B%22%3E%26gt%3B0%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3E.%20A%20returned%20value%20of%20zero%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3Emeans%20that%20the%20item%20is%20not%20in%20the%20list.%20A%20returned%20value%20of%201%20means%20that%20the%20item%20is%20in%20the%20list%20at%20least%20once.%22%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3E...%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3E%22Find%20out%20if%20the%20number%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20LucidaSansTypewriter%3B%20font-size%3A%209pt%3B%22%3E5%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20AGaramond-Regular%3B%20font-size%3A%2010pt%3B%22%3Eexists%20in%20the%20%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20LucidaSansTypewriter%3B%20font-size%3A%209pt%3B%22%3EnumList%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20LucidaSansTypewriter%3B%20font-size%3A%209pt%3B%22%3ENRow(Loc(numList%2C%205))%20%26gt%3B0%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CEM%20style%3D%22font-size%3A%209pt%3B%20font-family%3A%20LucidaSansTypewriter%2CItalic%3B%22%3E0%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20LucidaSansTypewriter%3B%20font-size%3A%209pt%3B%22%3EContains(numList%2C%205)%20%26gt%3B0%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CEM%20style%3D%22font-size%3A%209pt%3B%20font-family%3A%20LucidaSansTypewriter%2CItalic%3B%22%3E0%22%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EHope%20this%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6275%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6275%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHello%20wiebepo%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20tried%20different%20combinations%20and%20negating%20a%20positive%20integer%2C%20no%20matter%20how%20large%2C%20results%20in%20a%200.%26nbsp%3B%20So%20my%20code%20will%20work%20(in%20JMP%209%20and%2010).%26nbsp%3B%20Having%20said%20that%20I%20appreciate%20the%20heads%20up%20-%20this%20approach%20might%20not%20work%20in%20all%20languages.%26nbsp%3B%20It%20certainly%20bears%20further%20testing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Eb%20%3D%202000000000000%3B%0A!b%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20log%20shows%3A%3C%2FP%3E%0A%3CP%3E%3CSTRONG%20style%3D%22color%3A%20black%3B%20font-size%3A%2010.0pt%3B%20font-family%3A%20'Courier%20New'%3B%22%3E0%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERegards%2C%3C%2FP%3E%0A%3CP%3EPMroz%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6274%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6274%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EAlso%20handy%20to%20know.%20I'm%20always%20glad%20to%20have%20another%20tool%20to%20add%20to%20the%20toolbox.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6273%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6273%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThat's%20very%20handy%20to%20know.%20I'll%20have%20to%20watch%20out%20for%20that.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6272%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6272%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThere%20is%20no%20single%20function%20that%20I%20am%20aware%20of.%20But%20%3CEM%3EIs%20missing()%3C%2FEM%3E%20in%20combination%20with%20%3CEM%3EIs%20Scriptable()%3C%2FEM%3E%20can%20do%20the%20trick.%3C%2FP%3E%0A%3CP%3EThe%20column%20also%20need%20to%20be%20enclosed%20by%20a%20Try()%20statement%20to%20avoid%20the%20script%20from%20stopping%20if%20the%20column%20would%20not%20exist.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20Open(%20%22%24sample_data%5CBig%20Class.jmp%22%20)%3B%0A%20%0Anew_column%20%3D%20%22BMI%22%3B%0A%20%0AIf(%20Is%20Missing(%20Is%20Scriptable(%20Try(%20Column(%20new_column%20)%20)%20)%20)%2C%0A%20dt%20%26lt%3B%26lt%3B%20New%20Column(%20new_column%2C%20numeric%2C%20continuous%2C%20formula(%20703%20*%20%3Aweight%20%2F%20%3Aheight%20%2F%20%3Aheight%20)%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%20style%3D%22margin%3A%200.0px%200.0px%200.0px%200.0px%3B%20color%3A%20%23a70096%3B%22%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6271%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6271%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%20style%3D%22font-family%3A%20'Helvetica%20Neue'%2C%20Helvetica%2C%20Arial%2C%20'Lucida%20Grande'%2C%20sans-serif%3B%20background-color%3A%20%23ffffff%3B%22%3E%3CSPAN%20style%3D%22font-style%3A%20inherit%3B%20font-size%3A%2010pt%3B%20font-family%3A%20'Courier%20New'%3B%20color%3A%20black%3B%22%3EI%20am%20not%20certain%20if%20this%20is%20true%20in%20all%20recent%20versions%20of%20JMP.%20I%20make%20this%20comment%20only%20to%20highlight%20an%20opportunity%20to%20improve%20the%20robustness%20of%20the%20code.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22font-family%3A%20'Helvetica%20Neue'%2C%20Helvetica%2C%20Arial%2C%20'Lucida%20Grande'%2C%20sans-serif%3B%20background-color%3A%20%23ffffff%3B%22%3E%3CSPAN%20style%3D%22font-style%3A%20inherit%3B%20font-size%3A%2010pt%3B%20font-family%3A%20'Courier%20New'%3B%20color%3A%20black%3B%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22font-family%3A%20'Helvetica%20Neue'%2C%20Helvetica%2C%20Arial%2C%20'Lucida%20Grande'%2C%20sans-serif%3B%20background-color%3A%20%23ffffff%3B%22%3E%3CSPAN%20style%3D%22font-style%3A%20inherit%3B%20font-size%3A%2010pt%3B%20font-family%3A%20'Courier%20New'%3B%20color%3A%20black%3B%22%3E'Contains'%20returns%20the%20position%20of%20the%20item%2C%20which%20is%20fine%20to%20use%20in%20an%20'if'%20statement%2C%20since%20zero%20will%20be%20interpreted%20as%20false%20and%20a%20positive%20value%20will%20be%20interpreted%20true.%20When%20preceding%20a%20contains%20with%20a%20not%20'!'%2C%20the%20condition%20works%20when%20the%20item%20is%20not%20present%20or%20when%20present%20as%20the%20first%20item%3B%20however%2C%20when%20the%20item%20isn't%20first%20(%20a%20value%20greater%20than%201%20)%2C%20the%20not%20'!'%20can%20fail%20to%20convert%20the%20condition%20to%20false.%20This%20can%20be%20avoided%20by%20using%20a%20comparison%20'%26gt%3B0'%20with%20the%20'contains'.%20See%20below%20for%20syntax.%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20Open(%20%22%24sample_data%5CBig%20Class.jmp%22%20)%3B%0A%20%0Acol_name_list%20%3D%20dt%20%26lt%3B%26lt%3B%20get%20column%20names(%20string%20)%3B%0A%20%0Anew_column%20%3D%20%22BMI%22%3B%0A%20%0A%2F%2F%20English%20BMI%20Formula%0A%2F%2F%20BMI%20%3D%20(%20Weight%20in%20Pounds%20%2F%20(%20Height%20in%20inches%20x%20Height%20in%20inches%20)%20)%20x%20703%0A%20%0AIf(%20!(Contains(%20col_name_list%2C%20new_column%20)%20%26gt%3B%200)%2C%0A%20dt%20%26lt%3B%26lt%3B%20New%20Column(%20%22BMI%22%2C%20numeric%2C%20continuous%2C%20formula(%20703%20*%20%3Aweight%20%2F%20%3Aheight%20%2F%20%3Aheight%20)%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%20style%3D%22font-family%3A%20'Helvetica%20Neue'%2C%20Helvetica%2C%20Arial%2C%20'Lucida%20Grande'%2C%20sans-serif%3B%20background-color%3A%20%23ffffff%3B%22%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6270%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6270%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThanks%2C%20that%20works%20just%20fine%20for%20me.%20I%20was%20expecting%20to%20find%20a%20function%20to%20check%20for%20existance%2C%20but%20this%20works.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-6269%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Determining%20if%20a%20column%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-6269%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%20style%3D%22margin-bottom%3A%200.0001pt%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20open(%22%24sample_data%5CBig%20Class.jmp%22)%3B%0A%20%0Acol_name_list%20%3D%20dt%20%26lt%3B%26lt%3B%20get%20column%20names(string)%3B%0A%20%0Anew_column%20%3D%20%22BMI%22%3B%0A%20%0A%2F%2F%20English%20BMI%20Formula%0A%2F%2F%20BMI%20%3D%20(%20Weight%20in%20Pounds%20%2F%20(%20Height%20in%20inches%20x%20Height%20in%20inches%20)%20)%20x%20703%0A%20%0Aif%20(!contains(col_name_list%2C%20new_column)%2C%0A%20%20%20%20%20%20dt%20%26lt%3B%3CNEW%20column%3D%22%22%3E%3C%2FNEW%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
mwechtal
Level III

Determining if a column exists

I have a JMP script that creates a new column with a formula in it. I'm trying to find out how to detect if the column already exists so that I can skip creating it, and avoid generating extra columns. I'm sure it's there, but I'm not finding it in the literature that I have.

Thanks.

Mike

10 REPLIES 10
wiebepo
Level III

Re: Determining if a column exists

PMroz,

Let me start by confirming that the code you wrote worked for me as well, and the explaination I wrote is not consistent with JMP 9 or 10 behavior.

After reading your reply, I also tried several different combinations of contains, and if statements with lists, string, and numbers in JMP 9 and 10. Although my attempts were not exhaustive, all of my results are consistent with yours. Initially, when I read your first post I recalled some troubleshooting that I had perform years prior where I had adopted the the use of contains()>0. I am unable to recreate the scenario; however, I did find the following information in the JMP scripting guide. It details using contains with >0, which may be appropriate for their example, but does not explicitly state that it is or is not required.

Page 136 of the JMP scripting guide version 10:

"To assess whether an item is in a list, use Loc() and Contains() with >0. A returned value of zero

means that the item is not in the list. A returned value of 1 means that the item is in the list at least once."

...

"Find out if the number 5 exists in the numList:

NRow(Loc(numList, 5)) >0;

0

Contains(numList, 5) >0;

0"

Hope this helps.