BookmarkSubscribeRSS Feed
Walternate
Obsidian | Level 7

Hi,

I am trying to join two datasets on an ID variable present in both datasets. However, in one dataset, the ID is formatted to always be at least 3 digits by adding leading 0s if the ID is only a 1- or 2-digit number (so like 001, 050, 067, etc). In the other dataset, the ID variable does not have the leading 0s, and the same values that I used as examples before would be 1, 50, 67, etc. I'm not sure how to match on these values considering the mismatch. Trimming the 0s would be difficult because some IDs have two leading 0s, some one leading 0, and others zero leading 0s. Within each dataset, the IDs have different lengths (either 3 or 4).

Any help is much appreciated.

Thanks!

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Personally I would convert the two to numeric for comparison:

proc sql;

     create table WANT as

     select     A.*,

                   B.VAR

     from       HAVE A

     left join   ANOTHER_HAVE B

     on          input(A.ID,best.)=input(B.ID,best.);

quit;

Note that it assumes both are character.  If they are number, or one or the other is number, no need to input it.

Ksharp
Super User

Make SQL's join be more powerful by prxmatch . I am going to write a paper about it .Waiting ...................

data a;

input k $;

cards;

001

050

067

;

run;

data b;

input k $ flag;

cards;

1 1

50 2

;

run;

proc sql;

create table want as

select a.k,b.flag

  from a left join b on prxmatch(cats('/^0*',b.k,'/'),a.k);

quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 675 views
  • 0 likes
  • 3 in conversation