DATA Step, Macro, Functions and more

ID variable with multiple lengths

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 138
Accepted Solution

ID variable with multiple lengths

Hi,

I am trying to join two datasets (using proc sql) on an ID variable. Though the values of the ID variables match to the other dataset, one of the ID variables is numeric and one is character. I tried converting the numeric one to character using ID_char=put(ID, 7.), and that worked, enabling me to join on some of the values of ID. The problem is that there are some other values of ID that are length 6, and those will not join to the other dataset, presumably because SAS added a trailing blank and that doesn't match to the version in the other set which has no blank (I'm guessing).

Any help is much appreciated.


Accepted Solutions
Solution
‎02-13-2015 09:48 AM
Trusted Advisor
Posts: 2,116

Re: ID variable with multiple lengths

Posted in reply to Walternate

It may be more likely that the original character variable had a leading blank.  You can tell by printing some of the offending IDs in Hex.

Another alternative would be to recode BOTH IDs to be left justified.

View solution in original post


All Replies
Solution
‎02-13-2015 09:48 AM
Trusted Advisor
Posts: 2,116

Re: ID variable with multiple lengths

Posted in reply to Walternate

It may be more likely that the original character variable had a leading blank.  You can tell by printing some of the offending IDs in Hex.

Another alternative would be to recode BOTH IDs to be left justified.

Super User
Super User
Posts: 7,997

Re: ID variable with multiple lengths

Posted in reply to Walternate

Hi,

where strip(A.ID)=strip(put(B.ID,7.));

Assuming A is the char and B the numeric.

Super User
Super User
Posts: 7,083

Re: ID variable with multiple lengths

Posted in reply to Walternate

I like CATS() as a function that doesn't care whether its inputs are character or numeric.

data A ;

  id=7;

  A=1;

run;

data B;

  id=' 7 ';

  B=1;

run;

proc sql ;

select * from A,B where cats(a.id)=cats(b.id) ;

quit;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 199 views
  • 6 likes
  • 4 in conversation