Hello,
I'm trying to force a date variable into a character variable. This doesn't work, which makes me sad:
data want; set have;
format originaldate mmddyyd10.;
length chardate $9;
chardate = originaldate;
run;
Below does work, but there has to be an easier way?
data want; set have;
format originaldate mmddyyd10.;
originaldate = datepart(originaldate);
year = year(originaldate);
day = day(originaldate);
month = month(originaldate);
length month_c $5;
if month = 1 then month_c = '-Jan-';
if month = 2 then month _c = '-Feb-';
if month = 3 then month _c = '-Mar-';
if month = 4 then month _c = '-Apr-';
if month = 5 then month _c = '-May-';
if month = 6 then month _c = '-Jun-';
if month = 7 then month _c = '-Jul-';
if month = 8 then month _c = '-Aug-';
if month = 9 then month _c = '-Sep-';
if month = 10 then month _c = '-Oct-';
if month = 11 then month _c = '-Nov-';
if month = 12 then month _c = '-Dec-';
length chardate $9;
chardate = catt(day, month_c, year);
run;
char_date = put(originalDate, mmddyy10.);
SAS does not have a format for DD-MON-YYYY if that's the format you want, I would suggest creating your own format and combining it.
Either way your code could be simplified to:
char_date = CATX('-', put(day(originalDate), z2.), put(originalDate, monname3.), put(year(originalDate), 4.));
@tellmeaboutityo wrote:
Hello,
I'm trying to force a date variable into a character variable. This doesn't work, which makes me sad:
data want; set have;
format originaldate mmddyyd10.;
length chardate $9;
chardate = originaldate;run;
Below does work, but there has to be an easier way?
data want; set have;
format originaldate mmddyyd10.;
originaldate = datepart(degconfdate);
year = year(originaldate);
day = day(originaldate);
month = month(originaldate);if month = 1 then month_c = '-Jan-';
if month = 2 then month _c = '-Feb-';
if month = 3 then month _c = '-Mar-';
if month = 4 then month _c = '-Apr-';
if month = 5 then month _c = '-May-';
if month = 6 then month _c = '-Jun-';
if month = 7 then month _c = '-Jul-';
if month = 8 then month _c = '-Aug-';
if month = 9 then month _c = '-Sep-';
if month = 10 then month _c = '-Oct-';
if month = 11 then month _c = '-Nov-';
if month = 12 then month _c = '-Dec-';
length chardate $9;chardate = catt(day, month_c, year);
run;
char_date = put(originalDate, mmddyy10.);
SAS does not have a format for DD-MON-YYYY if that's the format you want, I would suggest creating your own format and combining it.
Either way your code could be simplified to:
char_date = CATX('-', put(day(originalDate), z2.), put(originalDate, monname3.), put(year(originalDate), 4.));
@tellmeaboutityo wrote:
Hello,
I'm trying to force a date variable into a character variable. This doesn't work, which makes me sad:
data want; set have;
format originaldate mmddyyd10.;
length chardate $9;
chardate = originaldate;run;
Below does work, but there has to be an easier way?
data want; set have;
format originaldate mmddyyd10.;
originaldate = datepart(degconfdate);
year = year(originaldate);
day = day(originaldate);
month = month(originaldate);if month = 1 then month_c = '-Jan-';
if month = 2 then month _c = '-Feb-';
if month = 3 then month _c = '-Mar-';
if month = 4 then month _c = '-Apr-';
if month = 5 then month _c = '-May-';
if month = 6 then month _c = '-Jun-';
if month = 7 then month _c = '-Jul-';
if month = 8 then month _c = '-Aug-';
if month = 9 then month _c = '-Sep-';
if month = 10 then month _c = '-Oct-';
if month = 11 then month _c = '-Nov-';
if month = 12 then month _c = '-Dec-';
length chardate $9;chardate = catt(day, month_c, year);
run;
Reeza-- If you keep being so awesome, I'm going to owe you a martini!
Astounding-- I didn't specify a result because I'm outputting multiple formats and wanted a general solution. Thanks for the help, though!
You didn't actually specify what you want the result to look like. Here's a simple conversion:
data want;
set have;
chardate = put( datepart(degconfdate), date9.);
run;
If you really want dashes in between, switch to:
chardate = put( datepart(degconfdate), date11.);
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.