DATA Step, Macro, Functions and more

day, month and year extraction

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

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: 7,804

Re: day, month and year extraction

Posted in reply to alexdsa310

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

View solution in original post


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

Re: day, month and year extraction

Posted in reply to alexdsa310

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
Contributor
Posts: 41

Re: day, month and year extraction

Posted in reply to KurtBremser

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

 

Can you please help.

 

 

Super User
Posts: 7,804

Re: day, month and year extraction

Posted in reply to alexdsa310

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
Super User
Posts: 11,343

Re: day, month and year extraction

Posted in reply to alexdsa310

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.

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

Discussion stats
  • 4 replies
  • 309 views
  • 1 like
  • 3 in conversation