Help using Base SAS procedures

SAS Dates

Reply
Occasional Contributor
Posts: 9

SAS Dates

How do I convert serial Numbers within raw data into regular dates within SAS? I have an excel spreadsheet that has most of its dates as serial numbers rather than general dates. 

Trusted Advisor
Posts: 1,374

Re: SAS Dates

Please post few lines with relevant varaibles only from the excel and explain, better by wanted output, what do you want.

Occasional Contributor
Posts: 9

Re: SAS Dates

Wanted Variable to be corrected Trap_Coll_Date;

Example:

---> 41503 is stored in excel, but it represents 8/17/2016 

 

Some dates are stored as serial numbers, some dates are stored as regular dates 

 

I can convert the serial numbers in excel into dates, but importing it to SAS reveals the original serial number from Excel. How to I fix this ? 

Trusted Advisor
Posts: 1,374

Re: SAS Dates

Assign excel cells format to display dates, then close and save the excel with its new format.

Import the excel only when closed.

Super User
Posts: 10,500

Re: SAS Dates

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

Do you mean you are seeing values like 40123 in the spreadsheet that may represent 11/6/2009?

It may be as simple as setting the column to display as a date in Excel using the FORMAT Cells options.

 
Occasional Contributor
Posts: 9

Re: SAS Dates

Yes it appears like that, I've tried using the format options to convert the serial numbers to dates.

The problem is not all the dates  were imputed into Excel as serial numbers. Some values appear as dates when formated options were changed from date to general. 

 

This is the SAS output after importing the excel file

Occasional Contributor
Posts: 9

Re: SAS Dates

Specifcally looking at the dates column, you will find that some of the output is in serial number format and some show actual dates. 

Trusted Advisor
Posts: 1,374

Re: SAS Dates

Maybe you just need add sas format to the date:

   format date date9. ;  /* or ddmmyy10. or mmddyy10. or any other available date format */

Occasional Contributor
Posts: 9

Re: SAS Dates

Alright, what is the procedure to adjust dates within excel spread sheets in sas 

do I type an informat line before typing a format ? 

Trusted Advisor
Posts: 1,374

Re: SAS Dates

What is your code to import the excel ?

Occasional Contributor
Posts: 9

Re: SAS Dates

My Code- 

proc import datafile="C:\Users\okund\Desktop\Practicum Documents\2016MosqDataEntry.xlsx"
out=Grad.mosdata
dbms=xlsx replace;
getnames=yes;
run;

Trusted Advisor
Posts: 1,374

Re: SAS Dates

I'm not sure you can add the date format in PROC IMPORT.

So just add next code:

      proc import .....   /* your code as is */

         out=Grad.mosdata

          ...  

      run;

      proc datasets lib=grad nolist;

              modify mosdata;

              format date date9.;   /* or any other SAS date format */

     run; quit;

Trusted Advisor
Posts: 1,374

Re: SAS Dates

check the dates that are correct as in excel
Occasional Contributor
Posts: 9

Re: SAS Dates

I used the code you sent me, and sas Log stated this: 

 

1320 proc datasets lib=grad nolist;
1321 modify mosdata;
ERROR: You are trying to use the numeric format DATE with the character variable trap_coll_date in
data set GRAD.MOSDATA.
1322 format Trap_Coll_Date date9.; /* or any other SAS date format */
1323 run; 

 

is this because sas is reading dates as a character variabel instead of a numberic variable ?

 

Occasional Contributor
Posts: 9

Re: SAS Dates

Also why is SAS reading Date as a character variable ?

Ask a Question
Discussion stats
  • 20 replies
  • 556 views
  • 2 likes
  • 4 in conversation