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