DATA Step, Macro, Functions and more

Numeric Concatination with Leading zeros

Reply
New Contributor
Posts: 2

Numeric Concatination with Leading zeros

Hi Everyone!

I was working on this data that has two numeric data fields with leading zeros. I wanted to concatinate without dropping the leading zeros to create a unique key. Can you guys help me please?

Field1=0001234

Field2=0005678

New Data Field will be: New_Field=00012340005678

 

I tried this, but keeps dropping the leading zeros from both data fields before it concatinates

DATA want;

SET have

New_Field=((LEFT(Field1)) || (LEFT(Field2));

RUN;

 

Thanks Guys!

Luke.

Respected Advisor
Posts: 4,173

Re: Numeric Concatination with Leading zeros

Posted in reply to maseffa82

I would also add a delimiter between the two columns when concatenating as done in below code for "key2"

data have;
  Field1=0001234;
  Field2=0005678;
  output;
run;

data want;
  set have;
  length key1 $14 key2 $15;
  key1=put(field1,z7.)||put(field2,z7.);
  key2=catx('|',put(field1,z7.),put(field2,z7.));
run;
Super User
Posts: 19,835

Re: Numeric Concatination with Leading zeros

Posted in reply to maseffa82
@Patrick's answer is correct. The fields probably have a format applied, but when you concatenate them it's using the unformatted value.

catx or catt are two good options to concatenate variables.
Super User
Super User
Posts: 7,063

Re: Numeric Concatination with Leading zeros

Posted in reply to maseffa82

maseffa82 wrote:

Hi Everyone!

I was working on this data that has two numeric data fields with leading zeros. I wanted to concatinate without dropping the leading zeros to create a unique key. Can you guys help me please?

Field1=0001234

Field2=0005678

New Data Field will be: New_Field=00012340005678

 

I tried this, but keeps dropping the leading zeros from both data fields before it concatinates

DATA want;

SET have

New_Field=((LEFT(Field1)) || (LEFT(Field2));

RUN;

 

Thanks Guys!

Luke.


 

Numbers cannot store eading zeros. If they are displayed that way then it is because they have a format attached to them.

If you concatenate them you will create a character variable, which could store the leading zeros.  So this statement will create a character variable with 14 digits (characters).

 

charid = put(field1,Z7.)||put(field2,Z7.);

Now if you want to create a new numeric variable then just use multiply one value by a power of 10 and add the other one. You can then attach a format to have the leading zeros appear.

numid = field1*10**7 + field2 ;
format numid Z14. ;

 

 

Super Contributor
Posts: 275

Re: Numeric Concatination with Leading zeros

Posted in reply to maseffa82

It makes no sense for numeric field with leading zero in actual data. Numeric field with leading zero you see is due to format, when you concatenate, you could not get what you want. If you need to keep new field with leading zero, you have to convert numeric field to char field.
data have;
Field1=1234;
Field2=5678;
new_field1=cats(put(field1,z7.),put(field2,z7.));
new_field2=input(new_field1,14.);
format field1 z7. field2 z7. new_field2 z14.;
output;
run;

proc contents data=have;
run;

New Contributor
Posts: 2

Re: Numeric Concatination with Leading zeros

Posted in reply to maseffa82
Thanks guys! It worked!
Ask a Question
Discussion stats
  • 5 replies
  • 400 views
  • 4 likes
  • 5 in conversation