BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tellmeaboutityo
Obsidian | Level 7

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; 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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; 


 

View solution in original post

3 REPLIES 3
Reeza
Super User
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; 


 

tellmeaboutityo
Obsidian | Level 7

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! 

Astounding
PROC Star

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.);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 3 replies
  • 888 views
  • 1 like
  • 3 in conversation