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
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.
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.
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.
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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.