Solved
Contributor
Posts: 41

# day, month and year extraction

[ Edited ]

How can i extract day , month and year in seperate variables from date9. variable.

Also if date is 9DEC2011 then the day should be *09* instead of just *9* as in some cases the day is single and in some it is 09.

Can anyone help.

I can use functions day() and month() but month() gives month number but i need as it is like DEC for month as shon above

Accepted Solutions
Solution
‎02-15-2017 06:48 AM
Super User
Posts: 10,519

## Re: day, month and year extraction

A day is a number is a number is a number. Numbers don't have leading zeroes.

If you absolutely need a leading zero, your target variable needs to be a string.

``day_string = put(day(datevar),z2.);``

What format should your months have? If you need the 3-letter name, use

``month = substr(put(datevar,date9.),3,3);``

Year is similar to day, just use the year() function and the z4. format.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Solution
‎02-15-2017 06:48 AM
Super User
Posts: 10,519

## Re: day, month and year extraction

A day is a number is a number is a number. Numbers don't have leading zeroes.

If you absolutely need a leading zero, your target variable needs to be a string.

``day_string = put(day(datevar),z2.);``

What format should your months have? If you need the 3-letter name, use

``month = substr(put(datevar,date9.),3,3);``

Year is similar to day, just use the year() function and the z4. format.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 41

## Re: day, month and year extraction

sorry kurt for asking it again. in other case i have data like below and need to do the same operation i.e split day, month and year

17JUL1968
1JAN2011
UNFEB2012
08UNK2016

and the month is missing then it is UNK and if the day is missing it is UN.

I needlike below

D        M     Y

---------------------

17   JUL    1968
01    JAN   2011
UN   FEB   2012
08   UNK   2016

Super User
Posts: 10,519

## Re: day, month and year extraction

You should have stated in your first post that you do not have a SAS date variable, but a string. You only have to use substr() to extract the parts.

A SAS date value can never have an unknown day or month.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,868

## Re: day, month and year extraction

Since some of your data is not a "date" in any way shape or form you will have to explicitly tell us what "value" you want from a "day" of UN or Month of UNK. From your examples:

UNFEB2012
08UNK2016

Also since you say that apparently have inconsistent value for day, your 9 vs 09 comment, provide some examples.

It helps to provide example data in the first part of your question so we don't have to guess what you have. You example should include example each type of possible malformed value. Do you have UNK for Year? What does it look like.

And then you should show your desired output for that example data.

☑ This topic is solved.