When I use PROC SQL statement in SAS, sometimes I need to convert variable from character to numeric or vice versa. I normally use the following two queries:
INPUT(A.KEY_ID, 8.) = B.KEY_ID
OR
A.KEY_ID = PUT(B.KEY_ID, 8.)
My question is, if the length of the variable is either 7 or 8, what length should I put after the KEY_ID? I tried A.KEY_ID = PUT(B.KEY_ID, 7.) and it lost all the records with KEY_ID length=8. And when I used A.KEY_ID = PUT(B.KEY_ID, 8.), it will not be able to find the corresponding KEY_ID with length=7.
Thank you very much!
Added:
Below is the query that I am currently using. LibnameA is a local library and tableA is a local SAS table. DatabaseB is a database that I am connected with. Key_id column is in both tables and used as the key to link them. For example, key_ids are 1234567, 12345678, and ect.
When I used the following query, which I put length 7 in the end of the put statement, all the records with 8 digits key_ids will not able to find a match.
PROC SQL;
CREATE TABLE LIBNAMEA.WORKTABLE AS
SELECT
A.*,
B.VAR1,
B.VAR2
FROM LIBNAMEA.TABLEA A
LEFT JOIN DATABASEb.TABLEB B
ON A.KEY_ID = PUT(B.KEY_ID,8.)
;
QUIT;
Update Results:
If I use the following query, it returns 192758 rows
ON A.KEY_ID = PUT(B.KEY_ID,7.)
If I use the following query, it returns 192923 rows
ON A.KEY_ID = PUT(B.KEY_ID,8.)
If I use the following query, it returns 192757 rows
ON INPUT(A.KEY_ID,8.) = B.KEY_ID
If I use the following query, it returns 192757 rows
ON A.KEY_ID = COMPRESS(PUT(B.KEY_ID,8.))
If I use the following query, it returns 192757 rows
ON COMPRESS(A.KEY_ID) = COMPRESS(PUT(B.KEY_ID,8.))
If I use the following query, it returns 192757 rows
ON INPUT(CATS(A.KEY_ID),8.) = INPUT(CATS(B.KEY_ID),8.)
If I use the following query, it returns 192757 rows
ON A.KEY_ID = PUT(B.KEY_ID,8.-L)
As you can see, only when I use ON A.KEY_ID = PUT(B.KEY_ID,8.), it would return 192923 rows, which is all rows from the table A. However, it will not find the corresponding key_ids in table B, and returns null value in the end results.