BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
13 REPLIES 13
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
I see. Thanks!
deleted_user
Not applicable
You can use the yymmdd8. format for your case.

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

~ Sukanya E
deleted_user
Not applicable
Thanks. In other words, I can convert any numeric to SAS dates?
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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!
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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!
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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.
deleted_user
Not applicable
hehe, i thought that didn't work but that's because my output wasn't formatted. thanks. Message was edited by: cosmid
deleted_user
Not applicable
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
deleted_user
Not applicable
oh, thanks. never heard about this format. will read about it now.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1204 views
  • 0 likes
  • 3 in conversation