SAS Date Function And SAS Time Function

Hi,

Most of the time we have date in a format which contains time as well. We call it as datetime or datetime stamp. It means we are going to deal with a date which also contains time.

What could we do if we want only a specific part from given datetime stamp....

Suppose we have a variable which is containing the datetime stamp and we need to schedule the job as per the time only or in simple case we just want to make some more variables from it like date, time, hour, month and second as well.

Here we go :

For example we are taking date and time as : 26SEP97:01:47:37AM

you guys can create the main variable by using datetime() function. Just do as per the code:

Data DSN;

                                                      When=datetime();

                                                      Date=datepart(when);

                                                      Time=timepart(when);

                                                      Day=day(date);

                                                      Month=Month(date);

                                                      Year=year(date);

                                                      Hour=Hour(time);

                                                      Minute=minute(time);

                                                      Second=second(time);

                                                      Qtr=qtr(date);

                                                      Format when nldatm. date date9. time timeAMPM.;

                                                      Run;



* DSN is the name of the data set.

* Our main variable is 'When'.

* NLDTM., DATE9., TIMEAMPM. are the formats

And the result would be as follows:

In data step, what ever we wrote at the left hand side that are the variables and on the right hand side all are the function with the same name as variable.

we can also use following functions :

QTR     -  To extract quarter of the year from date.

WEEK  -  To get to know the week of the year from date.

WEEKDAY - To get the day of the week from date.

* Whenever we import data to the SAS from .txt file means if we ae using proc import to import .txt file, it's a good practice to use SAS System option Datestyle. It would put your datetime in required format.

Ex :    Options DATESTYLE = MDY;

It would read the data in sequence of Month, Day and Year when ANYDATE informat is given to the data.

2 comments: