Help using Base SAS procedures

Formatting date from Access import

Reply
Occasional Contributor
Posts: 7

Formatting date from Access import

Hello,
I am importing a date from Access 2002-2003 into SAS 9. The date gets imported into SAS as:
ddmmmyyyy:hh:mm:ss

Even though it is in datetime19, SAS won't read it. I looked up in the documentation and found information on sasdatefmt, but I'm clearly not using it right.

Here are some of my attempts:

data FluShot2007; /*Previously imported from Access & sorted*/
format Date datetime19.;
set work.dateinfo (sasdatefmt = (Date = 'Datetime19.'));

data flu2007;
merge Patients0708 (keep = PatientID Gender) FluShot2007;
by PatientID;

FluShot = 0;
If VaccineType = 'Flu' then FluShot = 1;

Year = year(Date);
run;

2nd Attempt:

data flu2007;
merge Patients0708 (keep = PatientID Gender) FluShot2007;
by PatientID;

format Date datetime19.;
set work.dateinfo (sasdatefmt = (Date = 'Datetime19.'));
FluShot = 0;
If VaccineType = 'Flu' then FluShot = 1;

Year = year(Date);
run;

3rd attempt:

data flu2007;
merge Patients0708 (keep = PatientID Gender) FluShot2007;
by PatientID;

format Date datetime19.;

FluShot = 0;
If VaccineType = 'Flu' then FluShot = 1;

Year = year(Date);
run;

Can someone help tell me what I am doing wrong? Thanks!
SAS Super FREQ
Posts: 8,742

Re: Formatting date from Access import

Hi:
It looks to me like you have read the documentation:
http://support.sas.com/documentation/cdl/en/acpcref/61891/HTML/default/a003079774.htm
http://support.sas.com/documentation/cdl/en/acreldb/63023/HTML/default/a001371624.htm
http://support.sas.com/kb/6/413.html

However, my interpretation of the notes is that you use the SASDATEFMT option either in your config file or on your LIBNAME or SET statement when you are using SAS/ACCESS or the various engines to initially READ the data from Oracle or DB2 or Microsoft Access into a SAS data set.

Once your data are in SAS data format, then I believe that the SASDATEFMT option will no longer impact the data. You would then need to use other SAS formats, functions or conversion methods to impact the date or create a new variable.

You might investigate using the DATEPART function or the TIMEPART function to create new variables, as shown in the code below.

cynthia
[pre]
data testdate;
bday_time = '15Nov1950:07:15:35'dt;
try1 = year(bday_time);
try2 = year(datepart(bday_time));
month = month(datepart(bday_time));
bday = day(datepart(bday_time));
timeval = timepart(bday_time);
run;

proc print data=testdate;
format bday_time datetime18. timeval time8.;
run;

[/pre]

Output from above program is:
[pre]
Obs bday_time try1 try2 month bday timeval

1 15NOV50:07:15:35 . 1950 11 15 7:15:35

[/pre]
Occasional Contributor
Posts: 7

Re: Formatting date from Access import

That worked perfectly. Thank you!

(Although, upon further inspection, I realized that all my data was from 2007, and I could have just said 'Year = 2007;' but hey, I've learned something new! And I import from Access all the time)
Ask a Question
Discussion stats
  • 2 replies
  • 130 views
  • 0 likes
  • 2 in conversation