Converting dates in SAS - date

I'm quite new to SAS programming and I'm struggling with dates in it.
I have a dataset in SAS where dates are written in this format 16NOV2007:00:00:00 and I need to convert it do this format dd/mm/yyyy
Can anyone help in that?

In the following example
datetime_str is your original datetime, as a string (if it's not a string then all you need is the datepart() function and a proper format).
sasdate is the date part of datetime_str and is stored as a SAS date (which is numeric) but given a ddmmyy format.
date_str (which might not be
needed in your case) is a re-writing of the sasdate into a string
variable, using the same ddmmyy format as before.
SAS Code
data dates;
format datetime_str $20.
sasdate ddmmyys10.
date_str $10.;
datetime_str = "16NOV2007:00:00:00";
sasdate = datepart(input(datetime_str, datetime18.));
date_str = put(sasdate, ddmmyy10.);
run;
Results
datetime_str sasdate date_str
16NOV2007:00:00:00 16/11/2007 16/11/2007

Related

How Should I Format Dates in SAS from Strings to Numeric

I am trying to convert a string to a numeric in SAS. Currently, it looks like 05/23/2007. My code so far is
Data Data2;
Set Data1;
Input(Date, mmddyy10w.);
If Date > '07/15/2009'd;
run;
I get an error saying that the format mmddyy10w. cannot be recognized. Does anyone know how to fix this?
Correct informat to read date in the scenario is mmddyy10., date literal should be like '14Jul2009'd. as shown in below example
Data Data2;
date= Input('07/15/2009',mmddyy10.);
format date mmddyy10.;
If Date > '14Jul2009'd;
run;
below is the link, which gives good idea format and informats of dates in SAS.
https://support.sas.com/resources/papers/proceedings15/1334-2015.pdf

SAS date conversion from text.

Hi I have a date conversion problem in SAS,
I imported an excel file which has the following dates.,
2012-01-09
2011-01-31
2010-06-28
2005-06-10
2012-09-19
2012-09-19
2007-06-12
2012-09-20
2004-11-01
2007-03-27
2008-06-23
2006-04-20
2012-09-20
2010-07-14
after I imported the dates have changed like this
40917
40574
40357
38513
41171
41171
39245
41172
38292
39168
39622
38827
41172
40373
I have used the input function to convert the dates but it gives a strange result.,
the code I used.,
want_date=input(have_date, anydtdte12.);
informat want_date date9.; format have_date date9.;run;
I get very stange and out of the World dates., any idea how can I convert these?
You can encourage SAS to convert the data as date during the import, although this isn't necessarily a panacea.
proc import file=whatever out=whatever dbms=excel replace;
dbdsopts=(dbSasType=( datevar=date ) );
run;
where datevar is your date column name. This tells SAS to expect this to be a date and to try to convert it.
See So Your Data Are in Excel for more information, or the documentation.
From : http://www2.sas.com/proceedings/sugi29/068-29.pdf
Times are counted internally in SAS as seconds since midnight and
date/time combinations are calculated as the number of seconds since
midnight 1 January 1960.
Excel also uses simple numerical values for dates and times
internally. For the date values the difference with the SAS date is
only the anchor point. Excel uses 1 January 1900 as day one.
So add a constant.
EXAMPLES:
SAS_date = Excel_date - 21916;
SAS_time = Excel_time * 86400;
SAS_date_time = (Excel_date_time - 21916) * 86400;
As Justin wrote you need to correct for the different zero date (SAS vs. Excel).
Then you just need to apply a format (if you want to get a date variable to do calculations):
want_date = have_date-21916;
format want_date date9.;
Or convert it to a string:
want_date = put(have_date-21916, date9.);
In either case you can choose the date format you prefer.

Converting SAS Character of form "mo-yr" to a SAS date

I'm trying to convert a SAS string of the form "MO-YR" (e.g. "Jan-04") to a SAS date.
Unfortunately, I don't think there is a date format in SAS that takes that form, so I can't just use an input statement like this date = input(datestring, sasformat).
I've been using this site to find date formats: http://v8doc.sas.com/sashtml/lrcon/zenid-63.htm
Thanks,
Michael
MONYY seems to work for me.
data _null_;
input #1 mydate monyy6.;
put mydate= date9.;
datalines;
Jan-04
Dec-12
;;;;
run;
Puts the correct values to the log.

Conversion of SAS Macro Variable to time stamp

I am trying to convert SAS Macro variable to timestamp and stumped while conversion. The code which I am using is given below.
%let date = '03/15/2013';
%put %sysfunc(inputn(&date,datetime26.6));
Error which I am getting is
WARNING: Argument 1 to function INPUTN referenced by the %SYSFUNC or
%QSYSFUNC macro function is out of range. NOTE: Mathematical
operations could not be performed during %SYSFUNC function execution.
The result of the operations have been set
to a missing value.
Please let me know if someone knows answers to this.
That is not a DATETIME, that is a DATE format (to INPUT, which depends on the incoming data, not the outgoing). You also need to remove the quotes, SYSFUNC treats quotes as characters, not as string delimiters.
%let date = 03/15/2013;
%put %sysfunc(inputn(&date,MMDDYY10.));
To actually create the datetime, you need to use PUT:
%let date = 03/15/2013;
%put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.));
However, the better way to do this if you can is to use a date constant...
%let date=15MAR2013;
%put "&date."d;
Joe is mostly correct. If you want a datetime string of midnight 3/15/13, then use
%let date = 03/15/2013;
%put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.));
Just using PUTN on a date string to "convert" a date to datetime will convert the number of days from epoch (01JAN1960) to the number of seconds from epoch.
My preference for working with dates in macro variables is to store the actual numeric value in the macro variable, and if I need to view/print the formatted value then assign a format to it on the fly:
%let date = %sysfunc(mdy(3,15,2013));
%put %sysfunc(putn(&date,date9.));
That allows you to use it in comparisons like the below (which I find is the most common task):
data xx;
set something;
where datefield = &date;
run;

sas date - convert today() into yyyymmdd format

How do I convert a SAS date such as "30JUL2009"d into YYYYMMDD format (eg 20090730)?
So for instance:
data _null_;
format test ?????;
test=today();
put test=;
run;
Would give me "test=20090730" in the log...
data _null_;
format test yymmddn8.;
test=today();
put test=;
run;
YYMMDDxw. documentation
%let expectdate1=%sysfunc(putn(%eval(%sysfunc(today())-1),yymmddn8.));
You want to use the format yymmddn8. The 'n' means no separator.
Per http://support.sas.com/kb/24/610.html you can specify B for blank, C for colon, D for dash, N for no separator, P for period, or S for slash.
There is this one that should do the trick too
%let today=%sysfunc(today(),yymmddn8.);
%put &today.;
Everything on the link below
https://communities.sas.com/thread/60111
here's how I did it in macro, but surely there must be a format??!!!
%let today=%sysfunc(compress(%sysfunc(today(),yymmddd10.),'-'));
its weird - the INFORMAT yymmdd8. gives YYYYMMDD result, whereas the FORMAT yymmdd8. gives a YY-MM-DD result!!
You can see all date and time formats in Help tab when you enter 'date' to Index tab and then selecr 'date and time formats'