将SAS char合并到num

将SAS char合并到num,sas,Sas,这里的大地水准面是char,我想转换成num,以便能够通过id合并 libname Prob 'Y:\alsdkjf\alksjdfl'; 这里的大地水准面是数字的 data Problem2_1; set Prob.geocode; id = substr(GEOID, 8, 2); id = input(id, best5.); output; run; *错误:变量大地水准面已定义为字符和数字。 *错误:变量id已定义为字符和数字。对于问题2\u 1,如果子

这里的大地水准面是char,我想转换成num,以便能够通过id合并

libname Prob 'Y:\alsdkjf\alksjdfl';
这里的大地水准面是数字的

 data Problem2_1; 
 set Prob.geocode;     
 id = substr(GEOID, 8, 2);
 id = input(id, best5.);  
 output;
 run;
*错误:变量大地水准面已定义为字符和数字。
*错误:变量id已定义为字符和数字。

对于
问题2\u 1
,如果子字符串仅包含数字,则可以通过添加零将其强制为数字。类似这样的东西应该使ID为数字,然后您可以与
Problem2\u 2
合并

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定义为substr的输出,即字符。这也应该起作用:

data Problem2_1; 
set Prob.geocode;     
temp = substr(GEOID, 8, 2);
id = temp + 0;  
drop temp;
run;

看起来您可以替换这两行:

data Problem2_1; 
set Prob.geocode;     
temp = substr(GEOID, 8, 2);
id = input(temp, 8.0);  
drop temp;
run;
与:


这意味着两个合并数据集都包含数字id变量。

SAS要求链接id为相同的数据类型。这意味着您必须将int转换为string,反之亦然。就我个人而言,如果可能的话,我更喜欢转换成数字

A是一个已制定的示例:

id = input(substr(GEOID, 8, 2), best.);

你好谢谢,现在只有一条错误消息….*错误:变量id已定义为字符和数字。id在
问题2\u 2
中仍然是字符吗?变量类型描述示例GEOID NUM 1或2位地理代码34 TOTHOUSE NUM Total住户3064645未婚人数T未婚伴侣住户总数151318两个数据集中是否都没有名为
ID
的变量?一旦用一个类型定义了一个变量,您就不能覆盖它,afaik,因此我使用了temp变量。如果在两个原始数据集(prob.geocode和prob.houses)上运行
proc contents
,则每个数据集中的大地水准面类型是什么?
id = input(substr(GEOID, 8, 2), best.);
/*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;