SAS date format when loaded in is strange - date

This is the original format of my date column when i import it. How do I change to it something like 30April2019
[
Thanks

Seems like your import options used datetime format, instead of date format.
Try declaring import format as DATE9. format

Your DATE column is a datetime value, which is different than a date value.
Use the format DTDATE9. so that the datetime value is rendered (represented) as dd-mon-yyyy when viewing. There are many other DT* formats available for rendering the value in other representations.
Example:
data have;
mydate = datetime(); * current date time;
mydate_raw_unformatted = mydate;
format mydate dtdate9.;
run;
proc print data=have;
run;

Related

SAS date variable

I have a SAS data set A and one of the column is date. 31AUG2010. when I do the following code:
data b;
set a;
newdate=date;
run;
The newdate becomes numeric and 31AUG2010 becomes 18505. How do I solve it?
Apply a format.
See the example below with two different formats applied.
data b;
set a;
newdate=date;
newdate2 = date;
format newdate date9. newdate2 ddmmyy10.;
run;
Dates are just the number of days from 01JAN1960. You need to apply a format. A format doesn't change how the number is stored, just how it is presented to you.
Data a;
set a;
format newdate date9.;
newdate=date;
run;
You have to assign a date format to the new field. Since Date is a numeric field in SAS; SAS assigns the default format which is numeric.
You can assign the format by adding a format statement
data test;
format newdate date9.;
date='31AUG2010'd;
newdate=date;
run;
date has no format assigned but newdate has date9. format assigned.
Output:
newdate=31AUG2010 date=18505

SAS date format returning censored

I have a column called Last Payment as such
last payment
12DEC09:00:00:00
all the observations follow this structure, I've tried taking a substring such that
data want;
set have;
last_payment=substr(last_payment,1,7);
run;
that doesn't work, I've tried formatting the date with the date7. and date.9 but both just return ********, can someone help me format it into a ddmmmyy ty.
You have to use datapart function before formating it in date7. or date9.
Example:
data new;
format date_new date9. ;
date_new = datepart("12DEC09:00:00:00"dt);
run;
The censored text means wrong format used.
if last_payment is a string then replace "datetime_as_string " with last_payment.
if last_payment is datetime then replace "datetime_as_dt" with last_payment.
The code below will handle the two cases where you are reading the date time as string and numeric values.
Code:
data new;
format datetime_as_dt datetime21. dt_to_date date7. datetime1 date7.;
datetime_as_dt="12DEC09:00:00:00"dt;
datetime_as_string ="12DEC09:00:00:00";
/*Converting datetime to date*/
dt_to_date= datepart(datetime_as_dt);
/*converting datetime string to date*/
/*Option1: Convert string to datetime then extract date*/
datetime1 = datepart(input(datetime_as_string,datetime21.));
/*Option2: Extract date from string then convert to date*/
datetime2 = scan(datetime_as_string,1,':');
put _all_;
run;
Log:
datetime_as_dt=12DEC2009:00:00:00
datetime_as_string=12DEC09:00:00:00
dt_to_date=12DEC09
datetime1=12DEC09
datetime2=12DEC09
Table created:

use a character date string as a date in data step

I have the following two macro variables:
%let start_date = 29MAY2014;
%let end_date = 15JUL2014;
I would like to create a dataset which is a series of dates between these (inclusive.) I cannot change the input format of the macro variables &start_date and &end_date.
I have tried many variations of the following, but SAS spits out an error for each:
data base_dates;
do date = put("&start_date",date9.) to put("&end_date",date9.);
output;
end;
format date date11.;
run;
Any help in this would be much appreciated
Use them as date literals, enclose in quotes and add a d at the end.
Do date = "&start_date"d to "&end_date"d;
It was simple; input() instead of put()
data base_dates;
do date = input("&start_date",date9.) to input("&end_date",date9.);
output;
end;
format date date11.;
run;

convert numeric date into DATE in SAS Enterprise Guide

I have a date column which dates are showing as numeric, such as "201101", "201203"...
How can I convert these numeric date into format as "Jan2011", "Mar2012" by using SAS enterprise?
Thanks!
You can do it like this:
DATA test;
input date;
CARDS;
201101
201203
;
RUN;
data test2;
set test;
date2 = input(put(date,6.),yymmn6.);
format date2 monyy7.;
run;
Marti Mayo's answer is correct - but just wanted to point out that it only needs the one step:
DATA test;
format date monyy7. ; *<--- Sets the format you want to view (JAN2011) ;
input date :yymmn6.; *<--- Sets the informat of what you read in(201101);
CARDS;
201101
201203
;RUN;

SAS - custom date format

I'm trying to read in a date field that looks like:
Mar 20 2013 12:00AM
I am using the following user-defined date format and it is not working.
proc format;
picture mydate other='%MON %0d %Y %0H:%0M %p' (datatype=datetime);
run;
data DATASET;
infile CSVFILE
delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
informat TestDate mydate. ;
format TestDate mydate. ;
run;
Can anyone spot what is wrong with this? This is the first time I've needed to use a custom date format and I'm thinking I am missing something small. I am getting the following error:
NOTE: Informat MYDATE was not found or could not be loaded.
I do not believe you can create INformats using Picture; only formats. (INformat = taking a string and converting to a (in this case) date value, format = taking a date value and converting to a string.)
Fortunately, ANYDTDTM. seems to read this in fine. (I changed to 11AM to make sure the time part was okay.)
data test;
input #1 x ANYDTDTM19.;
put x= DATETIME17.;
datalines;
Mar 20 2013 11:00AM
;;;;
run;