SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

forcing date into character

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

forcing date into character

[ Edited ]

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; 


Accepted Solutions
Solution
‎02-21-2018 11:18 AM
Super User
Posts: 23,237

Re: forcing date into character

Posted in reply to tellmeaboutityo
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


All Replies
Solution
‎02-21-2018 11:18 AM
Super User
Posts: 23,237

Re: forcing date into character

Posted in reply to tellmeaboutityo
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; 


 

Occasional Contributor
Posts: 9

Re: forcing date into character

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! 

Super User
Posts: 6,626

Re: forcing date into character

[ Edited ]
Posted in reply to tellmeaboutityo

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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