BookmarkSubscribeRSS Feed
chofmeister
Calcite | Level 5

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?

8 REPLIES 8
ballardw
Super User

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?

chofmeister
Calcite | Level 5

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.

Haikuo
Onyx | Level 15

Daja vu:

Haikuo

Astounding
PROC Star

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

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

Or, using the newer functions:

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.

MikeZdeb
Rhodochrosite | Level 12

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

Astounding
PROC Star

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.

Linlin
Lapis Lazuli | Level 10

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;

Ksharp
Super User

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

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
  • 8 replies
  • 973 views
  • 1 like
  • 7 in conversation