Converting a date string with leading year into a data in SAS - sas

I've had various success with the input statement converting character strings into dates, but when the date is formatted YY/MM/DD, I can't find the right INFORMAT to get INPUT to convert it to a number.
e.g.
data test;
format date DDMMYY10.;
date_string = "22/11/07";
date_no = input(date_string, YYMMDDw.);
date = date_no;
run;
I've tried using the list of SAS INFORMATs but non of them seem to work when converting this type of date. I can only get INPUT to work with formats like DDMMYY10.
Expected output would be:
date_string "22/11/07"
date_no 22956
date 07/11/2022

Code:
data test;
format date DDMMYY10.;
date_string = "22/11/07";
date_no = input(date_string, YYMMDD8.);
date = date_no;
put date=;
run;
Log:
date=07/11/2022

Related

How to convert dd-mmm-yyyy to date9 in sas

Want to convert date = 01-SEP-2021 to date = 01SEP2021. I used this code to convert:
New date = input(compress(date,'-'),date9.);
But that didn't work. I also used substring to remove the parts of day month and year but seems this method is a bit lengthy.
If the type is numeric and SAS already displays it as a date then it's a formatting issue.
proc datasets lib=work nolist;
modify have;
format date date9.;
quit;
Otherwise, it must be a character and you have to convert it to a date and display it in date9. format.
data want;
date = '01-SEP-2021';
new_date = input(date, anydtdte.); *anydtdte. automatically reads a variety of date formats;
format new_date date9.;
run;

How to convert Character Year to SAS date 1/1/Year?

In Base SAS, how do i convert a character Year (2018) to 01/01/2018 in date format?
data date;
length charyear $8.;
charyear='2018';
run;
In general, when constructing a date from date parts, you should use SAS MDY function. MDY(month,day,year) returns a numeric date, but you need to convert all arguments to numbers, before using the function.
data date;
length charyear $8.;
charyear='2018';
format numDate date9.;
numDate = mdy(1,1, input(charyear, best.));
run;
In your particular case it can be done even easier by using function INPUT with a corresponding date informat:
data date;
length charyear $8.;
charyear='2018';
format numDate date9.;
numDate = input("01JAN" || charyear, date9.);
run;

How to convert string to date in SAS?

I have a table in SAS where in one column, the date is stored (e.g. "2005/10").
How do I have to convert this to a SAS data format?
Among many other tries, I tried this code:
data test;
format date YYMMS.;
date = input(ObservationMonth, YYMMS.);
put date=date9.;
run;
You could just use the anydtdte. informat.
data want;
format date yymms.;
text="2005/10";
date=input(text,anydtdte.);
put date;
run;
This informat detects most date formattings and converts the character-stored value to a SAS date.
One way is to use substr() and mdy() to extract the date components:
data _null_;
ObservationMonth ="2005/10";
date =mdy(substr(ObservationMonth,6,2),1,substr(ObservationMonth,1,4));
put date = date9.;
run;
Another option is to use the anydtdte informat (note that results may differ depending on your locale):
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth,anydtdte.);
put date = date9.;
run;
Yet another option is to modify the input to enable use of the YYMMDDw. informat:
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth!!'/01', YYMMDD10.);
put date = date9.;
run;
you're fantastic, guys! Thank you so much, with a "set" statement it works fine!

Convert character value to DATE MMDDYYYY

I have character variable as below
03211962
04181968
when i run this , the excEl output shows like
3211962
It removes 0.
I need to change this as DATE MMDDYYYY.
You can do this using the function mdy. Here is an example based on what you've provided.
data ds;
input dte $;
datalines;
03211962
04181968
;
run;
data ds;
set ds;
format date MMDDYY10.;
mnth = input(substr(dte,1,2),2.);
day = input(substr(dte,3,2),2.);
year = input(substr(dte,5,4),4.);
date = mdy(mnth,day,year);
run;
In the first data step I read in the two values as the character variable dte and then in the second data step I convert the values to their numeric counterparts. The line mnth = input(substr(dte,1,2),2.); is just taking the first 2 characters in the dte variable and converting it to a numeric variable. The mdy function takes numeric values for month, day and year.
I would recommend looking here for other ways to format the date.

How to input to date format from a number

I'm trying to put a number eg 20141001 into a date9. format eg 01OCT14.
I've tried to use the input function with an input format of yymmddn8. but SAS throws out 'informat could not be found or loaded'
Any ideas how to get around this? (Sample code below)
data _null_;
date=20141001;
output=input(date,yymmddn8.);
format output date9.;
put output=;
run;
You are almost there. Although there is a YYMMDDN format there is not an informat of the same name. Use the YYMMDD informat. The Input function is expecting a character string i.e. the DATE variable. Redefine DATE as a character variable e.g.
data _null_;
date='20141001';
output=input(date,yymmdd8.);
format output date9.;
put output=;
run;
Alternatively you could have used these assignments:
output = input('20141001',yymmdd8.);
or
output = '01oct2014'd;