In serious need of help, struggling with date conversion. I am starting with a list of dates, let's call them start_date and end_date, and they are in the format (e.g. 2016/10/05) YY/MM/DD. I am struggling converting them into a numeric value so that I can then convert them into a SAS date then back into a character value. I have been online for hours looking for some type of explanation and code, but I have had no luck. I have even compressed the variable and taken out the '/', tried renaming it so I could keep original name then used the input statement but still not luck. Even tried using the put statement...please I am so stuck. I have been programming consistently for about a month now and this is my first encounter with dates.
data test;
format date yymmdd10.;
date = input('2016-01-01',yymmdd10.);run;
This does not work because the "dates" in my orginial data are actually character variables. I need to convert the character "dates" to numeric before I can actually make them into SAS dates. I just can't figure out how...everything I try gives me an error or all missing values
The suggested solution does work.
You may need to plug in the variable names, such as:
start_sasdate = input(start_date, yymmdd10.);
You can't re-use the same variable name, since a character variable should not be assigned a numeric value.
Other possibilities if this is not working for you: Maybe your existing variables are not actually character. Maybe that are already SAS dates, with a format being applied for printing purposes. A PROC CONTENTS will reveal that quickly enough.
I preformed a Proc Contents and this is the result for those variables.
Start_Date | Char | 50 | $CHAR50. | Start Date |
Stop_Date | Char | 50 | $CHAR50. | Stop Date |
Your PROC CONTENTS results show one more possibility to guard against. With a $CHAR informat, it is possible your variable contains leading blanks. So protect against that with:
start_sasdate = input(left(start_date), yymmdd10.);
@nash_sas gave you the answer. Here's a somewhat wordier version:
data; date_str = '2016/10/05'; date_num = input(date_str, yymmdd10.); put 'Numeric date is ' date_num; put 'Formatted numeric date is ' date_num mmddyy10.; run;
date_str is your original date as a character string. date_num is its numeric equivalent. Without a format date_num is 20732. If printed using the mmddyy10. format, date_num is 10/05/2016.
this is my code:
data test;
set orig;
format start_date2 yymmdd10.;
start_date2 = input(start_date,yymmdd10.);
run;
I am still receiving an enormous amount of errors in my log that basically tells me that I have an invalid argument to function input. I may be interrupting the original data wrong, because this is not working. I am still confused as to how you can go directly from a character variable into a SAS date without converting to numeric first. I am sorry, but I am fairly new to SAS.
Post the log with the code and the error messages.
By any chance do you have dummy values such as 9999/99/99 for missing dates? 99 are not acceptable months or days and will generate errors.
This is not necessarily a bad result. This is saying that out of 14,186 observations, 34 had an incoming date value that wasn't valid. The log messages will give you more detail about what is actually in those incoming dates.
That's a separate question that could be addressed in due time ... how does the INPUT function convert from character to numeric. First, though, see if this clears up the problem:
data test;
set orig;
format start_date2 yymmdd10.;
start_date2 = input( left(start_date),yymmdd10.);
run;
The INPUT() function should let you convert strings to dates. But you will need to generate a numeric variable to store them as you cannot store them back into the character variable. So if you have the variable DATEVAR you can make a new variable DATE_NUM that has it as a real date value.
date_num = input(datevar,yymmdd10.);
format date_num yymmdd10. ;
If you want the new variable to have the same name again then you will need to rename the variables.
rename datevar=date_char date_num=datevar ;
Okay, so when I look into my contents window it is correctly formatted... Why am I still getting this gigantic amount of errors in my log window?
Well, the log is messy but it doesn't represent a lot of observations. let's clean it up and see where the problem comes from:
data test;
set orig;
format start_date2 yymmdd10.;
start_date2 = input( start_date, ??yymmdd10.);
if start_date2 = . then put start_date=;
run;
It doesn't appear to be the left-hand justification. So for now I took out the LEFT function. The ?? will suppress all those messages you were getting. And the PUT statement will give you more succinct messages for the problem cases.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.