07-11-2012 10:22 AM
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:
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?
07-11-2012 10:30 AM
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?
07-11-2012 10:37 AM
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.
07-11-2012 11:11 AM
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.
07-11-2012 12:25 PM
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');
length id1 $5;
id1 = '1';
length id3 id4 $5;
id2 = id1;
id1 = cats(id1,'0000');
id2 = cats(id2,'0000');
id3 = cats(id1,'0000');
id4 = cats(id1,substr('0000',length(id1)-1));
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));
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
07-11-2012 01:59 PM
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');
07-11-2012 11:35 PM
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;