Hello,
I have dates in my dataset in yyyy-mm-dd format. For example, 2016-09-26. I would like to calculate days with them, and need to format these accordingly. I've tried:
format variable date9.;
But it doesn't work. Is there a way to convert this date format to a valid one that can be used for calculations? Thank you in advance!
The Format of a SAS date variable has no impact on the ability to calculate intervals and such.
I would suspect that your date variable(s) are actually character and you need a SAS date valued variable which would be
data want;
set have;
DateSas = input(yourdate,yymmdd10.);
format DateSas date9.;
run;
Depending on the calculations you want there are many functions available but we would need an example of what you want to show specific examples.
Doesn't work is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
The Format of a SAS date variable has no impact on the ability to calculate intervals and such.
I would suspect that your date variable(s) are actually character and you need a SAS date valued variable which would be
data want;
set have;
DateSas = input(yourdate,yymmdd10.);
format DateSas date9.;
run;
Depending on the calculations you want there are many functions available but we would need an example of what you want to show specific examples.
Doesn't work is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
Thanks for your help! The date was indeed stored as a character value, so I was getting an invalid numeric value error message. The input statement preceeding the format statement did the trick.
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.