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.
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
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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.