libname Prob 'Y:\alsdkjf\alksjdfl';
这里的大地水准面是char我想转换为num以便能够通过id合并;
data Problem2_1;
set Prob.geocode;
id = substr(GEOID, 8, 2);
id = input(id, best5.);
output;
run;
geoid here is numeric;
data Problem2_2; c
set Prob.households;
id = GEOID;
output;
run;
data Problem2_3;
merge Problem2_1
Problem2_2 ;
by ID;
run;
proc print data = Problem2_3;
*错误:变量大地水准面被定义为字符和数字。 *错误:变量ID已被定义为字符和数字。
答案 0 :(得分:0)
看起来你可以替换这两行:
id = substr(GEOID, 8, 2);
id = input(id, best5.);
使用:
id = input(substr(GEOID, 8, 2), best.);
这意味着两个合并数据集都包含数字ID变量。
答案 1 :(得分:0)
SAS要求链接ID为相同的数据类型。这意味着您必须将int转换为字符串,反之亦然。就个人而言,我希望尽可能转换为数字。
A是一个成功的例子:
/*Create some dummy data for testing purposes:*/
data int_id;
length id 3 dummy $3;
input id dummy;
cards;
1 a
2 b
3 c
4 d
;
run;
data str_id;
length id $1 dummy2 $3;
input id dummy2;
cards;
1 aa
2 bb
3 cc
4 dd
;
run;
/*Convert string to numeric. Int in this case.*/
data str_id_to_int;
set str_id;
id2 =id+0; /* or you could use something like input(id, 8.)*/
/*The variable must be new. id=id+0 does _not_ work.*/
drop id; /*move id2->id */
rename id2=id;
run;
/*Same, but other way around. Imho, trickier.*/
data int_id_to_str;
set int_id;
id2=put(id, 1.); /*note that '1.' refers to lenght of 1 */
/*There are other ways to convert int to string as well.*/
drop id;
rename id2=id;
run;
/*Testing. Results should be equivalent */
data merged_by_str;
merge str_id(in=a) int_id_to_str(in=b);
by id;
if a and b;
run;
data merged_by_int;
merge int_id(in=a) str_id_to_int(in=b);
by id;
if a and b;
run;
答案 2 :(得分:-1)
对于Problem2_1
,如果您的子字符串仅包含数字,则可以通过添加零将其强制转换为数字。这样的东西应该使ID为数字,然后你可以与Problem2_2
合并。
data Problem2_1;
set Prob.geocode;
temp = substr(GEOID, 8, 2);
id = temp + 0;
drop temp;
run;
编辑: 您的原始代码最初将ID定义为substr的输出,即字符。这也应该有效:
data Problem2_1;
set Prob.geocode;
temp = substr(GEOID, 8, 2);
id = input(temp, 8.0);
drop temp;
run;