列标题搜索和替换你好,
我可以使用确切的名称替换列标题名称:高度<<设置名称(“测试”);
如果我有一个文本列表,我想将其替换为“”。
我该如何处理?
“姓名”“时间”“性别”“身高abc体重”“体重xyz”“12身高测试”
凯蒂 12 F 59 95 59
路易丝 12 F 61 123 61
最终输出:
“姓名”“时间”“性别”“abc”“xyz”“12测试”
Names Default To Here( 1 );
dt = Open( "C:/JSL/BigClass.jmp" );
headerList = dt << get column names( string );
//:height << Set Name( "test" );
For( i = 1, i <= N Items( headerList ), i++,
If( (headerList[i] == "height" || headerList[i] == "weight"), //trying to find any column header has height or weight and replace with ""
Column( dt, colList[i] ) << set name( " " ) //above are just two examples, maybe put it in list and loop?
)
);
脚本回复:列标题搜索和替换你好山姆,
问题似乎出在您仍在进行替换的第二个循环 (j) 中直接重命名列。
一种可能的解决方案(如下所示)是修改标题列表 (hl),然后在完成所有更改后重命名该列:
Names Default To Here( 1 );
dt = Open( "C:\Program Files\SAS\JMP\14\Samples\Data\Big Class.jmp" );
hl = dt << get column names( string );
sr = List();
sr[1] = "height"; //
sr[2] = "e"; // Order of replacement strings is critical
sr[3] = "a"; //
For( i = 1, i <= N Items( hl ), i++,
For (j = 1, j <= N Items (sr), j++,
If (contains (hl [i], sr [j]),
txt = substitute (hl [i], sr [j], "x"); // Just for clarity: the expression could be wrapped into the next
Substitute into (hl, hl [i], txt); // Hold all the changes into the hl list
)
);
Column (dt , i ) << set name (hl [i]); // Renames column only after all changes have been made
);
我希望它有所帮助。
最好,
TS
回复:列标题搜索和替换我试图提前定义列表并进行双嵌套循环。 它只能替代字符串替换 [3]。
headerList = dt << get column names( string );
stringReplace = List();
stringReplace[1] = "height";
stringReplace[2] = "e";
stringReplace[3] = "a";
For( i = 1, i <= N Items( headerList ), i++,
For( j = 1, j <= N Items( stringReplace ), j++,
Column( dt, i ) << set name( Substitute(headerList[i], stringReplace[j], "" ) )
);
);
回复:列标题搜索和替换你好山姆,
我在你的脚本中发现了 2 个问题:
- 您使用了连接“||”代表您的“或”语句的代码(应为“|”)
- JMP 不允许将列命名为“”。
此外,通常更快更容易地按编号调用列而不是遍历列名列表
所以,这就是我想出的:
Names Default To Here( 1 );
dt = Open( "C:\Program Files\SAS\JMP\14\Samples\Data\Big Class.jmp" );
headerList = dt << get column names( string );
For( i = 1, i <= N Items( headerList ), i++,
If( (headerList[i] == "height" | headerList[i] == "weight"), //Fixed the "Or" statement
Column( dt, i ) << set name(substitute (headerList [i], "e", "x")) //Used the substitute function as an example, you can replace any string with ""?
)
);