Hello, I have a variable Date in Char format. I would like to convert that into Date format.
Date
----------
2017-8
2016-7
Please help!
Thanks!
What do you want the day to represent, 1, 15, last day of the month?
Use the MDY() function is probably the easiest here. I broke out how to create the parts if necessary.
data have;
char_date = "2017-8";output;
char_date = "2017-10";output;
run;
data want;
set have;
year_char = scan(char_date, 1, "-");
year_num = input(year_char, 8.);
*same for month, or nest it all in one;
date_sas = mdy(input(scan(char_date, 2, "-"), 8.) , 1, input(scan(char_date, 1, "-"), 8.));
format date_sas yymon7.;
run;
What do you want the day to represent, 1, 15, last day of the month?
Use the MDY() function is probably the easiest here. I broke out how to create the parts if necessary.
data have;
char_date = "2017-8";output;
char_date = "2017-10";output;
run;
data want;
set have;
year_char = scan(char_date, 1, "-");
year_num = input(year_char, 8.);
*same for month, or nest it all in one;
date_sas = mdy(input(scan(char_date, 2, "-"), 8.) , 1, input(scan(char_date, 1, "-"), 8.));
format date_sas yymon7.;
run;
Sorry, I want to just output yyyy-mm no day. Also, your second char_date outputed 2017JAN not 2017OCT
ah..that's because there's a problem with the test data - note the answer though. I didn't assign a variable length so SAS defaults to the first it found, so it read it as 2017-1, not 2017-10.
A SAS date has to have a day component, though you can show only the month year as shown.
Anyways, find the format you want and change the format statement to what you want to see.
The formats are here:
https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm
SAS will require a day of month as part of the value to be treated as a "date" variable for any of the functions.
Best might be to read with the anydtdte. format.
data example; input x $; y = input(x,anydtdte.) ; format y yymmd7.; yy=year(y); mm=month(y); dd=day(y); datalines; 2017-8 2016-7 2016-10 ; run;
Note that the format I assigned will mimic the input values only the month will always have two digits.
Note that other SAS defaults can apply when reading quarters into dates where the month and day are the first of the calendar quarter
There is no such thing. In SAS, dates always refer to a specific day. Always. You can change the value of your character variable if you would like:
data want;
length date $ 7;
set have;
if length(date)=6 then date = cats(substr(date,1,5), '0', substr(date, 6,1));
run;
That creates DATE as a character string, with slightly different values than what it had before. But if you want what SAS considers to be a date, this is not it.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.