DATA Step, Macro, Functions and more

Merge

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Merge

Hi Team,

I have two tables like this.

i want to merge by the number.

but in table2 i have the same number but preceding with one or several zeros.

How can i remove them before I merge???

Regards


table1               table2

number              number

6234                 006234

76547               076547

70061               070061

5849                 005849


Accepted Solutions
Solution
‎12-11-2012 05:24 PM
Super Contributor
Posts: 1,636

Re: Merge

Hi,

try changing "number" in table1 before merging:

data table1;

input number $;

cards;

123

2345

3489

;

data temp(rename=(new=number));

length new $6.;

  set table1;

  new=put(number*1,z6.);

  drop number;

  proc print;run;

View solution in original post


All Replies
PROC Star
Posts: 7,364

Re: Merge

Are they just formatted that way (in which case you don't have to do anything but compare them) or is the field with the leading zeros a text field?

Super Contributor
Posts: 1,040

Re: Merge

Hi,

Thanks for the reply. Its just a charecter. No format given to it .its in the $10. format.

Regards

Solution
‎12-11-2012 05:24 PM
Super Contributor
Posts: 1,636

Re: Merge

Hi,

try changing "number" in table1 before merging:

data table1;

input number $;

cards;

123

2345

3489

;

data temp(rename=(new=number));

length new $6.;

  set table1;

  new=put(number*1,z6.);

  drop number;

  proc print;run;

Super Contributor
Posts: 1,040

Re: Merge

Hi,

Thanks. This works too....

So you want me to max length of the number variable from the other table and use Z6 format!!!

Cool.

so thwe Z6 adds leading zeros???whats is the 1* for???

If my length is 9 should I write Z9.?????

Regards

Super Contributor
Posts: 1,636

Re: Merge

"*1" converts a character variable to a numeric variable. If "number" in table1  is a numeric variable then you don't need "*1".

z6. adds leading zeros.

If my length is 9 should I write Z9.?????


i think so, you can try.

Super Contributor
Posts: 1,040

Re: Merge

In the mean while my friend suggested to use this on the second table to eliminate leading zeros.it works too

mrn2=substr(number,verify(number,'0'));

Regards

PROC Star
Posts: 7,364

Re: Merge

I would do the merge using proc sql and just convert the text on the fly.  e.g.:

data table1;

  input number x;

  cards;

6234 1

76547 1

70061 1

5849 1

;

data table2;

  informat number $10.;

  input number y;

  cards;

006234  2

076547  2

070061  2

005849  2

;

proc sql;

  create table want as

    select a.number,x,y

      from table1 a

        left join table2 b

          on a.number=input(b.number,12.)

  ;

quit;

Super Contributor
Posts: 1,040

Re: Merge

Could you also explain to me what this step is doing??

new=put(number*1,z6.);

if my length is 9 then have I to use Z9.???

Regards

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 282 views
  • 3 likes
  • 3 in conversation