New Contributor
Posts: 4

# Edit ID´s

Hey,

It would be very nice if somebody help me. I try to add some 0 behind my ID´s.

So I got something like this:

01

01012

01013

01014

........

02

02012

02013

........

03

etc....

Now I want to add 3x0 behind the "01" and "02".

It should result in "01000" and "02000". The aim is to get all the ID´s to the same digit.

How is this possible?

Super User
Posts: 13,523

## Re: Edit ID´s

Are you wanting to turn 01012 into 01000? or the 01 to 01000?

Is the ID text or numeric?

Do you have any that are longer than 5 characters such as  101 101013? If so how large to they get?

New Contributor
Posts: 4

## Re: Edit ID´s

There are only two option.

Either the ID´s got 2 or 5 characters. And I need to get the ID´s all to 5 characters. So i want to add three times "0" behind the ID´s with 2 characters.

The variable is text.

Posts: 3,167

Daja vu:

Haikuo

Super User
Posts: 6,762

## Re: Edit ID´s

IF your variable ID is already defined as 5 characters long, here's a simple way:

id = strip(id) || '00000';

id = cats(id, '00000');

When the length of ID is defined as 5 characters, any characters beyond the fifth one will be discarded.

Good luck.

Valued Guide
Posts: 765

## Re: Edit ID´s

hi ... you are correct in that this ...

id1 = cats(id1, '00000');

is better than this ...

id1 = cats(id1,substr('0000',length(id1)-1));

but I do find it "interesting" that this (make a new variable when a LENGTH statement is present in the data step for the new variable) produces an error .and missing value ...

id3 = cats(id1, '00000');

data ids;

length id1 \$5;

id1 = '1';

run;

data ids;

length id3 id4 \$5;

set ids;

id2 = id1;

id1 = cats(id1,'0000');

id2 = cats(id2,'0000');

id3 = cats(id1,'0000');

id4 = cats(id1,substr('0000',length(id1)-1));

run;

LOG ... ID3 has a LENGTH of 5 but is missing ...

323  data ids;

324  length id3 id4 \$5;

325  set ids;

326  id2 = id1;

327  id1 = cats(id1,'0000');

328  id2 = cats(id2,'0000');

329  id3 = cats(id1,'0000');

330  id4 = cats(id1,substr('0000',length(id1)-1));

331  run;

WARNING: In a call to the CATS function, the buffer allocated for the result was not long enough to contain the concatenation of all the arguments. The correct result would contain 9 characters, but the actual result might either be truncated to 5 character(s) or be completely blank, depending on the calling environment. The following note indicates the left-most argument that caused          truncation.

NOTE: Argument 2 to function CATS at line 329 column 7 is invalid.

id3=  id4=10000 id1=10000 id2=10000 _ERROR_=1 _N_=1

Super User
Posts: 6,762

## Re: Edit ID´s

Mike,

Strange, but true.  I guess SAS is trying to be efficient by limiting the size of the buffer that CATS uses.  But the results become unpredictable, even more so when SAS doesn't tell us what they mean by the "calling environment".  I'll stick with my first version then, always pay attention to warnings, and remain vigilant when using new "features."

The results are different when replacing the original variable.  I could run this statement twice in a row with no problem:

id = cats(id, '00000');

Thanks.

Super Contributor
Posts: 1,636

## Re: Edit ID´s

borrowed Mike's code:

data have;

input id \$5.;

cards;

01

0101

01013

01014

;

data want;

set have;

if length(id)<5 then id = catt(id,substr('00000',length(id)+1));

proc print;run;

Super User
Posts: 10,770

## Re: Edit ID´s

I would prefer to Translate() function.

```data want;
input a \$;
length b \$ 5;
b=left(a);
b=translate(b,'0',' ');
datalines;
01
01012
01013
01014
02
02012
02013
03
;
run;

```

Ksharp

Discussion stats
• 8 replies
• 294 views
• 1 like
• 7 in conversation