format convert date variable using SAS - date

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

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 how to change a date format in a where clause

I have some data which has the date stored in the yymmn6. format and others that are stored in the date9. format
I want to do a proc sql statement using the where clause but in order to compare two dates the date9. fields need to be in the yymmd6. format
is there an equivalent of this which can be used in the where clause
proc sql;
select dt format=yymmn6.,
from have;
quit;
i.e.
proc sql;
select *
from have WHERE dt format=yymmn6. = other_date;
quit;
A SAS variable formatted as a date is simply a number (representing days since 01JAN1960).
If other_date is also a SAS date value (regardless of format), you don't need to apply a format to either variable.
If other_date is a string representing a date (e.g. 201501), you can apply the format to dt, or input other_date to a SAS date.
/* Apply format to dt to match other_date */
proc sql ;
select *
from have
where put(dt,yymmn6.) = other_date ;
quit ;
/* Be careful here though as 201501 will input as 2015-01-01 */
proc sql ;
select *
from have
where dt = input(other_date,yymm6.) ;
quit ;