convert numeric date into DATE in SAS Enterprise Guide - date

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;

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:

How To store SAS dates in a macro

I am trying to create different datasets with the names starting from a date which has data for different dates. When i am trying to run the code it is reading the date as numeric numbers and not as Dates. Here is the code
data dates;
input dates mmddyy8. name : $10. ;
format dates date9.;
cards;
01312015 swati
02282015 kangan
01232015 Gotam
04302015 Hushiyar
05172015 yash
09192015 Kuldeep
08302015 David
05172015 yash
11192015 Uninayal
11192015 Uninayal
12032015 sahil
;
data dates;
set dates;
format new date9.;
new=intnx('month', dates, 0, 'e');
run;
proc sql;
select distinct new into : new1 -: new8 from dates;
quit;
%put &new1.;
%macro swati;
%do i= 1 %to 1;
data data_&&new&i.;
set dates;
if new="&&new&i." then output data_&&new&i.;
run;
%end;
%mend;
%swati;
When I try running this code it gives me the error that says it is reading the dates stored in the macro as numbers. How do i make SAS read the dates as just dates only?

format convert date variable using SAS

I want to convert a character date variable (categorical) in the format 9/12/1990, 10/1/1990, etc. into this format: 09/12/1990, 10/01/1990, etc. (mmddyy10.) using SAS.
format date_new mmddyy10.;
date_new =input(trim(VAR1),mmddyy10.);
The code is not working.
Try this:
data new;
set old;
/* Parse character date components */
array dt[*] month day year;
do i = 1 to 3;
dt[i] = input(scan(var1, i, "/"), best.);
end;
/* Recontruct date */
date_new = mdy(month, day, year);
format date_new mmddyy10.;
run;
Documentation links:
mdy
scan
SAS dates
Arrays

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;