2017-03-20 78 views
-2
libname Prob 'Y:\alsdkjf\alksjdfl'; 

這裏的大地水準儀是字符我想轉換爲數字,以便能夠通過ID合併;將SAS字符合併爲數字

data Problem2_1; 
set Prob.geocode;  
id = substr(GEOID, 8, 2); 
id = input(id, best5.); 
output; 
run; 

這裏的數字是數字;

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已被定義爲字符和數字。

回答

-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; 
+0

您好,感謝現在只有一個錯誤信息.... * ERROR:變量ID已經被定義爲字符和數字。 – LEARNDATAsCI

+0

ID仍然是'problem2_2'中的字符? – Craig

+0

是...這是我的家庭例如 – LEARNDATAsCI

0

看起來你可以取代這兩條線路:

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

有了:

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

這意味着,兩個合併數據集包含數字ID變量。

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;