Hi all,
Please advice to convert the following to date .
Thanks in Advance .
data have; input DateToConvert $30.; datalines; January 1, 2020 January 1, 2020 January 1, 2020 run;
You can go straight to the data that you want as a part of the Input process. To wit:
data WANT;
INFILE Datalines;
INFORMAT Input_Date ANYDTDTE30.;
FORMAT Input_Date DATE11.;
LENGTH Input_Date 8.;
INPUT Input_Date &;
datalines;
January 1, 2020
February 28, 2020
September 21, 2020
November 15, 2020
;
run;
Results:
The trick here is the ampersand (&) on the Input statement. The ampersand here tells SAS to treat a single space as part of the data. Two spaces are required to separate input fields. Absent the ampersand, SAS will stop at the first blank, and will treat "January 1, 2020" as three separate fields.
I then use an Informat, ANYDTDTE which is a sort of "Jack-of-All-Trades" input date format. If it can be made sense of in terms of a date, SAS will do it. The one caveat with ANYDTDTE is that you have to be aware of something like 12-08-2020. In some localities, 12-08-2020 represents December 8th, and in others August 12th. How it is interpreted through ANYDTDTE is determined by your SAS options.
Lastly, I formatted the date as DATE11, but that format is arbitrary. Any valid SAS date will work fine for display purposes so long as it suits your needs.
Hope that helps,
Jim
Oh, and @dennis_oz,
If you're in a situation where you really do need to take a character date from a SAS data set (instead of taking it from a text file as I did above), the approach is basically the same. Again, I'm going to use ANYDTDTE30. as my informat but this time instead of an Input statement I will use the Input function:
data have;
input DateToConvert $30.;
datalines;
January 1, 2020
February 28, 2020
September 21, 2020
November 15, 2020
;
run;
DATA Want_2;
SET Have;
FORMAT SAS_Date DATE11.;
SAS_Date = INPUT(DateToConvert, ANYDTDTE30.);
RUN;
Results:
Notice the icon on the "SAS_Date" variable. SAS does indeed regard this variable as a date.
I hope one or the other of the above can serve as a solution for what you need.
Jim
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.