How to convert string to date in SAS? - 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!

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;

Sas date format concatenating

I have two datatset Input12 and input18. Below is the code.
New12 dataset have variable score_date in format yymmn6.
data new12;
set input12;
run;
Now adding new variable score_date in dataset new18
%let score_date=201807;
data new18;
set input18;
format score_date yymmn6.;
run;
After concatenating the dataset new 12 and new18 the date format is not in yymmn6.
data new;
set new12 new18;
run;
This is giving informat date for new12 and blank for new18 in new
data new;
set new18 new12;
run;
This is giving correct date format for new18 and blank for new12 in new.
Is there any reason for improper format after concatenating.
Instead of relying on the source data sets for a variable's format, place a format statement in your data set stacking (concatenation) DATA step.
Example:
data new;
set
new12
new18
;
format score_date yymmn6.;
If you mean to have your date representation stored in a macro symbol, as I infer from your posted code, you need to input the representation using the appropriate informat in order to get a SAS date value.
score_date = input ("&score_date", yymmn6.);
format score_date yymmn6.;
An alternative is to set the macro symbol to the source code of a SAS date literal.
%let score_date = "01JUL2018"D;
and resolve that later in DATA Step as perhaps
data new18;
score_date = &score_date;
format score_date yymmn6.;
run;
I tried below codes.
%let score_date="01dec2019"d;
data twelvenew;
set twelve;
score_date=&scoredate.;
format score_date yymmn6.;
run;
%let score_date="01jun2019"d;
data eightnew;
set eight;
score_date=&scoredate.;
format score_date yymmn6.;
run;
data final;
set eightnew twelvenew;
run;
I am getting dates only eightnew in final dataset and others are missing.
Is iam missing anything here.

Convert number to SAS date with a DATE9 format

I have a SAS field where the datatype is number and format is date9.
It has a value like 30SEP2018.
How do I convert it to a SAS date so I can do date operations?
SAS dates are stored as number starting from 1/1/1960 and it starts form number = 0 and increases by 1 for every day. Your date is stored as number and then you get from proc contents and format is to display in the way you want.
data have;
input date:date9.;
format date date9.;
datalines;
30SEP2018
;
proc contents data=have;
run;
you can calculations on above date and gives you appropriate results as shown below
data want;
set have;
new_date= date+1;
new_date1= date-1;
format new_date new_date1 date9.;
run;
proc print; 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;

Character date 31.03.2001 to numerical, SAS

I have a variable that was entered as 31.01.2002 for all entries, and is a character. I would like to put it in numerical form with date9. .
I have tried the below:
date=input(oldway, 10.);
date=input(oldway, date9.);
put date=ddmmyy10.;
date=input(compress(oldway,'.'),10.);
date = INPUT(compress(oldway),date9.);
format date date9.;
run;
I have also tried combinations of the above and to no avail.
Any ideas for forward motion?
Kind Regards!!
You can't input your date using the date9. informat as your string variable isn't in that format. You can use ddmmyy10., though, and that also takes care of the . characters.
data have;
input old $10.;
cards;
31.01.2014
28.02.2014
01.01.2015
;
run;
data want;
set have;
new = input(old, ddmmyy10.);
format new date9.;
run;
try this:
data _null_;
date ="31.01.2014";
date=compress(date,".");
new_date=input(date,ddmmyy8.);
format new_date date9.;
put new_date;
run;