DATA Step, Macro, Functions and more

Date time formats

Reply
N/A
Posts: 0

Date time formats

Hi,

Would someone please let me know hw to convert this date to CCYYMMDD format.

Jun 31 2007 12:00AM

thanks,
sasbase9
SAS Employee
Posts: 174

Re: Date time formats

Posted in reply to deleted_user
There is no such date as jun 31 !!

June only has 30 days :-)
Super Contributor
Super Contributor
Posts: 3,174

Re: Date time formats

Posted in reply to GertNissen
In a DATA step, you would use the appropriate INFORMAT with either an INPUT statement or INPUT function to parse the character string to a SAS numeric variable that represents a "date" (important for choosing your INFORMAT).

Then you must convert the "date" variable to be a "datetime" variable likely using the DHMS function - my preference.

Here's a general example that reads a date character string in a particular input format, converting to a date, and then assigning a new variable as a datetime:

DATA _NULL_;
format mydate date9. ;
INPUT mydate yymmdd8.;
format mydatetime datetime21. ;
mydatetime = dhms(mydate,0,0,0);
putlog _all_;
datalines
20090101
run;

The SAS support http://support.sas.com/ website has technical documentation and supplemental conference and sample papers on this type of topic - I have provided links below for reference.

Scott Barry
SBBWorks, Inc.


Dates, Times, and Intervals
About SAS Date, Time, and Datetime Values
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002200738.htm

Reading Raw Data
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a001112330.htm

SAS 9.2 Language Reference: Dictionary - SAS INFORMATs and FORMATs are documented here:
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/titlepage.htm
N/A
Posts: 0

Re: Date time formats

Posted in reply to deleted_user
Hi,

Thanks SBB to reply back. I tried using below logic in my data set.

INPUT_VAR_DT_MM = PUT(month(PUT(input(var1,date9.),10.)),Z2.) ;


but it is not working. Our date on raw file is Like I said above , for ex : Nov 30 2008 12:08 AM.


Can you pls again let me know what needs to be corrected in my above logic.

thanks for help,
sasbase9
SAS Employee
Posts: 174

Re: Date time formats

Posted in reply to deleted_user
Try [pre]
data test;
var1 = 'Nov 30 2008 12:08 AM';
INPUT_VAR_DT_MM = input(scan(var1,2)!!scan(var1,1)!!scan(var1,3), date9.);
put INPUT_VAR_DT_MM= INPUT_VAR_DT_MM= date.;
run;[/pre]
This will give you a SAS date (not datetime)!
Super Contributor
Super Contributor
Posts: 3,174

Re: Date time formats

Posted in reply to GertNissen
Consider simplifying your code, for the "input" side - here is an example:

data _null_;
var1 = 'Nov 30 2008 12:08 AM';
format INPUT_VAR_DT_MM datetime. ;
INPUT_VAR_DT_MM = input(var1,anydtdtm.);
put _all_;
run;


Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 5 replies
  • 218 views
  • 0 likes
  • 3 in conversation