Vin*_*ent 7 sas type-conversion
我想转换x为数字.
DATA test;
input x $1.;
cards;
1
2
0
;
run;
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方法:
用*1:
/* trial1 */
DATA test1;
SET test;
x = x*1;
run;
Run Code Online (Sandbox Code Playgroud)日志打印以下注释:
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
2470:3
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
2470:4
Run Code Online (Sandbox Code Playgroud)
格式不会改变.
用input():
/* trial2 */
DATA test2;
SET test;
x = input(x,BEST1.);
run;`
Run Code Online (Sandbox Code Playgroud)日志打印以下注释:
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
2396:3
Run Code Online (Sandbox Code Playgroud)
格式不会改变.
用informat:
/* trial3 */
DATA test3;
SET test;
informat x BEST1.;
run;
Run Code Online (Sandbox Code Playgroud)日志打印以下错误:
ERROR 48-59: The informat $BEST was not found or could not be loaded.
Run Code Online (Sandbox Code Playgroud)
这里和这里解释了:编译器检测变量和格式的不同类型,假设它是一个错误,添加假定缺失$,因此找不到格式.
如果我创建第二个变量,例如:所有这些试验都会有效:
DATA test4;
SET test (rename=(x=x2));
x = x2*1;
drop x2;
run;
Run Code Online (Sandbox Code Playgroud)
但我正在尝试清理我的代码,我想知道是否有办法在没有这样做的情况下进行这样的转换?
如其他地方所述,您确实需要使用第二个变量.SAS不允许您直接更改列的变量类型,但您可以通过使用重命名以与上述类似的方式来欺骗事物.
我要提出的与NEOmen的答案不同的一件事或者你的答案正在使用input.长度/分配或使用*1方法都很好,但它们依赖于SAS的自动类型转换,它会在日志中添加一个注释,它正在执行它.你应该在你的日志中避免这样的事情,因为它们很混乱并且让别人认为你可能在事故中这样做了.
使用NEOmen的测试数据集:
data test1;
set test(rename=x=x_old);
x=input(x_old,best12.); *whatever is appropriate informat for your variable;
run;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26019 次 |
| 最近记录: |