DATA Step, Macro, Functions and more

About SAS dates conversions

Reply
N/A
Posts: 0

About SAS dates conversions

Only some numerics can be converted to SAS dates directly right? Such as, mmddyy, 040609? You can use format mmddyy8.

But there are special cases where you can't convert them directly and need to go through an extra step rgith? Such as, yyyymmdd, 20090406? You have to convert 20090406 to character using put then convert it to date right?

I just want to make sure if I understand this correctly. Conclusion is that only some numerics can be converted to SAS date directly?

thanks.
Super Contributor
Super Contributor
Posts: 3,174

Re: About SAS dates conversions

Posted in reply to deleted_user
There is the SAS INFORMAT (not format) called ANYDTDTE. which is a bit more forgiving. Here's a sample code snippet to demonstrate:

data _null_;
length cdt $10;
format dt date9.;
cdt = '2005/01/01';
dt = input(cdt,anydtdte10.);
putlog _all_;
/* need to consider YEARCUTOFF= for these */
cdt = '01/01/99';
dt = input(cdt,anydtdte10.);
putlog _all_;
cdt = '1/1/1';
dt = input(cdt,anydtdte10.);
putlog _all_;
run;



Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: About SAS dates conversions

I see. Thanks!
N/A
Posts: 0

Re: About SAS dates conversions

Posted in reply to deleted_user
You can use the yymmdd8. format for your case.

x='20090406';
date1=input(x, yymmdd8.);

~ Sukanya E
N/A
Posts: 0

Re: About SAS dates conversions

Posted in reply to deleted_user
Thanks. In other words, I can convert any numeric to SAS dates?
SAS Super FREQ
Posts: 8,864

Re: About SAS dates conversions

Posted in reply to deleted_user
Hi:
Sure, you can convert any -NUMBER- to a SAS date or SAS date/time value, as long as that number either
1) represents the number of days from Jan 1, 1960 (so the number -3334 represents Nov 15, 1950 and the number 3334 is Feb 16, 1969 and the number 18216 is Nov 15, 2009)
or
2) represents the number of seconds from midnight Jan 1, 1960 (date/time value)

However, a NUMERIC variable value like this: 19501115 or a character variable value like this: '19501115' does NOT represent the number of days from Jan 1, 1960 -- so it must be converted from being a number or a character string so that SAS recognizes it as a date variable value and correctly stores your date value as the number of days since Jan 1, 1960.

So when you say "any" numeric, I have to say that the answer is, yes, if you understand SAS date/time values and SAS date/time functions.

cynthia
N/A
Posts: 0

Re: About SAS dates conversions

Posted in reply to Cynthia_sas
hm..i think i need to read the section SAS dates again. I need to tell whether if the number is a qualified number or a number that I need to convert to characters first. Also, Cynthia, how can you be so good with SAS? I see that you reply pretty much every thread here. How can you know everything about SAS? That's amazing! Any book recommendations? Thanks!
SAS Super FREQ
Posts: 8,864

Re: About SAS dates conversions

Posted in reply to deleted_user
Hi:
Thanks for the compliment. Long ago, (in a galaxy far, far away) I was a Literature student who discovered that there were no good jobs for someone who could deconstruct Faulkner, Melville, Wallace Stevens and T. S. Eliot. And, so I aimed myself at a programmer trainee job; learned Fortran, Cobol, JCL SAS and Easytreve.

But SAS was actually the very first programming language I learned, before the programmer trainee job even started. So when I got the opportunity to be a "real" programmer, one of my choices was to work in the Cobol side of the shop or the "ad-hoc" SAS side of the shop. I chose working on the SAS side of things and have been using SAS ever since.

So it's been a very, -very- long time that I have worked with SAS. I don't know "everything" about SAS -- but I do know how to look things up in the documentation. And, there's the whole puzzle aspect of solving a problem that I find appealing.

My students all speak highly of these authors: Cody, Carpenter, Delwiche/Slaughter, Burlew and Haworth. But there are many other good books available on different topics (For example, I don't teach Stat classes, so I don't know what Stat-related books are most useful.)

The best advice I can give you is learn how to use the resources on support.sas.com -- the search facility, the documentation, the samples and the repository of SAS Global Forum papers. And, don't be afraid to ask yourself "what if" questions and test out your theories or ideas in a test program on sample data.

Good luck!
cynthia
N/A
Posts: 0

Re: About SAS dates conversions

Posted in reply to Cynthia_sas
Thank you for the inputs and advices. I will definitly look into these resources. But I think I should read a few SAS books again just to get a better understanding on everything. I went through both the BASE and ADVANCED SAS certification exam and I feel at the end I remembered and learned nothing. I keep getting stuck on small problems. This just makes me sad. It is like spending so much time and effort and at end you got nothing from it. I have been a SAS programmer for a year and half now and I feel that my skills on SAS did not improve at all. Ok, i need to stop whining and start reading and check on these resources. Thanks again!
Super Contributor
Super Contributor
Posts: 3,174

Re: About SAS dates conversions

Posted in reply to deleted_user
My recommendation - stop reading and start coding!

Then, refer to the SAS documentation and SAS support website resources, as needed, or when you might want to review a particular PROC and/or DATA step programming element.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: About SAS dates conversions

Yes. I think you are right. This is a good place to start. Where I can try to solve other people's questions which will give me practice and help memorize the functions.
N/A
Posts: 0

Re: About SAS dates conversions

Posted in reply to deleted_user
hehe, i thought that didn't work but that's because my output wasn't formatted. thanks. Message was edited by: cosmid
N/A
Posts: 0

Re: About SAS dates conversions

Posted in reply to deleted_user
i ran through this exercise a little while ago.

to import a YYYYMMDD-style integer into a sas date value, i use the ND8601DA. informat and apply the date9. format. YYYYMMDDHHMMSS-style integer into a sas datetime value, one can use the ND8601DT. informat and apply the datetime19. format.

http://quatch.koopmann.us/2009/05/reading-iso-dates-and-datetimes.html
N/A
Posts: 0

Re: About SAS dates conversions

Posted in reply to deleted_user
oh, thanks. never heard about this format. will read about it now.
Ask a Question
Discussion stats
  • 13 replies
  • 425 views
  • 0 likes
  • 3 in conversation