BookmarkSubscribeRSS Feed
Ujjawal
Quartz | Level 8

Hi Team,

I have dates stored in character format. They look like - "15Jul2014". I need to convert it into 2014-Jul-15 i.e. YYYY-MMM-DD . Is there any easy way to convert it?


Thanks in anticipation!

5 REPLIES 5
skillman
SAS Employee

What software are you using? Base SAS, Enterprise Guide, SAS Data Management, SAS Data Integration, etc.?

-shawn

Ujjawal
Quartz | Level 8

I am using Base SAS. Does your answer vary in Base SAS and SAS Enterprise Guide? Thanks!

Ksharp
Super User

You need to define a format for yourself .

Code: Program

proc format;
picture fmt
low-high='%Y-%b-%0d'(datatype=date);
run;
data x;
a='15Jul2014'd;
format a fmt12.
run;

Xia Keshan

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Alternatively just create a text field:

data want;

     set have;

     length new_date $10;

     new_date=catx('-',substr(old_date,6,4),substr(old_date,3,3),substr(old_date,1,2));

run;

cov_derek
Fluorite | Level 6

Ujjawal,

Is your date really a character variable? If so, I would urge you to convert it into a SAS date value first.

If your date is stored as a SAS date value, RW9's solution won't work without additional coding. If it is a character value, then you won't be able to do any calulations with it without code to do that transformation to a SAS date value. Xia's solution is the easiest way of dealing with the fact that SAS does not have every possible date format available. Only change I would make is I would use a more descriptive format name than "fmt".

IMHO, there is no reason to represent a date as a character variable in a SAS table or view, UNLESS you are dealing with complex ISO8601 dates and need to calculate ISO durations and intervals using CALL IS8601_CONVERT.

Derek

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 2441 views
  • 2 likes
  • 5 in conversation