BookmarkSubscribeRSS Feed
maseffa82
Calcite | Level 5

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.

5 REPLIES 5
Patrick
Opal | Level 21

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;
Reeza
Super User
@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.
Tom
Super User Tom
Super User

@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. ;

 

 

slchen
Lapis Lazuli | Level 10

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;

maseffa82
Calcite | Level 5
Thanks guys! It worked!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 6507 views
  • 6 likes
  • 5 in conversation