- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Could you please let me know any trick to pad character variable with leading zeroes?
I have a 4 byte variable,if the data in that field is less than 4 bytes,then I would like to pad the value with leading zeroes.
Please advice.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your character variable value is all digit? Use INPUT to read as a number, then Z4. format to add leading zeroes.
data test;
length charvar $ 4 padded $ 4;
infile datalines;
input charvar;
/* read charvar as number, then format with leading zeroes */
/* then put result into padded */
padded = put(input(charvar,best4.),z4.);
datalines;
28
1034
4
8759
1234
234
34
;
run;
Output:
charvar padded 28 0028 1034 1034 4 0004 8759 8759 1234 1234 234 0234 34 0034
Ksharp
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your character variable value is all digit? Use INPUT to read as a number, then Z4. format to add leading zeroes.
data test;
length charvar $ 4 padded $ 4;
infile datalines;
input charvar;
/* read charvar as number, then format with leading zeroes */
/* then put result into padded */
padded = put(input(charvar,best4.),z4.);
datalines;
28
1034
4
8759
1234
234
34
;
run;
Output:
charvar padded 28 0028 1034 1034 4 0004 8759 8759 1234 1234 234 0234 34 0034
Ksharp
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Ksharp
In case I have a variable(character format) with both digits and characters, how will the code change?
For instance,
Raw data Desired output
007851 007851
05614 005614
2447 002447
Could you please help?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi
I just got the solution to my problem here:
https://groups.google.com/forum/#!topic/comp.soft-sys.sas/uBuLYXMRqhw
if length(raw data) = 6 then new=raw data;
else new=repeat('0',6-length(raw data)-1)||raw data;
Just thought would share the code here if anyone needs
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Namrata,
I believe your code will not produce a desired output if the input records with length more than 6.
can anyone suggests?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
OK. Here are two solutions . Pick one up you like :
data have; input Raw $char6.; cards; 007851 05614 2447 ; run; /*** the first way ***/ data want1; set have; want=translate(right(raw),'0',' '); run; /*** the second way ***/ data want2; set have; want=put(input(raw,best32.),z6.); run;
Xia Keshan
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have a similar problem. My zip variable data has following values:
zip
123456789
123456789
1234
1234
12345
123456789
12345
12345
1234
123456789
I want to get a single variable with:
- first 5 digit from 9 digit zip var;
- add leading zero where there is 4 digit zip and
- keep five digit zip value as it is.
Thanking you in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Found the answer to my question:
data test2; set ptcz2;
zip5 = substr(compress(zip),1,5);
zip5v2 = substr(("0000"||zip5),length(zip5),5);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have a similar problem. My data has following values:
zip
123456789
-123456789
1234
-.234
12345
-.23456789
12345
12345
1234
123456789
I want to get a single variable with:
- first 6 digit from 9 digit zip var;
- add leading zero where there is 1 less digit zip and
- keep 6 digit zip value as it is.
- I tried increasing the zw.d format to 7.3 and its working for values with negative sign but the other values are not correct. Expected format is 6.3.
Thank you