DATA Step, Macro, Functions and more

format a date variable in to JAN18(monthyear) format

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

format a date variable in to JAN18(monthyear) format

Dear All,

 

I  need to convert the date format variable in to month-year format.

 

04JAN18:10:22:00(type=date)---->JAN18(type=date)

 

date_format.PNG

 

Thanks,

Chithra


Accepted Solutions
Solution
‎01-04-2018 09:08 AM
Super User
Super User
Posts: 9,799

Re: format a date variable in to JAN18(monthyear) format

Type=date - you don't seem to understand what a date or time actually is.  Dates are stored as the number of days since a certain timepoint, so all dates are stored this way.  Same with times, number of seconds since midnight.  Both are numbers, which can be formatted in different ways.  So if your Created variable is indeed numeric then you should be able to do:

data want;
  set have;
  new_created=datepart(created);
  format new_created monyy5.;
run;

View solution in original post


All Replies
Solution
‎01-04-2018 09:08 AM
Super User
Super User
Posts: 9,799

Re: format a date variable in to JAN18(monthyear) format

Type=date - you don't seem to understand what a date or time actually is.  Dates are stored as the number of days since a certain timepoint, so all dates are stored this way.  Same with times, number of seconds since midnight.  Both are numbers, which can be formatted in different ways.  So if your Created variable is indeed numeric then you should be able to do:

data want;
  set have;
  new_created=datepart(created);
  format new_created monyy5.;
run;
Contributor
Posts: 28

Re: format a date variable in to JAN18(monthyear) format

Thanks..It works.

Thanks for the explanation also

Super User
Posts: 10,530

Re: format a date variable in to JAN18(monthyear) format

04JAN18:10:22:00 is not a date value, but a datetime value.

Try this

newvar = datepart(oldvar):
format newvar monyy5.;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Valued Guide
Posts: 538

Re: format a date variable in to JAN18(monthyear) format

Date variables are not stored in a formatted way. They are stored numerically as the number of days since 01JAN1960. Only when presenting them in a report or a table viewer a format is used. Conversion is not needed when you want to reconsider the presentation format of your variable. In your case the format MONYY5. would the appropriate one. MONYY7. would also print the century.

 

Formats are a fundamental concept in SAS. Have a look at the doc if you want to read up on how they are used in SAS. 

 

Hope this helps,

-- Jan.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 232 views
  • 2 likes
  • 4 in conversation