- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello SAS Community,
Is there a way to pull a numeric field in a "proc sql; connect to teradata" step and add leading zeros at the same time? So I'd want to make this sort of translation:
Have ----> Want
1 -----> 01
10 -----> 10
11 -----> 11
12 -----> 12
2 -----> 02
20 -----> 20
21 -----> 21
22 -----> 22
I feel like I've been dancing around the solution using one of the cast, input, or put functions and the format of char(2), char2., z2., 'XX', or $char2. but I just can't seem to get the right combination to get it to work. I've tried all of the commented out combinations in the code below.
proc sql;
connect to teradata;
create table add_leading_zeros as select * from connection to teradata
( select distinct
/* want_character = input(have_number,char2.)*/
/* want_character = put(have_number,char2.)*/
/* want_character = put(have_number,char(2))*/
/* want_character = put(have_number,z2.)*/
/* cast(have_number as z2.)*/
/* cast(have_number as char('XX'))*/
/* cast(have_number as char(2))*/
/* cast(have_number as char2.)*/
/* cast(have_number as $char2.)*/
/* cast(have_number as '99')*/
/* have_number format $char2.*/
/* have_number format char2.*/
/* have_number format CHAR(2)*/
/* format(have_number,CHAR(2))*/
/* put(have_number,CHAR(2)) as want_character*/
/* input(have_number,CHAR(2)) as want_character*/
from database1.table1
order by 1
);
disconnect from teradata;
quit;
I'm using SAS Enterprise Guide 9.4.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Looks like casting as a VARCHAR does the trick! Thanks for the nudge in the right direction @Reeza!
LPAD(cast(have_number as varchar(2)), 2, '0') as want_character
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
lpad(have_number, 2, '0')
https://docs.teradata.com/reader/kmuOwjp1zEYg98JsB8fu_A/e5w8LujIQDlVmRSww2E27A
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Looks like casting as a VARCHAR does the trick! Thanks for the nudge in the right direction @Reeza!
LPAD(cast(have_number as varchar(2)), 2, '0') as want_character