Help using Base SAS procedures

Date and Time Stamps

Reply
Occasional Contributor
Posts: 18

Date and Time Stamps

I'm trying to develop a column in my SAS dataset that combines both date and time into one column (Time1):

Date Time Time1
11/22/2008 0:08:26 xxxxxxxxx
11/22/2008 0:16:26 xxxxxxxxx
11/22/2008 0:24:26 xxxxxxxxx

so I can run an analysis accounting for temporal autocorrelation in my dataset. I saw a SAS article on combining date and time data together to give me the following format (ex. 1482223680). This is exactly what I need but I was unable to clearly follow how to derive this in my extensively large dataset (>=100,000 lines of data). Supposedly, the date represents days since 1/1/1960 and time represent seconds since midnight or midnight of 1/1/1960 if specifying a datetime variable. Does anyone know of an easy coding to create this date/time stamp based on two seperate columns (i.e., Date and time)? Thank you very much!
Occasional Contributor
Posts: 11

Re: Date and Time Stamps

PROC Star
Posts: 7,492

Re: Date and Time Stamps

Use the dhms function. See, e.g.,
http://www.hollandnumerics.co.uk/sasfaq/SASFAQ1.HTM

HTH,
Art
----------
> I'm trying to develop a column in my SAS dataset that
> combines both date and time into one column (Time1):
>
> Date Time
> Time1
> 0:08:26 xxxxxxxxx
> 22/2008 0:16:26
> xxxxxxxxx
> 0:24:26 xxxxxxxxx
> can run an analysis accounting for temporal
> autocorrelation in my dataset. I saw a SAS article
> on combining date and time data together to give me
> the following format (ex. 1482223680). This is
> exactly what I need but I was unable to clearly
> follow how to derive this in my extensively large
> dataset (>=100,000 lines of data). Supposedly, the
> date represents days since 1/1/1960 and time
> represent seconds since midnight or midnight of
> 1/1/1960 if specifying a datetime variable. Does
> anyone know of an easy coding to create this
> date/time stamp based on two seperate columns (i.e.,
> Date and time)? Thank you very much!
Occasional Contributor
Posts: 18

Re: Date and Time Stamps

Ok! I've read over the comments you posted below and still am a little confused. See my SAS code below:

DATA DATETIME;
SET DEER1;
RUN;

DATA CORRECT;
SET DATETIME;
INFORMAT DATE MMDDYY10.;
DATE2=DATE;
FORMAT DATE2 MMDDYY10.;
HRID=HMS(HOUR,MINUTE,SECOND);
H_M_S=HRID;
FORMAT H_M_S TIME.;
DTID=DHMS(DATE2,HOUR,MINUTE,SECOND);
RUN;

PROC PRINT; RUN;

DATA HABITAT; SET CORRECT; RUN;



Do you see any inherent problems? I'm importing the data from an Excel file because I have approximately 100,000 lines of data and figured it would be easier to make sure things don't get jumbled up in the process of using DATALINES. I ran the code last night and kept getting the following errors:

46573 DATA DATETIME;
46574 SET DEER1;
46575 RUN;
NOTE: There were 46492 observations read from the data set WORK.DEER1.
NOTE: The data set WORK.DATETIME has 46492 observations and 37 variables.
NOTE: DATA statement used (Total process time):
real time 0.10 seconds
cpu time 0.09 seconds
 
46576 DATA CORRECT;
46577 SET DATETIME;
46578 INFORMAT DATE MMDDYY10.;
---------
48
ERROR 48-59: The informat $MMDDYY was not found or could not be loaded.
46579 DATE2=DATE;
46580 FORMAT DATE2 MMDDYY10.;
---------
48
ERROR 48-59: The format $MMDDYY was not found or could not be loaded.
46581 HRID=HMS(HOUR,MINUTE,SECOND);
46582 H_M_S=HRID;
46583 FORMAT H_M_S TIME.;
46584 DTID=DHMS(DATE2,HOUR,MINUTE,SECOND);
46585 RUN;
NOTE: Character values have been converted to numeric values at the places given by:
(Line)Smiley SadColumn).
46584:11
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.CORRECT may be incomplete. When this step was stopped there were 0
observations and 41 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.06 seconds
 
46586 PROC PRINT; RUN;
NOTE: No observations in data set WORK.CORRECT.
NOTE: PROCEDURE PRINT used (Total process time):


Thank you very much for any help that you can give me!
Super Contributor
Super Contributor
Posts: 3,174

Re: Date and Time Stamps

When SAS adds a "$" that means that the SAS variable type is CHARACTER, not the required numeric -- honestly, it's a lame diagnostic indication but hey it's SAS and the SAS environment has been generating such goofy warnings/errors since SAS 79 era.

So, when you are using a SAS NUMERIC type variable, the associated FORMAT/INFORMAT also must be for the same variable type.

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 18

Re: Date and Time Stamps

So are you saying that I can't create the DATE2 column? The INFORMAT date and time are in the following format: Date = 11/22/2008 and Time = 09:22:26. My goal is to create a time stamp joining both date and time. I see your point about FORMAT and INFORMAT must be the same variable type but doesn't the code reflect that? Sorry for the confusion! Thank you very much for your help!
Super Contributor
Super Contributor
Posts: 3,174

Re: Date and Time Stamps

Declaring an INFORMAT statement does not automatically performing any interpretation or data-conversion. The issue is that when you declare your SET statement, then you have associated the DATE variable as a CHARACTER type SAS variable/column.

If the data-string is character, then you must use the INPUT function in a SAS (new) variable assignment statement to convert using the appropriate INFORMAT. The same would need to occur with DATE2, regarding the INPUT function.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

convert character date site:sas.com

input function convert date variable site:sas.com
Valued Guide
Posts: 2,177

Re: Date and Time Stamps

assuming these are string columns of date and time, you can make one timestamp with the code using the INPUT() function with the informat MDYAMPM.
[pre]timestamp = input( date !!'-'!! time, mdyampm. ) ;
Ask a Question
Discussion stats
  • 7 replies
  • 1160 views
  • 0 likes
  • 5 in conversation